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
@attr_reader [Hash] datasets YAML
datasets from sources
@api private
@attr_reader [Hash] sources Data loaded from files
@api private
Public Class Methods
Load yaml file
@api private
# File lib/rom/yaml/gateway.rb, line 69 def self.load_file(path) ::YAML.load_file(path) end
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 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
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
# File lib/rom/yaml/gateway.rb, line 41 def self.new(path) super(load_from(path)) end
@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
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
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
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