Class: FoodIngredientParser::Loose::Node

Inherits:
Object
  • Object
show all
Includes:
ToHtml
Defined in:
lib/food_ingredient_parser/loose/node.rb

Overview

Parsing result.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ToHtml

#to_html

Constructor Details

#initialize(input, interval, auto_close: false) ⇒ Node

Returns a new instance of Node.



11
12
13
14
15
16
17
18
19
# File 'lib/food_ingredient_parser/loose/node.rb', line 11

def initialize(input, interval, auto_close: false)
  @input = input
  @interval = interval.is_a?(Range) ? interval : ( interval .. interval )
  @auto_close = auto_close
  @contains = []
  @notes = []
  @name_parts = []
  @mark = @amount = nil
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



8
9
10
# File 'lib/food_ingredient_parser/loose/node.rb', line 8

def amount
  @amount
end

#auto_closeObject (readonly)

Returns the value of attribute auto_close.



9
10
11
# File 'lib/food_ingredient_parser/loose/node.rb', line 9

def auto_close
  @auto_close
end

#containsObject

Returns the value of attribute contains.



8
9
10
# File 'lib/food_ingredient_parser/loose/node.rb', line 8

def contains
  @contains
end

#inputObject (readonly)

Returns the value of attribute input.



9
10
11
# File 'lib/food_ingredient_parser/loose/node.rb', line 9

def input
  @input
end

#intervalObject (readonly)

Returns the value of attribute interval.



9
10
11
# File 'lib/food_ingredient_parser/loose/node.rb', line 9

def interval
  @interval
end

#markObject

Returns the value of attribute mark.



8
9
10
# File 'lib/food_ingredient_parser/loose/node.rb', line 8

def mark
  @mark
end

#name_partsObject

Returns the value of attribute name_parts.



8
9
10
# File 'lib/food_ingredient_parser/loose/node.rb', line 8

def name_parts
  @name_parts
end

#notesObject

Returns the value of attribute notes.



8
9
10
# File 'lib/food_ingredient_parser/loose/node.rb', line 8

def notes
  @notes
end

Instance Method Details

#<<(child) ⇒ Object



25
26
27
# File 'lib/food_ingredient_parser/loose/node.rb', line 25

def <<(child)
  @contains << child
end

#ends(index) ⇒ Object



21
22
23
# File 'lib/food_ingredient_parser/loose/node.rb', line 21

def ends(index)
  @interval = @interval.first .. index
end

#inspect(indent = "", variant = "") ⇒ Object



49
50
51
52
# File 'lib/food_ingredient_parser/loose/node.rb', line 49

def inspect(indent="", variant="")
  inspect_self(indent, variant) +
  inspect_children(indent)
end

#inspect_children(indent = "") ⇒ Object



64
65
66
67
68
69
# File 'lib/food_ingredient_parser/loose/node.rb', line 64

def inspect_children(indent="")
  [
    *contains.map {|child| "\n" + child.inspect(indent + "  ") },
    *notes.map    {|note|  "\n" + note.inspect(indent + "  ", "(note)") }
  ].join("")
end

#inspect_self(indent = "", variant = "") ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/food_ingredient_parser/loose/node.rb', line 54

def inspect_self(indent="", variant="")
  [
    indent + "Node#{variant} interval=#{@interval}",
    name ? "name=#{name.inspect}" : nil,
    mark ? "mark=#{mark.text_value.strip.inspect}" : nil,
    amount ? "amount=#{amount.text_value.strip.inspect}" : nil,
    auto_close ? "auto_close" : nil
  ].compact.join(", ")
end

#nameObject



44
45
46
47
# File 'lib/food_ingredient_parser/loose/node.rb', line 44

def name
  strings = name_parts.map {|n| n.text_value.strip }.reject {|n| n == nil || n == '' }
  return strings.any? ? strings.join(" ") : nil
end

#text_valueObject



29
30
31
# File 'lib/food_ingredient_parser/loose/node.rb', line 29

def text_value
  @input[@interval]
end

#to_hObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/food_ingredient_parser/loose/node.rb', line 33

def to_h
  r = {}
  _name = name
  r[:name] = _name if _name
  r[:marks] = [mark.text_value.strip] if mark
  r[:amount] = amount.text_value.strip if amount
  r[:contains] = contains.map(&:to_h).reject {|c| c == {} } if contains.any?
  r[:notes] = notes.map{|n| n.text_value.strip }.reject {|c| c == '' } if notes.any?
  r
end