Class: SentenceBuilder::SentenceNode

Inherits:
BaseNode
  • Object
show all
Defined in:
lib/sentence_builder/sentence_node.rb

Instance Attribute Summary collapse

Attributes inherited from BaseNode

#default

Instance Method Summary collapse

Methods inherited from BaseNode

#prefix, #prefix=, #suffix, #suffix=

Constructor Details

#initialize(name, options = {}) ⇒ SentenceNode

Returns a new instance of SentenceNode.



10
11
12
13
14
15
16
# 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

Instance Attribute Details

#custom_structureObject (readonly)

Returns the value of attribute custom_structure.



8
9
10
# File 'lib/sentence_builder/sentence_node.rb', line 8

def custom_structure
  @custom_structure
end

#custom_structure_hashObject (readonly)

Returns the value of attribute custom_structure_hash.



8
9
10
# File 'lib/sentence_builder/sentence_node.rb', line 8

def custom_structure_hash
  @custom_structure_hash
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/sentence_builder/sentence_node.rb', line 8

def name
  @name
end

#sort_by_valueObject (readonly)

Returns the value of attribute sort_by_value.



8
9
10
# File 'lib/sentence_builder/sentence_node.rb', line 8

def sort_by_value
  @sort_by_value
end

Instance Method Details

#always_useObject



28
29
30
# 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) ⇒ Object



32
33
34
# 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_nameObject



36
37
38
# File 'lib/sentence_builder/sentence_node.rb', line 36

def display_name
  @options[:display_name] || nil
end

#display_name=(new_display_name) ⇒ Object



40
41
42
# File 'lib/sentence_builder/sentence_node.rb', line 40

def display_name=(new_display_name)
  @options[:display_name] = new_display_name || nil
end

#extraObject



60
61
62
# File 'lib/sentence_builder/sentence_node.rb', line 60

def extra
  @options[:extra] || []
end

#match_with_optionsObject

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



20
21
22
# 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) ⇒ Object



24
25
26
# 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

#optionsObject



44
45
46
# File 'lib/sentence_builder/sentence_node.rb', line 44

def options
  @options[:options] || []
end

#options=(new_options) ⇒ Object



48
49
50
# File 'lib/sentence_builder/sentence_node.rb', line 48

def options=(new_options)
  @options[:options] = new_options || []
end

#structure(value = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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

#typeObject



52
53
54
# File 'lib/sentence_builder/sentence_node.rb', line 52

def type
  @options[:type] || 'custom'
end

#type=(new_type) ⇒ Object



56
57
58
# File 'lib/sentence_builder/sentence_node.rb', line 56

def type=(new_type)
  @options[:type] = new_type || 'custom'
end