Module: IndexableAttribute::ClassMethods

Defined in:
lib/indexable_attribute.rb

Instance Method Summary collapse

Instance Method Details

#indexable_attribute(attr_name, attr_list, has_id = true) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/indexable_attribute.rb', line 8

def indexable_attribute(attr_name, attr_list, has_id=true)
  _CNST = attr_name.to_s.upcase
  _ATTR = attr_name.to_s.downcase
  _ATTR_ID = has_id ? _ATTR + '_id' : _ATTR
  class_eval %Q{
    #{_CNST} = #{attr_list.map { |i| i.to_sym }.to_s}

      def #{_ATTR}
        #{_CNST}[self[:#{_ATTR_ID}]]
      end

      def #{_ATTR}=(v)
        self[:#{_ATTR_ID}] = #{_CNST}.index(v.to_sym)
      end
      }
end