Class: ROM::YAML::Gateway
- Inherits:
-
Gateway
- Object
- Gateway
- ROM::YAML::Gateway
- Defined in:
- lib/rom/yaml/gateway.rb
Overview
YAML gateway
Connects to a yaml file and uses it as a data-source
Instance Attribute Summary collapse
- #datasets ⇒ Object readonly private
- #sources ⇒ Object readonly private
Class Method Summary collapse
-
.load_file(path) ⇒ Object
private
Load yaml file.
-
.load_files(path) ⇒ Object
private
Load yaml files from a given directory and return a name => data map.
-
.load_from(path) ⇒ Object
private
Load data from yaml file(s).
-
.new(path) ⇒ Gateway
Create a new yaml gateway from a path to file(s).
Instance Method Summary collapse
-
#[](name) ⇒ Array<Hash>
Return dataset by its name.
-
#dataset(name) ⇒ Dataset
Register a new dataset.
-
#dataset?(name) ⇒ Boolean
Return if a dataset with provided name exists.
-
#initialize(sources) ⇒ Gateway
constructor
private
A new instance of Gateway.
Constructor Details
#initialize(sources) ⇒ Gateway
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Gateway.
76 77 78 79 |
# File 'lib/rom/yaml/gateway.rb', line 76 def initialize(sources) @sources = sources @datasets = {} end |
Instance Attribute Details
#datasets ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
29 30 31 |
# File 'lib/rom/yaml/gateway.rb', line 29 def datasets @datasets end |
#sources ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
24 25 26 |
# File 'lib/rom/yaml/gateway.rb', line 24 def sources @sources end |
Class Method Details
.load_file(path) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Load yaml file
69 70 71 |
# File 'lib/rom/yaml/gateway.rb', line 69 def self.load_file(path) ::YAML.load_file(path) end |
.load_files(path) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Load yaml files from a given directory and return a name => data map
59 60 61 62 63 64 |
# File 'lib/rom/yaml/gateway.rb', line 59 def self.load_files(path) Dir["#{path}/*.yml"].each_with_object({}) do |file, h| name = File.basename(file, '.*') h[name] = load_file(file).fetch(name) end end |
.load_from(path) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Load data from yaml file(s)
48 49 50 51 52 53 54 |
# File 'lib/rom/yaml/gateway.rb', line 48 def self.load_from(path) if File.directory?(path) load_files(path) else load_file(path) end end |
.new(path) ⇒ Gateway
Create a new yaml gateway from a path to file(s)
41 42 43 |
# File 'lib/rom/yaml/gateway.rb', line 41 def self.new(path) super(load_from(path)) end |
Instance Method Details
#[](name) ⇒ Array<Hash>
Return dataset by its name
88 89 90 |
# File 'lib/rom/yaml/gateway.rb', line 88 def [](name) datasets.fetch(name) end |
#dataset(name) ⇒ Dataset
Register a new dataset
99 100 101 |
# File 'lib/rom/yaml/gateway.rb', line 99 def dataset(name) datasets[name] = Dataset.new(sources.fetch(name.to_s)) end |
#dataset?(name) ⇒ Boolean
Return if a dataset with provided name exists
106 107 108 |
# File 'lib/rom/yaml/gateway.rb', line 106 def dataset?(name) datasets.key?(name) end |