Module: Parxer::InheritedResource

Included in:
BaseParser
Defined in:
lib/parxer/utils/inherited_resource.rb

Instance Method Summary collapse

Instance Method Details

#for_each_ancestor_with_method(object, method_name, &block) ⇒ Object



23
24
25
26
27
28
# File 'lib/parxer/utils/inherited_resource.rb', line 23

def for_each_ancestor_with_method(object, method_name, &block)
  object_ancestors(object).each do |klass|
    next unless klass.respond_to?(method_name)
    block.call(klass.send(method_name))
  end
end

#inherited_collection(object, method_name, collection_class) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/parxer/utils/inherited_resource.rb', line 3

def inherited_collection(object, method_name, collection_class)
  result = collection_class.new

  for_each_ancestor_with_method(object, method_name) do |collection|
    collection.each { |item| result << item }
  end

  result
end

#inherited_hash(object, method_name) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/parxer/utils/inherited_resource.rb', line 13

def inherited_hash(object, method_name)
  result = {}

  for_each_ancestor_with_method(object, method_name) do |hash|
    result.merge!(hash)
  end

  result
end

#object_ancestors(object) ⇒ Object



30
31
32
33
# File 'lib/parxer/utils/inherited_resource.rb', line 30

def object_ancestors(object)
  klass = object.is_a?(Class) ? object : object.class
  klass.ancestors.grep(Class).reverse
end