Module: WrapIt::DerivedAttributes::ClassMethods

Included in:
WrapIt::DerivedAttributes
Defined in:
lib/wrap_it/derived_attributes.rb

Overview

Instance Method Summary collapse

Instance Method Details

#collect_derived(name, result = [], method = :concat) ⇒ Object

Collects all derived variables with specified name

Parameters:

  • name (Symbol)

    variable name (should contain @ sign)

  • initial (Object)

    initial collection object

  • method (Symbol) (defaults to: :concat)

    collection's method name to concatinate founded variable with collection

Returns:

  • (Object)

    collection of variables



49
50
51
52
53
54
55
# File 'lib/wrap_it/derived_attributes.rb', line 49

def collect_derived(name, result = [], method = :concat)
  parents.select { |p| p.instance_variable_defined?(name) }
         .each do |p|
    result = result.send(method, p.instance_variable_get(name))
  end
  result
end

#get_derived(name) ⇒ Object?

retrieves first founded derived variable or nil

Parameters:

  • name (Symbol)

    variable name (should contain @ sign)

Returns:

  • (Object, nil)

    founded variable or nil



31
32
33
34
35
36
37
38
39
# File 'lib/wrap_it/derived_attributes.rb', line 31

def get_derived(name)
  return instance_variable_get(name) if instance_variable_defined?(name)
  ancestors.each do |ancestor|
    next unless ancestor.instance_variable_defined?(name)
    break if ancestor == Base
    return ancestor.instance_variable_get(name)
  end
  nil
end

#parentsObject



22
23
24
# File 'lib/wrap_it/derived_attributes.rb', line 22

def parents
  @parents ||= ancestors.take_while { |a| a != Base }.concat([Base])
end