Class: YamlModel

Inherits:
Object
  • Object
show all
Defined in:
lib/yaml_model.rb,
lib/yaml_model/rails.rb

Overview

YamlModel is a super-lightweight datastore for loading and accessing fixed, infrequently changing sets of data such as currencies or languages.

Direct Known Subclasses

Rails

Defined Under Namespace

Classes: Rails

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, attrs = {}) ⇒ YamlModel

Instantiate the model with the provided key and attributes.



51
52
53
54
55
56
57
# File 'lib/yaml_model.rb', line 51

def initialize(key, attrs = {})
  attrs = attrs.inject({}){|h,(k,v)| h[k.to_s] = v; h}
  @key = key.to_s
  self.class.attributes.each do |attribute|
    instance_variable_set("@#{attribute}", attrs[attribute.to_s] || self.class.defaults[attribute.to_s])
  end
end

Class Attribute Details

.attributesObject (readonly)

Returns the value of attribute attributes.



46
47
48
# File 'lib/yaml_model.rb', line 46

def attributes
  @attributes
end

.defaultsObject (readonly)

Returns the value of attribute defaults.



47
48
49
# File 'lib/yaml_model.rb', line 47

def defaults
  @defaults
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/yaml_model.rb', line 7

def key
  @key
end

Class Method Details

.[](key) ⇒ Object Also known as: find

Find the mode



31
32
33
34
# File 'lib/yaml_model.rb', line 31

def [](key)
  return nil unless collection.key?(key.to_s)
  (@instances ||= {})[key.to_s] ||= self.new(key, collection[key.to_s])
end

.allObject

The complete instantiated list of models



21
22
23
# File 'lib/yaml_model.rb', line 21

def all
  @all ||= collection.inject([]){|result, (key, value)| result << self.new(key.to_s, value); result}.freeze
end

.attribute(name, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/yaml_model.rb', line 37

def attribute(name, options = {})
  (@attributes ||= []) << name.to_s
  (@defaults ||= {})[name.to_s] = options.delete(:default)
  
  class_eval do
    attr_reader name
  end
end

.collectionObject

The complete hash of existing themes.



16
17
18
# File 'lib/yaml_model.rb', line 16

def collection
  @collection ||= YAML.load_file(yaml_file).freeze
end

.reset!Object

Reset the collection to be reloaded again from YAML on the next call.



26
27
28
# File 'lib/yaml_model.rb', line 26

def reset!
  @collection, @all, @instances = nil
end

.yaml_file(path_or_file = nil) ⇒ Object



10
11
12
13
# File 'lib/yaml_model.rb', line 10

def yaml_file(path_or_file = nil)
  return @yaml_file unless path_or_file
  @yaml_file = path_or_file
end

Instance Method Details

#<(other) ⇒ Object



67
# File 'lib/yaml_model.rb', line 67

def < (other); (self <=> other) < 0 end

#<=(other) ⇒ Object



68
# File 'lib/yaml_model.rb', line 68

def <= (other); (self <=> other) <= 0 end

#<=>(other) ⇒ Object



63
64
65
# File 'lib/yaml_model.rb', line 63

def <=>(other)
  -(other <=> self.key)
end

#==(other) ⇒ Object



59
60
61
# File 'lib/yaml_model.rb', line 59

def ==(other)
  other.is_a?(self.class) && self.key == other.key
end

#>(other) ⇒ Object



70
# File 'lib/yaml_model.rb', line 70

def > (other); (self <=> other) > 0 end

#>=(other) ⇒ Object



69
# File 'lib/yaml_model.rb', line 69

def >= (other); (self <=> other) >= 0 end

#to_sObject Also known as: to_yaml



72
73
74
# File 'lib/yaml_model.rb', line 72

def to_s
  key
end

#to_symObject



77
78
79
# File 'lib/yaml_model.rb', line 77

def to_sym
  key.to_sym
end