Class: YamlEnumeration::Enumeration
- Inherits:
-
Object
- Object
- YamlEnumeration::Enumeration
- Extended by:
- Association
- Defined in:
- lib/yaml_enumeration/enumeration.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
Class Method Summary collapse
- .all ⇒ Object
- .find(id) ⇒ Object (also: find_by_id)
- .find_all_by(column, value = nil) ⇒ Object
- .find_by(column, value = nil) ⇒ Object
- .find_by_type(type) ⇒ Object
- .load_values(filename) ⇒ Object
- .value(hash) ⇒ Object
- .where(matches) ⇒ Object
-
.with_named_items(column = :type) ⇒ Object
Define singleton methods .BLAH for each type.
Instance Method Summary collapse
-
#==(other) ⇒ Object
def method_missing(name, *args, &block) if name == ‘=’ && self.class.attributes.include?(name.to_sym) self.class.values[name.to_sym] = args.pop elsif name == ‘?’ && self.class.attributes.include?(name.to_sym) !!self.class.values[name.to_sym] elsif self.class.attributes.include?(name.to_sym) self.class.values[name.to_sym] else super end end.
- #to_s ⇒ Object
Methods included from Association
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
12 13 14 |
# File 'lib/yaml_enumeration/enumeration.rb', line 12 def id @id end |
Class Method Details
.all ⇒ Object
75 76 77 78 79 |
# File 'lib/yaml_enumeration/enumeration.rb', line 75 def all return [] unless values # values.keys.map {|id| new(id)} # override this if you do not want to keep associated obj loaded against self.all_instances ||= values.keys.map {|id| new(id)} end |
.find(id) ⇒ Object Also known as: find_by_id
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/yaml_enumeration/enumeration.rb', line 81 def find(id) case id when Integer all.detect {|a| a.id == id} when NilClass nil else all.detect {|a| a.type == id.to_s} end end |
.find_all_by(column, value = nil) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/yaml_enumeration/enumeration.rb', line 105 def find_all_by(column, value=nil) case column when Hash raise "Hashes with multiple filters are not support so far" if column.keys.size > 1 key = column.keys[0] all.select {|item| item.send(key) == column[key] } else all.select {|item| item.send(column) == value } end end |
.find_by(column, value = nil) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/yaml_enumeration/enumeration.rb', line 93 def find_by(column, value=nil) case column when Hash raise "Hashes with multiple filters are not support so far" if column.keys.size > 1 key = column.keys[0] all.find {|item| item.send(key) == column[key] } else all.find {|item| item.send(column) == value } end end |
.find_by_type(type) ⇒ Object
117 118 119 |
# File 'lib/yaml_enumeration/enumeration.rb', line 117 def find_by_type(type) all.detect {|a| a.type == type.to_s} end |
.load_values(filename) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/yaml_enumeration/enumeration.rb', line 14 def self.load_values(filename) file = File.join(Rails.root, 'db', 'enumerations', "#{filename}.yml") YAML.load(ERB.new(File.read(file)).result).values.each do |data| value data.symbolize_keys end end |
.value(hash) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/yaml_enumeration/enumeration.rb', line 21 def self.value(hash) unless hash.key?(:id) && hash.key?(:type) raise "invalid definition" end self.attributes ||= Set.new self.attributes += hash.keys hash.keys.each do |attr| # defining getter method unless method_defined?(attr) define_method attr do self.class.values[id][attr] end end # defining question method unless method_defined?("#{attr}?") define_method "#{attr}?" do !!self.class.values[id][attr] end end # defining setter method unless method_defined?("#{attr}=") define_method "#{attr}=" do |v| self.class.values[id][attr] = v end end end self.values ||= {} self.values[hash[:id]] = hash end |
.where(matches) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/yaml_enumeration/enumeration.rb', line 121 def where(matches) raise "Only the hash form of where is supported, not #{matches}" unless matches.is_a?(Hash) all.select do |item| matches.all? {|k,v| item.send(k) == v } end end |
.with_named_items(column = :type) ⇒ Object
Define singleton methods .BLAH for each type
130 131 132 133 134 135 136 |
# File 'lib/yaml_enumeration/enumeration.rb', line 130 def with_named_items(column = :type) all.each do |item| define_singleton_method "#{item.send(column).upcase}" do find_by(column, item.send(column).downcase) end end end |
Instance Method Details
#==(other) ⇒ Object
def method_missing(name, *args, &block)
if name[-1] == '=' && self.class.attributes.include?(name[0..-2].to_sym)
self.class.values[id][name[0..-2].to_sym] = args.pop
elsif name[-1] == '?' && self.class.attributes.include?(name[0..-2].to_sym)
!!self.class.values[id][name[0..-2].to_sym]
elsif self.class.attributes.include?(name.to_sym)
self.class.values[id][name.to_sym]
else
super
end
end
66 67 68 |
# File 'lib/yaml_enumeration/enumeration.rb', line 66 def ==(other) other && id == other.id end |
#to_s ⇒ Object
70 71 72 |
# File 'lib/yaml_enumeration/enumeration.rb', line 70 def to_s "#{self.class}(#{self.class.values[id].inspect})" end |