Class: FoodIngredientParser::Loose::Node
- Inherits:
-
Object
- Object
- FoodIngredientParser::Loose::Node
- Includes:
- ToHtml
- Defined in:
- lib/food_ingredient_parser/loose/node.rb
Overview
Parsing result.
Instance Attribute Summary collapse
-
#amount ⇒ Object
Returns the value of attribute amount.
-
#auto_close ⇒ Object
readonly
Returns the value of attribute auto_close.
-
#contains ⇒ Object
Returns the value of attribute contains.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#mark ⇒ Object
Returns the value of attribute mark.
-
#name_parts ⇒ Object
Returns the value of attribute name_parts.
-
#notes ⇒ Object
Returns the value of attribute notes.
Instance Method Summary collapse
- #<<(child) ⇒ Object
- #ends(index) ⇒ Object
-
#initialize(input, interval, auto_close: false) ⇒ Node
constructor
A new instance of Node.
- #inspect(indent = "", variant = "") ⇒ Object
- #inspect_children(indent = "") ⇒ Object
- #inspect_self(indent = "", variant = "") ⇒ Object
- #name ⇒ Object
- #text_value ⇒ Object
- #to_h ⇒ Object
Methods included from ToHtml
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
#amount ⇒ Object
Returns the value of attribute amount.
8 9 10 |
# File 'lib/food_ingredient_parser/loose/node.rb', line 8 def amount @amount end |
#auto_close ⇒ Object (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 |
#contains ⇒ Object
Returns the value of attribute contains.
8 9 10 |
# File 'lib/food_ingredient_parser/loose/node.rb', line 8 def contains @contains end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
9 10 11 |
# File 'lib/food_ingredient_parser/loose/node.rb', line 9 def input @input end |
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
9 10 11 |
# File 'lib/food_ingredient_parser/loose/node.rb', line 9 def interval @interval end |
#mark ⇒ Object
Returns the value of attribute mark.
8 9 10 |
# File 'lib/food_ingredient_parser/loose/node.rb', line 8 def mark @mark end |
#name_parts ⇒ Object
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 |
#notes ⇒ Object
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 |
#name ⇒ Object
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_value ⇒ Object
29 30 31 |
# File 'lib/food_ingredient_parser/loose/node.rb', line 29 def text_value @input[@interval] end |
#to_h ⇒ Object
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 |