class ROM::YAML::Gateway

YAML gateway

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

@example

rom = ROM.container(:yaml, '/path/to/data.yml')
gateway = rom.gateways[:default]
gateway[:users] # => data under 'users' key from the yaml file

@api public

Attributes

datasets[R]

@attr_reader [Hash] datasets YAML 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 yaml file

@api private

# File lib/rom/yaml/gateway.rb, line 69
def self.load_file(path)
  ::YAML.load_file(path)
end
load_files(path) click to toggle source

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

@api private

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

Load data from yaml file(s)

@api private

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

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

@example

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

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

@return [Gateway]

@api public

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

@param [Hash] sources The hashmap containing data loaded from files

@api private

# File lib/rom/yaml/gateway.rb, line 76
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/yaml/gateway.rb, line 88
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/yaml/gateway.rb, line 99
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/yaml/gateway.rb, line 106
def dataset?(name)
  datasets.key?(name)
end