Class: Metacrunch::Mab2::Document::Datafield
- Inherits:
-
Object
- Object
- Metacrunch::Mab2::Document::Datafield
- Defined in:
- lib/metacrunch/mab2/document/datafield.rb
Instance Attribute Summary collapse
-
#ind1 ⇒ Object
Returns the value of attribute ind1.
-
#ind2 ⇒ Object
Returns the value of attribute ind2.
-
#tag ⇒ Object
Returns the value of attribute tag.
Instance Method Summary collapse
-
#add_subfield(subfield) ⇒ Object
Adds a new sub field.
-
#initialize(tag = nil, ind1: nil, ind2: nil) ⇒ Datafield
constructor
A new instance of Datafield.
-
#subfields(code = nil) ⇒ Metacrunch::Mab2::Document::SubfieldSet
Returns the sub field matching the given code.
-
#to_xml(builder) ⇒ Object
—————————————————————————— Serialization ——————————————————————————.
- #value ⇒ Object
Constructor Details
#initialize(tag = nil, ind1: nil, ind2: nil) ⇒ Datafield
Returns a new instance of Datafield.
9 10 11 12 13 14 |
# File 'lib/metacrunch/mab2/document/datafield.rb', line 9 def initialize(tag = nil, ind1:nil, ind2:nil) @tag = tag @ind1 = ind1 @ind2 = ind2 @subfields = {} end |
Instance Attribute Details
#ind1 ⇒ Object
Returns the value of attribute ind1.
7 8 9 |
# File 'lib/metacrunch/mab2/document/datafield.rb', line 7 def ind1 @ind1 end |
#ind2 ⇒ Object
Returns the value of attribute ind2.
7 8 9 |
# File 'lib/metacrunch/mab2/document/datafield.rb', line 7 def ind2 @ind2 end |
#tag ⇒ Object
Returns the value of attribute tag.
6 7 8 |
# File 'lib/metacrunch/mab2/document/datafield.rb', line 6 def tag @tag end |
Instance Method Details
#add_subfield(subfield) ⇒ Object
Adds a new sub field.
50 51 52 |
# File 'lib/metacrunch/mab2/document/datafield.rb', line 50 def add_subfield(subfield) (@subfields[subfield.code] ||= []) << subfield end |
#subfields(code = nil) ⇒ Metacrunch::Mab2::Document::SubfieldSet
Returns the sub field matching the given code.
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/metacrunch/mab2/document/datafield.rb', line 31 def subfields(code = nil) result = Metacrunch::Mab2::Document::SubfieldSet.new if code.nil? result.concat(@subfields.values.flatten(1)) elsif _subfields = @subfields[code] result.concat(_subfields) elsif (codes = code).is_a?(Array) result.concat(codes.map { |_code| @subfields[_code] }.compact.flatten(1)) end result end |
#to_xml(builder) ⇒ Object
Serialization
58 59 60 61 62 63 64 |
# File 'lib/metacrunch/mab2/document/datafield.rb', line 58 def to_xml(builder) builder.datafield(tag: tag, ind1: ind1, ind2: ind2) do @subfields.values.each do |_subfield_set| _subfield_set.to_xml(builder) end end end |
#value ⇒ Object
16 17 18 |
# File 'lib/metacrunch/mab2/document/datafield.rb', line 16 def value subfields.value end |