Module: UnitedAttributes::Model

Defined in:
lib/united_attributes/model.rb

Instance Method Summary collapse

Instance Method Details

#unite(accessor, unit, options = {}) ⇒ Object

Should be called like: unite :attribute, :unit, options e.g. unite :duration, :minute, domain: :time, precision: 2,



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/united_attributes/model.rb', line 9

def unite(accessor, unit, options = {})

  options.reverse_merge!({precision: 0})
  options[:unit] = unit

  unless options.has_key?(:domain)
    UnitedAttributes::DOMAINS.each do |key, value|
      if value.has_key? unit
        options[:domain] = key
        break
      end
    end
  end

  class_eval do
    define_method "united_#{accessor}" do
      Attribute.new(self.send(accessor), options)
    end
  end

end