class ROM::JSON::Gateway

JSON gateway

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

@example

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

@api public

Attributes

datasets[R]

@attr_reader [Hash] datasets JSON datasets from sources

@api private

sources[R]

@attr_reader [Hash] sources Data loaded from files

@api private

Public Class Methods

load_file(path) click to toggle source

Load json file

@api private

# File lib/rom/json/gateway.rb, line 72
def self.load_file(path)
  ::JSON.parse(File.read(path))
end
load_files(path) click to toggle source

Load json files from a given directory and return a name => data map

@api private

# 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) click to toggle source

Load data from json file(s)

@api private

# 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) click to toggle source

Create a new json gateway from a path to file(s)

@example

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

@param [String, Pathname] path The path to your JSON file(s)

@return [Gateway]

@api public

Calls superclass method
# File lib/rom/json/gateway.rb, line 44
def self.new(path)
  super(load_from(path))
end
new(sources) click to toggle source

@param [String] path The absolute path to json file

@api private

# File lib/rom/json/gateway.rb, line 79
def initialize(sources)
  @sources = sources
  @datasets = {}
end

Public Instance Methods

[](name) click to toggle source

Return dataset by its name

@param [Symbol]

@return [Array<Hash>]

@api public

# File lib/rom/json/gateway.rb, line 91
def [](name)
  datasets.fetch(name)
end
dataset(name) click to toggle source

Register a new dataset

@param [Symbol]

@return [Dataset]

@api public

# File lib/rom/json/gateway.rb, line 102
def dataset(name)
  datasets[name] = Dataset.new(sources.fetch(name.to_s))
end
dataset?(name) click to toggle source

Return if a dataset with provided name exists

@api public

# File lib/rom/json/gateway.rb, line 109
def dataset?(name)
  datasets.key?(name)
end