class WCC::Data::EnumeratedType

Public Class Methods

[](value) click to toggle source
# File lib/wcc/data/enumerated_type.rb, line 10
def self.[](value)
  all.find { |record| record.matches?(value) }
end
all() click to toggle source
# File lib/wcc/data/enumerated_type.rb, line 26
def self.all
  @all ||= db.collect { |data| new(data) }
end
db() click to toggle source
# File lib/wcc/data/enumerated_type.rb, line 20
def self.db
  raise NotImplementedError,
    "The ::db class method should be defined in subclasses as an array " \
    "of hashes containing data for each record."
end
new(args={}) click to toggle source
# File lib/wcc/data/enumerated_type.rb, line 4
def initialize(args={})
  only_attributes(args).each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end
reset() click to toggle source
# File lib/wcc/data/enumerated_type.rb, line 30
def self.reset
  @all = nil
end

Private Class Methods

attributes(*attrs) click to toggle source
# File lib/wcc/data/enumerated_type.rb, line 42
def self.attributes(*attrs)
  attrs.each do |attr|
    defined_attributes << attr
    define_method(attr) do
      instance_variable_get("@#{attr}")
    end
  end
end
defined_attributes() click to toggle source
# File lib/wcc/data/enumerated_type.rb, line 51
def self.defined_attributes
  @attributes ||= []
end
inherited(subclass) click to toggle source
# File lib/wcc/data/enumerated_type.rb, line 55
def self.inherited(subclass)
  subclass.send(
    :instance_variable_set,
    :@attributes,
    defined_attributes.dup
  )
end

Public Instance Methods

matches?(value) click to toggle source
# File lib/wcc/data/enumerated_type.rb, line 14
def matches?(value)
  raise NotImplementedError,
    "The #matches? method should be defined in subclasses and return " \
    "true if the argument matches this record and false otherwise."
end

Private Instance Methods

only_attributes(args) click to toggle source
# File lib/wcc/data/enumerated_type.rb, line 36
def only_attributes(args)
  args.select { |key, value|
    self.class.defined_attributes.include?(key)
  }
end