class YamlEnumeration::Enumeration

Attributes

id[RW]

Public Class Methods

all() click to toggle source
# 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) click to toggle source
# 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
Also aliased as: find_by_id
find_all_by(column, value=nil) click to toggle source
# 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) click to toggle source
# 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_id(id)
Alias for: find
find_by_type(type) click to toggle source
# 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) click to toggle source
# 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
new(id) click to toggle source
# File lib/yaml_enumeration/enumeration.rb, line 143
def initialize(id)
  self.id = id
end
value(hash) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

Define singleton methods .BLAH for each type

# 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

Public Instance Methods

==(other) click to toggle source

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

# File lib/yaml_enumeration/enumeration.rb, line 66
def ==(other)
  other && id == other.id
end
to_s() click to toggle source
# File lib/yaml_enumeration/enumeration.rb, line 70
def to_s
  "#{self.class}(#{self.class.values[id].inspect})"
end