Module: Katello::LazyAccessor::ClassMethods

Defined in:
app/lib/katello/lazy_accessor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lazy_attributesObject

Returns the value of attribute lazy_attributes.



9
10
11
# File 'app/lib/katello/lazy_accessor.rb', line 9

def lazy_attributes
  @lazy_attributes
end

Instance Method Details

#lazy_accessor(*args) ⇒ Object

Examples:

lazy_accessor :a, :b, :c,

:initializer => lambda { json = Resources::Candlepin::Product.get(cp_id)[0] },
:unless => lambda { cp_id.nil? }


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.extract_options!
  @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

#lazy_attributes_options(attr) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'app/lib/katello/lazy_accessor.rb', line 11

def lazy_attributes_options(attr)
  if @lazy_attributes_options && @lazy_attributes_options.key?(attr)
    @lazy_attributes_options.fetch(attr.to_s)
  elsif superclass.respond_to?(:lazy_attributes_options)
    superclass.lazy_attributes_options(attr.to_s)
  else
    fail "lazy attribute #{attr} not defined"
  end
end