Class: CLLI::YAMLData

Inherits:
Object
  • Object
show all
Defined in:
lib/clli/yaml_data.rb

Overview

This module provides methods to load external data from YAML files.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*paths) ⇒ YAMLData

Create a new YAMLData object loading the data from the specified YAML file.

Params:

paths

a splat containing the path names.



24
25
26
# File 'lib/clli/yaml_data.rb', line 24

def initialize(*paths)
  @cache = YAML.load_file(self.class.real_path(paths)).freeze
end

Class Method Details

.real_path(*paths) ⇒ Object

Expand the relative path into a real path.

Params:

paths

a splat containing the path names.



13
14
15
# File 'lib/clli/yaml_data.rb', line 13

def real_path(*paths)
  File.join([File.dirname(__FILE__), '..'] + paths)
end

Instance Method Details

#dataObject

Get the cached data Hash. Note the hash is frozen, any modification will cause a RuntimeError to be thrown.



37
38
39
# File 'lib/clli/yaml_data.rb', line 37

def data
  @cache
end

#get(*keys) ⇒ Object

Get the data at the corresponding keys. Note the keys are interpreted as a tree. So a second key indicates that the first key returns a Hash which contains the second key. If no matching records are found then nil will be returned.

Porams:

keys

the keys to lookup.



49
50
51
52
53
54
# File 'lib/clli/yaml_data.rb', line 49

def get(*keys)
  keys.inject(@cache) do |data, key|
    return nil unless data.respond_to?(:[])
    data[key]
  end
end

#keysObject

Get the Hash keys from the cached data.



30
31
32
# File 'lib/clli/yaml_data.rb', line 30

def keys
  @cache.keys
end