class SentenceBuilder::SentenceNode

Attributes

custom_structure[R]
custom_structure_hash[R]
name[R]
sort_by_value[R]

Public Class Methods

new(name, options = {}) click to toggle source
Calls superclass method SentenceBuilder::BaseNode::new
# File lib/sentence_builder/sentence_node.rb, line 10
def initialize(name, options = {})
  super(name, options)
  @options = options
  @sort_by_value = options[:sort_by_value].to_i
  @custom_structure = options[:custom_structure] || nil
  @custom_structure_hash = options[:custom_structure_hash] || {}
end

Public Instance Methods

always_use() click to toggle source
# File lib/sentence_builder/sentence_node.rb, line 28
def always_use
  SentenceBuilder::Helper.is_boolean(@options[:always_use]) ? @options[:always_use] : true
end
always_use=(new_value) click to toggle source
# File lib/sentence_builder/sentence_node.rb, line 32
def always_use=(new_value)
  @options[:always_use] = SentenceBuilder::Helper.to_boolean(new_value)
end
display_name() click to toggle source
# File lib/sentence_builder/sentence_node.rb, line 36
def display_name
  @options[:display_name] || nil
end
display_name=(new_display_name) click to toggle source
# File lib/sentence_builder/sentence_node.rb, line 40
def display_name=(new_display_name)
  @options[:display_name] = new_display_name || nil
end
extra() click to toggle source
# File lib/sentence_builder/sentence_node.rb, line 60
def extra
  @options[:extra] || []
end
match_with_options() click to toggle source

If this is true and options is a hash with format {name: 123, value: 456, xxx: 789} Then default option entered as the value in hash gets converted into its name

# File lib/sentence_builder/sentence_node.rb, line 20
def match_with_options
  SentenceBuilder::Helper.is_boolean(@options[:match_with_options]) ? @options[:match_with_options] : false
end
match_with_options=(new_value) click to toggle source
# File lib/sentence_builder/sentence_node.rb, line 24
def match_with_options=(new_value)
  @options[:match_with_options] = SentenceBuilder::Helper.to_boolean(new_value)
end
options() click to toggle source
# File lib/sentence_builder/sentence_node.rb, line 44
def options
  @options[:options] || []
end
options=(new_options) click to toggle source
# File lib/sentence_builder/sentence_node.rb, line 48
def options=(new_options)
  @options[:options] = new_options || []
end
structure(value = nil) click to toggle source
# File lib/sentence_builder/sentence_node.rb, line 83
def structure(value = nil)
  if value.present?
    names = get_option_names(value)
    if match_with_options and names.present?
      add_prefix_and_suffix(enhance_content(names))
    else
      add_prefix_and_suffix(enhance_content(value))
    end
  elsif @default.present?
    names = get_option_names(@default)
    if match_with_options and names.present?
      add_prefix_and_suffix(enhance_content(names))
    else
      add_prefix_and_suffix(enhance_content(@default))
    end
  elsif @custom_structure.present? and @custom_structure.is_a?(String)
    @custom_structure % {sb_prefix: prefix,
                         sb_suffix: suffix}.merge(@custom_structure_hash)
  else
    ''
  end
end
to_hash(current_value = nil) click to toggle source
# File lib/sentence_builder/sentence_node.rb, line 64
def to_hash(current_value = nil)
  {
      name: @name,
      type: type,
      always_use: always_use,
      display_name: display_name,
      default: @default,
      prefix: prefix,
      suffix: suffix,
      current_value: current_value,
      options: options,
      extra: @extra,
      sort_by_value: @sort_by_value,
      custom_structure: @custom_structure,
      custom_structure_hash: @custom_structure_hash,
      sentence: structure(current_value)
  }
end
type() click to toggle source
# File lib/sentence_builder/sentence_node.rb, line 52
def type
  @options[:type] || 'custom'
end
type=(new_type) click to toggle source
# File lib/sentence_builder/sentence_node.rb, line 56
def type=(new_type)
  @options[:type] = new_type || 'custom'
end

Private Instance Methods

add_prefix_and_suffix(value) click to toggle source
# File lib/sentence_builder/sentence_node.rb, line 111
def add_prefix_and_suffix(value)
  [prefix, value, suffix].select(&:present?).join(' ')
end
enhance_content(value, separator = ', ') click to toggle source

Combines array into a string with given separator

# File lib/sentence_builder/sentence_node.rb, line 116
def enhance_content(value, separator = ', ')
  value.is_a?(Array) ? value.join(separator) : value
end
get_option_names(value) click to toggle source
# File lib/sentence_builder/sentence_node.rb, line 107
def get_option_names(value)
  options.select{|o| o[:value] == value}.map{|o| o[:name]}.uniq
end