Class: Giblish::DocAttrBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/giblish/docattr_providers.rb

Overview

a builder class that can be setup with one or more document attribute providers and then used as the sole doc attrib provider where the merged sum of all added providers will be presented.

If more than one added provider set the same doc attrib, the last added has preference.

Each added provider must conform to the itf defined in the DocAttributesBase class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*attr_providers) ⇒ DocAttrBuilder

Returns a new instance of DocAttrBuilder.



100
101
102
103
# File 'lib/giblish/docattr_providers.rb', line 100

def initialize(*attr_providers)
  @providers = []
  add_doc_attr_providers(*attr_providers)
end

Instance Attribute Details

#providersObject (readonly)

Returns the value of attribute providers.



98
99
100
# File 'lib/giblish/docattr_providers.rb', line 98

def providers
  @providers
end

Instance Method Details

#add_doc_attr_providers(*attr_providers) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/giblish/docattr_providers.rb', line 105

def add_doc_attr_providers(*attr_providers)
  return if attr_providers.empty?

  # check itf compliance of added providers
  attr_providers.each do |ap|
    unless ap.respond_to?(:document_attributes) &&
        ap.method(:document_attributes).arity == 3
      raise ArgumentError, "The supplied doc attribute provider of type: #{ap.class} did not conform to the interface"
    end
  end

  @providers += attr_providers
end

#document_attributes(src_node, dst_node, dst_top) ⇒ Object



119
120
121
122
123
# File 'lib/giblish/docattr_providers.rb', line 119

def document_attributes(src_node, dst_node, dst_top)
  result = {}
  @providers.each { |p| result.merge!(p.document_attributes(src_node, dst_node, dst_top)) }
  result
end