module WrapIt::DerivedAttributes::ClassMethods

{DerivedAttributes} class methods

Public Instance Methods

collect_derived(name, result = [], method = :concat) click to toggle source

Collects all derived variables with specified name @param name [Symbol] variable name (should contain `@` sign) @param initial [Object] initial collection object @param method [Symbol] collection's method name to concatinate

founded variable with collection

@return [Object] collection of variables

# 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) click to toggle source

retrieves first founded derived variable or nil @param name [Symbol] variable name (should contain `@` sign)

@return [Object, nil] founded variable or nil

# 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
parents() click to toggle source
# File lib/wrap_it/derived_attributes.rb, line 22
def parents
  @parents ||= ancestors.take_while { |a| a != Base }.concat([Base])
end