Class: ROM::JSON::Gateway
- Inherits:
-
Gateway
- Object
- Gateway
- ROM::JSON::Gateway
- Defined in:
- lib/rom/json/gateway.rb
Overview
JSON gateway
Connects to a json 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 json file.
-
.load_files(path) ⇒ Object
private
Load json files from a given directory and return a name => data map.
-
.load_from(path) ⇒ Object
private
Load data from json file(s).
-
.new(path) ⇒ Gateway
Create a new json 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.
79 80 81 82 |
# File 'lib/rom/json/gateway.rb', line 79 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.
32 33 34 |
# File 'lib/rom/json/gateway.rb', line 32 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.
27 28 29 |
# File 'lib/rom/json/gateway.rb', line 27 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 json file
72 73 74 |
# File 'lib/rom/json/gateway.rb', line 72 def self.load_file(path) ::JSON.parse(File.read(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 json files from a given directory and return a name => data map
62 63 64 65 66 67 |
# File 'lib/rom/json/gateway.rb', line 62 def self.load_files(path) Dir["#{path}/*.json"].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 json file(s)
51 52 53 54 55 56 57 |
# File 'lib/rom/json/gateway.rb', line 51 def self.load_from(path) if File.directory?(path) load_files(path) else load_file(path) end end |
.new(path) ⇒ Gateway
Create a new json gateway from a path to file(s)
44 45 46 |
# File 'lib/rom/json/gateway.rb', line 44 def self.new(path) super(load_from(path)) end |
Instance Method Details
#[](name) ⇒ Array<Hash>
Return dataset by its name
91 92 93 |
# File 'lib/rom/json/gateway.rb', line 91 def [](name) datasets.fetch(name) end |
#dataset(name) ⇒ Dataset
Register a new dataset
102 103 104 |
# File 'lib/rom/json/gateway.rb', line 102 def dataset(name) datasets[name] = Dataset.new(sources.fetch(name.to_s)) end |
#dataset?(name) ⇒ Boolean
Return if a dataset with provided name exists
109 110 111 |
# File 'lib/rom/json/gateway.rb', line 109 def dataset?(name) datasets.key?(name) end |