class FoodIngredientParser::Loose::Node

Parsing result.

Attributes

amount[RW]
auto_close[R]
contains[RW]
input[R]
interval[R]
mark[RW]
name[RW]
notes[RW]

Public Class Methods

new(input, interval, auto_close: false) click to toggle source
# 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 = @mark = @amount = nil
end

Public Instance Methods

<<(child) click to toggle source
# File lib/food_ingredient_parser/loose/node.rb, line 24
def <<(child)
  @contains << child
end
ends(index) click to toggle source
# File lib/food_ingredient_parser/loose/node.rb, line 20
def ends(index)
  @interval = @interval.first .. index
end
inspect(indent="", variant="") click to toggle source
# File lib/food_ingredient_parser/loose/node.rb, line 42
def inspect(indent="", variant="")
  inspect_self(indent, variant) +
  inspect_children(indent)
end
inspect_children(indent="") click to toggle source
# File lib/food_ingredient_parser/loose/node.rb, line 57
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="") click to toggle source
# File lib/food_ingredient_parser/loose/node.rb, line 47
def inspect_self(indent="", variant="")
  [
    indent + "Node#{variant} interval=#{@interval}",
    name ? "name=#{name.text_value.strip.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
text_value() click to toggle source
# File lib/food_ingredient_parser/loose/node.rb, line 28
def text_value
  @input[@interval]
end
to_h() click to toggle source
# File lib/food_ingredient_parser/loose/node.rb, line 32
def to_h
  r = {}
  r[:name] = name.text_value.strip if name && name.text_value.strip != ''
  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