24
25
26
27
28
29
30
31
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
60
61
62
63
|
# File 'app/lib/katello/lazy_accessor.rb', line 24
def lazy_accessor(*args)
options = args.
@lazy_attributes = [] if @lazy_attributes.nil?
@lazy_attributes = @lazy_attributes.concat args
@lazy_attributes_options ||= {}
fail ArgumentError, "Attribute names must be symbols" if args.any? { |attribute| !attribute.is_a?(Symbol) }
redefined_attr = args.find { |attribute| instance_methods.include?(attribute.to_s) }
Rails.logger.warn "Remote attribute '#{redefined_attr}' has already been defined" if redefined_attr
fail ArgumentError, "Please provide an initializer" if options[:initializer].nil?
args.each do |symbol|
options[:in_group] = args.size > 1
@lazy_attributes_options[symbol.to_s] = options
send :define_method, "#{symbol}_will_change!" do
lazy_attribute_will_change!(symbol)
end
send :define_method, "#{symbol.to_s}_changed?" do
remote_attribute_changed?(symbol.to_s)
end
send :define_method, "#{symbol.to_s}_change" do
lazy_attribute_change(symbol)
end
send :define_method, "#{symbol.to_s}_was" do
lazy_attribute_was(symbol)
end
send :define_method, "#{symbol.to_s}=" do |val|
lazy_attribute_set(symbol, val)
end
send :define_method, symbol do
lazy_attribute_get(symbol)
end
end
end
|