class Quandl::Format::Abstract::Node

Attributes

block[RW]
line[RW]
lines[RW]

Public Class Methods

new(*args) click to toggle source
# File lib/quandl/format/abstract/node.rb, line 15
def initialize(*args)
  # options
  opts = args.extract_options!.symbolize_keys!
  # assign options
  self.line = opts[:line].to_i if opts[:line].present?
  self.block = opts[:block]
end

Public Instance Methods

add(value) click to toggle source
# File lib/quandl/format/abstract/node.rb, line 23
def add(value)
  increment_line
  # clean the value
  value = clean(value)
  # does this mark the end of the node?
  return close if end_of_node?(value)
  # handle the new line
  add_to_lines(value)
  # onwards
  self
end
as_json() click to toggle source
# File lib/quandl/format/abstract/node.rb, line 50
def as_json
  YAML.load(lines.join("\n"))
end
close() click to toggle source
# File lib/quandl/format/abstract/node.rb, line 35
def close
  # pass the node to the block
  block.call(self) unless lines.blank?
  # return a new node
  self.class.new( line: line, block: block )
end

Protected Instance Methods

add_to_lines(value) click to toggle source
# File lib/quandl/format/abstract/node.rb, line 60
def add_to_lines(value)
  # ignore comments
  return false if comment?(value)
  # otherwise append the value
  lines << value
end
clean(value) click to toggle source
# File lib/quandl/format/abstract/node.rb, line 80
def clean(value)
  value.to_s.strip.rstrip
end
comment?(value) click to toggle source
# File lib/quandl/format/abstract/node.rb, line 71
def comment?(value)
  value.blank? || syntax_matches?(:comment, value)
end
end_of_node?(value) click to toggle source
# File lib/quandl/format/abstract/node.rb, line 67
def end_of_node?(value)
  syntax_matches?( :end_of_node, value )
end
increment_line() click to toggle source
# File lib/quandl/format/abstract/node.rb, line 56
def increment_line
  self.line += 1
end
syntax_matches?(key, value) click to toggle source
# File lib/quandl/format/abstract/node.rb, line 75
def syntax_matches?(key, value)
  syn = syntax[key]
  value.to_s[ 0..(syn.length) ] == syn
end