Class: UnitedAttributes::Attribute
- Inherits:
-
Object
- Object
- UnitedAttributes::Attribute
- Extended by:
- Forwardable
- Defined in:
- lib/united_attributes/attribute.rb
Instance Method Summary collapse
- #domain ⇒ Object
- #domain_units ⇒ Object
-
#initialize(value, options) ⇒ Attribute
constructor
A new instance of Attribute.
- #precision(precision = nil) ⇒ Object
- #to_s ⇒ Object
- #unit ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(value, options) ⇒ Attribute
Returns a new instance of Attribute.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/united_attributes/attribute.rb', line 7 def initialize(value, ) @options = @value = value UnitedAttributes::DOMAINS[@options[:domain]].keys.each do |key| instance_eval do define_singleton_method "as_#{key}s" do as(key) end end end end |
Instance Method Details
#domain ⇒ Object
23 24 25 |
# File 'lib/united_attributes/attribute.rb', line 23 def domain @options[:domain] end |
#domain_units ⇒ Object
40 41 42 |
# File 'lib/united_attributes/attribute.rb', line 40 def domain_units UnitedAttributes::DOMAINS[@options[:domain]] end |
#precision(precision = nil) ⇒ Object
31 32 33 34 |
# File 'lib/united_attributes/attribute.rb', line 31 def precision(precision = nil) return @options[:precision] if precision.nil? UnitedAttributes::Attribute.new(value, @options.merge(precision: precision)) end |
#to_s ⇒ Object
27 28 29 |
# File 'lib/united_attributes/attribute.rb', line 27 def to_s ("%.#{precision}f" % @value) + " " + I18n.t("united_attributes.#{domain}.#{unit}", count: @value) end |
#unit ⇒ Object
19 20 21 |
# File 'lib/united_attributes/attribute.rb', line 19 def unit @options[:unit] end |
#value ⇒ Object
36 37 38 |
# File 'lib/united_attributes/attribute.rb', line 36 def value @value end |