Class: Chordpro::Metadata

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/chordpro/metadata.rb

Instance Method Summary collapse

Constructor Details

#initialize(elements) ⇒ Metadata

Returns a new instance of Metadata.



5
6
7
# File 'lib/chordpro/metadata.rb', line 5

def initialize(elements)
  @elements = elements
end

Instance Method Details

#[](key) ⇒ Object



17
18
19
20
# File 'lib/chordpro/metadata.rb', line 17

def [](key)
  values = select { |directive| directive.name == key }.map(&:value)
  values.length > 1 ? values : values[0]
end

#directivesObject



9
10
11
# File 'lib/chordpro/metadata.rb', line 9

def directives
  @elements.lazy.select { |e| e.is_a?(Directive) && e.name.meta }
end

#each(&block) ⇒ Object



13
14
15
# File 'lib/chordpro/metadata.rb', line 13

def each(&block)
  directives.each(&block)
end

#to_hObject



22
23
24
25
26
27
# File 'lib/chordpro/metadata.rb', line 22

def to_h
  each_with_object({}) do |d, h|
    key = d.name.to_s
    h[key] = h.has_key?(key) ? Array(h[key]).push(d.value) : d.value
  end
end