Class: ROM::JSON::Gateway

Inherits:
Gateway
  • Object
show all
Defined in:
lib/rom/json/gateway.rb

Overview

JSON gateway

Connects to a json file and uses it as a data-source

Examples:

ROM.setup(:json, '/path/to/data.json')

rom = ROM.finalize.env

gateway = rom.gateways[:default]

gateway.dataset?(:users) # => true
gateway[:users] # => data under 'users' key from the json file

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • path (String)

    The absolute path to json file



79
80
81
82
# File 'lib/rom/json/gateway.rb', line 79

def initialize(sources)
  @sources = sources
  @datasets = {}
end

Instance Attribute Details

#datasetsObject (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

#sourcesObject (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)

Examples:

gateway = ROM::JSON::Gateway.new('/path/to/files')

Parameters:

  • path (String, Pathname)

    The path to your JSON file(s)

Returns:



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

Parameters:

  • (Symbol)

Returns:

  • (Array<Hash>)


91
92
93
# File 'lib/rom/json/gateway.rb', line 91

def [](name)
  datasets.fetch(name)
end

#dataset(name) ⇒ Dataset

Register a new dataset

Parameters:

  • (Symbol)

Returns:



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

Returns:

  • (Boolean)


109
110
111
# File 'lib/rom/json/gateway.rb', line 109

def dataset?(name)
  datasets.key?(name)
end