Module: Attribution::ClassMethods

Defined in:
lib/attribution.rb

Instance Method Summary collapse

Instance Method Details

#add_attribute(name, type, metadata = {}) ⇒ Object



59
60
61
62
# File 'lib/attribution.rb', line 59

def add_attribute(name, type, ={})
  attr_reader name
  attributes << ( || {}).merge(:name => name.to_sym, :type => type.to_sym)
end

#attribute_namesObject



55
56
57
# File 'lib/attribution.rb', line 55

def attribute_names
  @attribute_names ||= attributes.map{|a| a[:name] }
end

#attributesObject



51
52
53
# File 'lib/attribution.rb', line 51

def attributes
  @attributes ||= []
end

#belongs_to(association_name, metadata = {}) ⇒ Object

Association macros



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/attribution.rb', line 149

def belongs_to(association_name, ={})
  # foo_id
  id_getter = "#{association_name}_id".to_sym
  add_attribute(id_getter, :integer, )
  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")
      # TODO: Support a more generic version of lazy-loading
      begin
        association_class = association_name.to_s.classify.constantize
      rescue NameError => ex
        raise ArgumentError.new("Association #{association_name} in #{self.class} is invalid because #{association_name.to_s.classify} does not exist")
      end

      if 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 = association_name.to_s.classify.constantize
    rescue NameError => ex
      raise ArgumentError.new("Association #{association_name} in #{self.class} is invalid because #{association_name.to_s.classify} 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



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/attribution.rb', line 72

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) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/attribution.rb', line 43

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/attribution.rb', line 107

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



100
101
102
103
104
105
# File 'lib/attribution.rb', line 100

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



93
94
95
96
97
98
# File 'lib/attribution.rb', line 93

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) ⇒ Object



206
207
208
209
210
211
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
# File 'lib/attribution.rb', line 206

def has_many(association_name)
  # foos
  define_method(association_name) do |*query|

    # TODO: Support a more generic version of lazy-loading
    begin
      association_class = association_name.to_s.singularize.classify.constantize
    rescue NameError => ex
      raise ArgumentError.new("Association #{association_name} in #{self.class} is invalid because #{association_name.to_s.classify} 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 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 association_class.respond_to?(:all)
        Array(association_class.all({"#{self.class.name.underscore}_id" => id}.merge(query.first)))
      end
    end
  end

  # foos=
  define_method("#{association_name}=") do |arg|
    # TODO: put this in method
    begin
      association_class = association_name.to_s.singularize.classify.constantize
    rescue NameError => ex
      raise ArgumentError.new("Association #{association_name} in #{self.class} is invalid because #{association_name.to_s.classify} does not exist")
    end

    objs = (arg.is_a?(Hash) ? arg.values : Array(arg)).map do |obj|
      o = association_class.cast(obj)
      o.send("#{self.class.name.underscore}=", self)
      o.send("#{self.class.name.underscore}_id=", id)
      o
    end
    instance_variable_set("@#{association_name}", objs)
  end
end

#integer(attr, metadata = {}) ⇒ Object



86
87
88
89
90
91
# File 'lib/attribution.rb', line 86

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

Attribute macros



65
66
67
68
69
70
# File 'lib/attribution.rb', line 65

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/attribution.rb', line 124

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



141
142
143
144
145
146
# File 'lib/attribution.rb', line 141

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