Module: Attribution::ClassMethods
- Defined in:
- lib/attribution.rb
Instance Method Summary collapse
-
#add_attribute(name, type, metadata = {}) ⇒ Object
Defines an attribute.
-
#attribute_names ⇒ Array<Symbol>
The names of the attributes in the order in which they were defined.
-
#attributes ⇒ Hash{Symbol => Object}
Each attribute name, type and any related metadata in the order in which they were defined.
- #autoload_associations? ⇒ Boolean
-
#belongs_to(association_name, metadata = {}) ⇒ Object
Defines an attribute that is a reference to another Attribution class.
-
#boolean(attr, metadata = {}) ⇒ Object
Defines a boolean attribute.
-
#cast(obj) ⇒ Attribution
An instance of this class.
-
#date(attr, metadata = {}) ⇒ Object
Defines a date attribute.
-
#decimal(attr, metadata = {}) ⇒ Object
Defines a decimal attribute.
-
#float(attr, metadata = {}) ⇒ Object
Defines a float attribute.
-
#has_many(association_name, metadata = {}) ⇒ Object
Defines an attribute that is a reference to an Array of another Attribution class.
-
#integer(attr, metadata = {}) ⇒ Object
Defines a integer attribute.
-
#string(attr, metadata = {}) ⇒ Object
Defines a string attribute.
-
#time(attr, metadata = {}) ⇒ Object
Defines a time attribute.
-
#time_zone(attr, metadata = {}) ⇒ Object
Defines a time zone attribute, based on ActiveSupport::TimeZone.
Instance Method Details
#add_attribute(name, type, metadata = {}) ⇒ Object
Defines an attribute
80 81 82 83 |
# File 'lib/attribution.rb', line 80 def add_attribute(name, type, ={}) attr_reader name attributes << ( || {}).merge(:name => name.to_sym, :type => type.to_sym) end |
#attribute_names ⇒ Array<Symbol>
Returns The names of the attributes in the order in which they were defined.
69 70 71 |
# File 'lib/attribution.rb', line 69 def attribute_names @attribute_names ||= attributes.map{|a| a[:name] } end |
#attributes ⇒ Hash{Symbol => Object}
Returns Each attribute name, type and any related metadata in the order in which they were defined.
59 60 61 62 63 64 65 |
# File 'lib/attribution.rb', line 59 def attributes @attributes ||= if superclass && superclass.respond_to?(:attributes) superclass.attributes.dup else [] end end |
#autoload_associations? ⇒ Boolean
200 201 202 |
# File 'lib/attribution.rb', line 200 def autoload_associations? true end |
#belongs_to(association_name, metadata = {}) ⇒ Object
Defines an attribute that is a reference to another Attribution class.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/attribution.rb', line 212 def belongs_to(association_name, ={}) # foo_id id_getter = "#{association_name}_id".to_sym add_attribute(id_getter, :integer, ) association_class_name = .try(:fetch, :class_name, [name.split('::')[0..-2].join('::'), association_name.to_s.classify].reject(&:blank?).join('::')) define_method(id_getter) do ivar = "@#{id_getter}" if instance_variable_defined?(ivar) instance_variable_get(ivar) else if obj = send(association_name) instance_variable_set(ivar, obj.id) else instance_variable_set(ivar, nil) end end end # foo_id= define_method("#{id_getter}=") do |arg| instance_variable_set("@#{id_getter}", arg.to_i) end # foo define_method(association_name) do if instance_variable_defined?("@#{association_name}") instance_variable_get("@#{association_name}") elsif id = instance_variable_get("@#{association_name}_id") begin association_class = Object.const_get(association_class_name) rescue NameError => ex raise ArgumentError.new("Association #{association_name} in #{self.class} is invalid because #{association_class_name} does not exist") end if self.class.autoload_associations? && association_class.respond_to?(:find) instance_variable_set("@#{association_name}", association_class.find(id)) end else instance_variable_set("@#{association_name}", nil) end end # foo= define_method("#{association_name}=") do |arg| begin association_class = Object.const_get(association_class_name) rescue NameError => ex raise ArgumentError.new("Association #{association_name} in #{self.class} is invalid because #{association_class_name} does not exist") end if instance_variable_defined?("@#{association_name}_id") remove_instance_variable("@#{association_name}_id") end instance_variable_set("@#{association_name}", association_class.cast(arg)) end end |
#boolean(attr, metadata = {}) ⇒ Object
Defines a boolean attribute
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/attribution.rb', line 100 def boolean(attr, ={}) add_attribute(attr, :boolean, ) define_method("#{attr}=") do |arg| v = case arg when String then BOOLEAN_TRUE_STRINGS.include?(arg.downcase) when Numeric then arg == 1 when nil then nil else !!arg end instance_variable_set("@#{attr}", v) end alias_method "#{attr}?", attr end |
#cast(obj) ⇒ Attribution
Returns An instance of this class.
49 50 51 52 53 54 55 |
# File 'lib/attribution.rb', line 49 def cast(obj) case obj when Hash then new(obj) when self then obj else raise ArgumentError.new("can't convert #{obj.class} to #{name}") end end |
#date(attr, metadata = {}) ⇒ Object
Defines a date attribute
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/attribution.rb', line 151 def date(attr, ={}) add_attribute(attr, :date, ) define_method("#{attr}=") do |arg| v = case arg when Date then arg when Time, DateTime then arg.to_date when String then Date.parse(arg) when Hash args = Util.extract_values(arg, :year, :month, :day) args.present? ? Date.new(*args.map(&:to_i)) : nil when nil then nil else raise ArgumentError.new("can't convert #{arg.class} to Date") end instance_variable_set("@#{attr}", v) end end |
#decimal(attr, metadata = {}) ⇒ Object
Defines a decimal attribute
140 141 142 143 144 145 |
# File 'lib/attribution.rb', line 140 def decimal(attr, ={}) add_attribute(attr, :decimal, ) define_method("#{attr}=") do |arg| instance_variable_set("@#{attr}", arg.nil? ? nil : BigDecimal.new(arg.to_s)) end end |
#float(attr, metadata = {}) ⇒ Object
Defines a float attribute
129 130 131 132 133 134 |
# File 'lib/attribution.rb', line 129 def float(attr, ={}) add_attribute(attr, :float, ) define_method("#{attr}=") do |arg| instance_variable_set("@#{attr}", arg.nil? ? nil : arg.to_f) end end |
#has_many(association_name, metadata = {}) ⇒ Object
Defines an attribute that is a reference to an Array of another Attribution class.
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/attribution.rb', line 278 def has_many(association_name, ={}) association_class_name = .try(:fetch, :class_name, [name.split('::')[0..-2].join('::'), association_name.to_s.singularize.classify].reject(&:blank?).join('::')) # foos define_method(association_name) do |*query| # TODO: Support a more generic version of lazy-loading begin association_class = Object.const_get(association_class_name) rescue NameError => ex raise ArgumentError.new("Association #{association_name} in #{self.class} is invalid because #{association_class_name} does not exist") end if query.empty? # Ex: Books.all, so we want to cache it. ivar = "@#{association_name}" if instance_variable_defined?(ivar) instance_variable_get(ivar) elsif self.class.autoload_associations? && association_class.respond_to?(:all) instance_variable_set(ivar, Array(association_class.all("#{self.class.name.underscore}_id" => id))) end else # Ex: Book.all(:name => "The..."), so we do not want to cache it if self.class.autoload_associations? && association_class.respond_to?(:all) Array(association_class.all({"#{self.class.name.demodulize.underscore}_id" => id}.merge(query.first))) end end end # foos= define_method("#{association_name}=") do |arg| # TODO: put this in method begin association_class = Object.const_get(association_class_name) rescue NameError => ex raise ArgumentError.new("Association #{association_name} in #{self.class} is invalid because #{association_class_name} does not exist") end attr_name = self.class.name.demodulize.underscore objs = (arg.is_a?(Hash) ? arg.values : Array(arg)).map do |obj| o = association_class.cast(obj) if o.respond_to?("#{attr_name}=") o.send("#{attr_name}=", self) end if o.respond_to?("#{attr_name}_id=") && respond_to?(:id) o.send("#{attr_name}_id=", id) end o end instance_variable_set("@#{association_name}", objs) end end |
#integer(attr, metadata = {}) ⇒ Object
Defines a integer attribute
118 119 120 121 122 123 |
# File 'lib/attribution.rb', line 118 def integer(attr, ={}) add_attribute(attr, :integer, ) define_method("#{attr}=") do |arg| instance_variable_set("@#{attr}", arg.nil? ? nil : arg.to_i) end end |
#string(attr, metadata = {}) ⇒ Object
Defines a string attribute
89 90 91 92 93 94 |
# File 'lib/attribution.rb', line 89 def string(attr, ={}) add_attribute(attr, :string, ) define_method("#{attr}=") do |arg| instance_variable_set("@#{attr}", arg.nil? ? nil : arg.to_s) end end |
#time(attr, metadata = {}) ⇒ Object
Defines a time attribute
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/attribution.rb', line 172 def time(attr, ={}) add_attribute(attr, :time, ) define_method("#{attr}=") do |arg| v = case arg when Date, DateTime then arg.to_time when Time then arg when String then Time.parse(arg) when Hash args = Util.extract_values(arg, :year, :month, :day, :hour, :min, :sec, :utc_offset) args.present? ? Time.new(*args.map(&:to_i)) : nil when nil then nil else raise ArgumentError.new("can't convert #{arg.class} to Time") end instance_variable_set("@#{attr}", v) end end |
#time_zone(attr, metadata = {}) ⇒ Object
Defines a time zone attribute, based on ActiveSupport::TimeZone
193 194 195 196 197 198 |
# File 'lib/attribution.rb', line 193 def time_zone(attr, ={}) add_attribute(attr, :time_zone, ) define_method("#{attr}=") do |arg| instance_variable_set("@#{attr}", arg.nil? ? nil : ActiveSupport::TimeZone[arg.to_s]) end end |