Class: ROM::YAML::Gateway

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

Overview

YAML gateway

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

Examples:

rom = ROM.container(:yaml, '/path/to/data.yml')
gateway = rom.gateways[:default]
gateway[:users] # => data under 'users' key from the yaml 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:

  • sources (Hash)

    The hashmap containing data loaded from files



76
77
78
79
# File 'lib/rom/yaml/gateway.rb', line 76

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.



29
30
31
# File 'lib/rom/yaml/gateway.rb', line 29

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.



24
25
26
# File 'lib/rom/yaml/gateway.rb', line 24

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 yaml file



69
70
71
# File 'lib/rom/yaml/gateway.rb', line 69

def self.load_file(path)
  ::YAML.load_file(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 yaml files from a given directory and return a name => data map



59
60
61
62
63
64
# 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) ⇒ 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 yaml file(s)



48
49
50
51
52
53
54
# 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) ⇒ Gateway

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

Examples:

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

Parameters:

  • path (String, Pathname)

    The path to your YAML file(s)

Returns:



41
42
43
# File 'lib/rom/yaml/gateway.rb', line 41

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>)


88
89
90
# File 'lib/rom/yaml/gateway.rb', line 88

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

#dataset(name) ⇒ Dataset

Register a new dataset

Parameters:

  • (Symbol)

Returns:



99
100
101
# File 'lib/rom/yaml/gateway.rb', line 99

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)


106
107
108
# File 'lib/rom/yaml/gateway.rb', line 106

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