Module: SimpleModel::Attributes::ClassMethods
- Defined in:
- lib/simple_model/attributes.rb
Instance Method Summary collapse
-
#define_reader_with_options(attr, options) ⇒ Object
Defines a reader method that returns a default value if current value is nil, if :default is present in the options hash.
- #fetch_alias_name(attr) ⇒ Object
- #has_attributes(*attrs) ⇒ Object
-
#has_booleans(*attrs) ⇒ Object
Creates setter and getter methods for boolean attributes.
-
#has_currency(*attrs) ⇒ Object
Creates setter and getter methods for float attributes.
-
#has_dates(*attrs) ⇒ Object
Creates setter and getter methods for date attributes.
-
#has_floats(*attrs) ⇒ Object
Creates setter and getter methods for float attributes.
-
#has_ints(*attrs) ⇒ Object
Creates setter and getter methods for integer attributes.
-
#has_times(*attrs) ⇒ Object
Creates setter and getter methods for time attributes.
Instance Method Details
#define_reader_with_options(attr, options) ⇒ Object
Defines a reader method that returns a default value if current value is nil, if :default is present in the options hash
178 179 180 181 182 183 184 185 186 |
# File 'lib/simple_model/attributes.rb', line 178 def (attr,) unless [:default].blank? define_method (attr.to_s) do val = instance_variable_get("@#{attr.to_s}") val = [:default] if val.nil? val end end end |
#fetch_alias_name(attr) ⇒ Object
169 170 171 172 173 174 |
# File 'lib/simple_model/attributes.rb', line 169 def fetch_alias_name(attr) alias_name = (attr.to_s << "_old=").to_sym self.module_eval("alias #{alias_name} #{attr}") alias_name end |
#has_attributes(*attrs) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/simple_model/attributes.rb', line 55 def has_attributes(*attrs) attrs.each do |attr| attr_reader attr define_method("#{attr.to_s}=") do |val| instance_variable_set("@#{attr}", val) attributes[attr] = val val end end end |
#has_booleans(*attrs) ⇒ Object
Creates setter and getter methods for boolean attributes
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/simple_model/attributes.rb', line 70 def has_booleans(*attrs) = attrs. attrs.each do |attr| attr_reader attr (attr,) define_method("#{attr.to_s}=") do |val| instance_variable_set("@#{attr}", val.to_s.to_b) attributes[attr] = val val end define_method ("#{attr.to_s}?") do send("#{attr.to_s}".to_sym).to_s.to_b end end end |
#has_currency(*attrs) ⇒ Object
Creates setter and getter methods for float attributes
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/simple_model/attributes.rb', line 106 def has_currency(*attrs) = attrs. attrs.each do |attr| attr_reader attr (attr,) define_method("#{attr.to_s}=") do |val| instance_variable_set("@#{attr}", val.to_s.to_currency) attributes[attr] = val val end end end |
#has_dates(*attrs) ⇒ Object
Creates setter and getter methods for date attributes
137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/simple_model/attributes.rb', line 137 def has_dates(*attrs) = attrs. attrs.each do |attr| attr_reader attr (attr,) define_method("#{attr.to_s}=") do |val| instance_variable_set("@#{attr}", val.to_date) attributes[attr] = val val end end end |
#has_floats(*attrs) ⇒ Object
Creates setter and getter methods for float attributes
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/simple_model/attributes.rb', line 120 def has_floats(*attrs) = attrs. attrs.each do |attr| attr_reader attr (attr,) define_method("#{attr.to_s}=") do |val| instance_variable_set("@#{attr}", val.to_f) attributes[attr] = val val end end end |
#has_ints(*attrs) ⇒ Object
Creates setter and getter methods for integer attributes
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/simple_model/attributes.rb', line 90 def has_ints(*attrs) = attrs. attrs.each do |attr| attr_reader attr (attr,) define_method("#{attr.to_s}=") do |val| instance_variable_set("@#{attr}", val.to_i) attributes[attr] = val val end end end |
#has_times(*attrs) ⇒ Object
Creates setter and getter methods for time attributes
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/simple_model/attributes.rb', line 153 def has_times(*attrs) = attrs. attrs.each do |attr| attr_reader attr (attr,) define_method("#{attr.to_s}=") do |val| instance_variable_set("@#{attr}", val.to_time) attributes[attr] = val val end end end |