Module: EasyAttributes::ClassMethods
- Defined in:
- lib/easy_attributes.rb
Instance Method Summary collapse
-
#attr_bytes(attribute, *args) ⇒ Object
attr_bytes allows manipultion and display as kb, mb, gb, tb, pb Adds method: attribute_bytes=() and attribute_bytes(:unit, :option=>value ).
-
#attr_money(attribute, *args) ⇒ Object
Creates an money instance method for the given method, named “#attribute_money” which returns a formatted money string, and a #attribute_money= method used to set an edited money string.
-
#attr_sequence(attribute, *names) ⇒ Object
Defines an attribute as a sequence of names.
-
#attr_values(attribute, *args) ⇒ Object
Defines an attribute as a hash like :key=>value,… where key names are used interchangably with values.
Instance Method Details
#attr_bytes(attribute, *args) ⇒ Object
attr_bytes allows manipultion and display as kb, mb, gb, tb, pb Adds method: attribute_bytes=() and attribute_bytes(:unit, :option=>value )
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/easy_attributes.rb', line 117 def attr_bytes(attribute, *args) name = "#{self.name}##{attribute}" opt = EasyAttributes.(args) #getter, setter = EasyAttributes.getter_setter(attribute) attr_accessor attribute if EasyAttributes::Config.orm == :attr code = %Q( def #{attribute}_bytes(*args) args.size == 0 ? v : EasyAttributes::format_bytes(self.#{attribute}, *args) end def #{attribute}_bytes=(v) self.#{attribute} = EasyAttributes.parse_bytes(v) end ) #puts code class_eval code end |
#attr_money(attribute, *args) ⇒ Object
Creates an money instance method for the given method, named “#attribute_money” which returns a formatted money string, and a #attribute_money= method used to set an edited money string. The original method stores the value as integer (cents, or other precision/currency setting). Options:
-
:money_method - Use this as the alternative name to the money-access methods
-
:units - Use this as an alternative suffix name to the money methods (‘dollars’ gives ‘xx_dollars’)
-
:precision - The number of digits implied after the decimal, default is 2
-
:separator - The character to use after the integer part, default is ‘.’
-
:delimiter - The character to use between every 3 digits of the integer part, default none
-
:positive - The sprintf format to use for positive numbers, default is based on precision
-
:negative - The sprintf format to use for negative numbers, default is same as :positive
-
:zero - The sprintf format to use for zero, default is same as :positive
-
:nil - The sprintf format to use for nil values, default none
-
:unit - Prepend this to the front of the money value, say ‘$’, default none
-
:blank - Return this value when the money string is empty or has no digits on assignment
-
:negative_regex - A Regular Expression used to determine if a number is negative (and without a - sign)
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/easy_attributes.rb', line 150 def attr_money(attribute, *args) opt = args.last.is_a?(Hash) ? args.pop : {} money_method = opt.delete(:money_method) || "#{attribute}_#{opt.delete(:units)||'money'}" class_eval %Q( def #{money_method}(*args) opt = args.last.is_a?(Hash) ? args.pop : {} EasyAttributes.integer_to_money( #{attribute}, #{opt.inspect}.merge(opt)) end def #{money_method}=(v, *args) opt = args.last.is_a?(Hash) ? args.pop : {} self.#{attribute} = EasyAttributes.money_to_integer( v, #{opt.inspect}.merge(opt)) end ) end |
#attr_sequence(attribute, *names) ⇒ Object
Defines an attribute as a sequence of names. :name1=>1, :name2=>2, etc. attr_sequence :attr, :name, … :start=>n, :step=>:n
72 73 74 75 76 77 |
# File 'lib/easy_attributes.rb', line 72 def attr_sequence(attribute, *names) opt = EasyAttributes.(names) values = {} names.inject(opt[:start]||1) { |seq, n| values[n]=seq; seq+=(opt[:step]||1)} attr_values( attribute, values, opt) end |
#attr_values(attribute, *args) ⇒ Object
Defines an attribute as a hash like :key=>value,… where key names are used interchangably with values
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/easy_attributes.rb', line 80 def attr_values(attribute, *args) opt = args.size > 1 ? args.pop : {} hash = args.first # Use :like=>colname to copy from another definition (ClassName#attribute) or from the loaded table columns if opt[:like] hash = EasyAttributes::Config.attributes[opt[:like]] end name = "#{self.name}##{attribute}" EasyAttributes.add_definition( name, hash) code = '' if EasyAttributes::Config.orm == :active_model validates_inclusion_of attribute, :in=>hash.values else attr_accessor attribute end code += %Q( def #{attribute}_sym=(v) self.#{attribute} = EasyAttributes.value_for_sym("#{name}", v) end def #{attribute}_sym EasyAttributes.sym_for_value("#{name}", #{attribute}) end def self.#{attribute}_values EasyAttributes::Config.attributes["#{name}"] end def #{attribute}_is?(*args) EasyAttributes.value_is?("#{name}", attribute, *args) end ) #puts code class_eval code end |