class SentenceBuilder::Builder

Public Class Methods

new(nodes = []) click to toggle source
# File lib/sentence_builder/builder.rb, line 8
def initialize(nodes = [])
  @nodes = nodes.select{|b| b.is_a?(SentenceNode)}
end

Public Instance Methods

get_hash(params = {}, sorted = true) click to toggle source

Return all nodes in order as an hashalways_use

# File lib/sentence_builder/builder.rb, line 13
def get_hash(params = {}, sorted = true)
  get_nodes(sorted).map{|n| n.to_hash(params[n.name])}
end
get_sentence(params = {}, sorted = true, separator = ' ') click to toggle source

 Returns the string representation of nodes and blocks by updating with given parameters

# File lib/sentence_builder/builder.rb, line 18
def get_sentence(params = {}, sorted = true, separator = ' ')
  build_sentence_from_hash(get_hash(params, sorted)).select(&:present?).join(separator)
end

Private Instance Methods

build_sentence_from_hash(nodes) click to toggle source

By parsing each node’s hash, create a sentence

# File lib/sentence_builder/builder.rb, line 29
def build_sentence_from_hash(nodes)
  result = []
  nodes.each do |node|
    # This node does not appear in params
    if node[:current_value].nil?
      if node[:always_use]
        result << node[:sentence]
      end
    else
      result << node[:sentence]
    end
  end
  result
end
get_nodes(sorted = true) click to toggle source

Return nodes by sorting option

# File lib/sentence_builder/builder.rb, line 24
def get_nodes(sorted = true)
  SentenceBuilder::Helper.to_boolean(sorted) ? @nodes.sort_by{|i| i.sort_by_value} : @nodes
end