Module: ModelX::Percentage::ClassMethods

Defined in:
lib/model_x/percentage.rb

Instance Method Summary collapse

Instance Method Details

#percentage(*attributes) ⇒ Object

Adds percentage attributes for the given attribute. Per specified attribute, the following are added:

:<attribute>_percentage

Retrieves or accepts the value of the original times 100.

Usage

class MyObject
  include ModelX::Percentage

  attr_accessor :my_attribute
  percentage :my_attribute
end

Now, the following holds true:

object = MyObject.new

object.my_attribute = 0.5
object.my_attribute_percentage # => 50

object.my_attribute_percentage = 10
object.my_attribute # => 0.1

Note that an existing attribute reader must exist. For the writer to be defined, an existing attribute writer must exist.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/model_x/percentage.rb', line 36

def percentage(*attributes)
  attributes.each do |attribute|

    # An attribute must already exist.
    unless instance_methods.include?(:"#{attribute}") || instance_methods.include?(:read_attribute)
      raise ArgumentError, "cannot add percentage attribute #{attribute} - no existing attribute exists"
    end

    define_writer = instance_methods.include?(:"#{attribute}=") || private_instance_methods.include?(:write_attribute)

    # Create a *_percentage reader and writer.
    class_eval "\n      def \#{attribute}_multiplier\n        1 - (\#{attribute}.to_f || 0)\n      end\n\n      def \#{attribute}_increase_multiplier\n        1 + (\#{attribute}.to_f || 0)\n      end\n\n      alias_method  :\#{attribute}_decrease_multiplier, :\#{attribute}_multiplier\n\n      def \#{attribute}_percentage\n        @\#{attribute}_percentage ||= if \#{attribute}.present?\n          \#{attribute}.to_f * 100\n        else\n          nil\n        end\n      end\n\n      def self.human_attribute_name(attribute, options = {})\n        if attribute =~ /_percentage$/\n          super $`, options\n        else\n          super\n        end\n      end\n\n    RUBY\n\n    if define_writer\n      validates_numericality_of :\"\#{attribute}_percentage\", :allow_blank => true\n\n      class_eval <<-RUBY, __FILE__, __LINE__+1\n\n        def \#{attribute}_percentage=(value)\n          if value.present?\n            @\#{attribute}_percentage = value\n            self.\#{attribute} = value.to_f / 100\n          else\n            @\#{attribute}_percentage = nil\n            self.\#{attribute} = nil\n          end\n        end\n\n      RUBY\n    end\n\n  end\nend\n", __FILE__, __LINE__+1