Files

Class Index [+]

Quicksearch

CodedAttributes

CodedAttributes

Constants

VERSION

Public Instance Methods

coded_attribute(method, *attribute_or_codes) click to toggle source
    # File lib/coded_attributes.rb, line 6
 6:   def coded_attribute(method, *attribute_or_codes)
 7:     if [String,Symbol].include?(attribute_or_codes.first.class)
 8:       attribute = attribute_or_codes.shift
 9:     else
10:       attribute = :"#{method}_code"
11:     end
12: 
13:     if attribute_or_codes.first.class == Hash
14:       codes = attribute_or_codes.shift
15:       raise ArgumentError, "Too many arguments" unless attribute_or_codes.blank?
16:     elsif attribute_or_codes.first.class == Array
17:       codes = attribute_or_codes.shift.inject({}) { |h, v| h.merge! h.keys.count => v }
18:       raise ArgumentError, "Too many arguments" unless attribute_or_codes.blank?
19:     else
20:       codes = attribute_or_codes.inject({}) { |h, v| h.merge! h.keys.count => v }
21:     end
22: 
23:     class_variable_set :"@@#{method}_codes", codes
24:     cattr_reader :"#{method}_codes"
25: 
26:     define_method :"#{method}" do
27:       self.class.class_variable_get("@@#{method}_codes")[read_attribute(attribute)]
28:     end
29: 
30:     define_method :"#{method}=" do |value|
31:       write_attribute(attribute, self.class.class_variable_get("@@#{method}_codes").key(value.to_sym))
32:     end
33:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.