32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/parcel/has_parcel.rb', line 32
def has_parcel(name, options = {})
options = { :class => :zip }.merge(options).merge({ :name => name })
unless included_modules.include?(InstanceMethods)
include(InstanceMethods)
if defined?(ActiveRecord) && is_a?(ActiveRecord::Base)
after_save :commit_parcels!
after_destroy :destroy_parcels!
end
end
define_method(name.to_sym) do
instance_variable_get("@_parcel_#{name}") || begin
parcel = Parcel.create_repository(self, options)
instance_variable_set("@_parcel_#{name}", parcel)
parcel
end
end
define_method("#{name}=".to_sym) do |input|
instance = instance_variable_get("@_parcel_#{name}")
instance.destroy if instance
instance_variable_set("@_parcel_#{name}", Parcel.create_repository(self, options, input))
end
end
|