Class: Ghaki::Match::Parser::Boolean

Inherits:
Base
  • Object
show all
Defined in:
lib/ghaki/match/parser/boolean.rb

Constant Summary collapse

DEFAULT_VALUES =
{
  true  => %w{ TRUE  YES ON  ENABLED  },
  false => %w{ FALSE NO  OFF DISABLED },
}

Instance Attribute Summary collapse

Attributes inherited from Base

#default_value, #match_lookup

Instance Method Summary collapse

Methods inherited from Base

#add_words, #match_lines, #match_text

Constructor Details

#initialize(opts = {}) ⇒ Boolean

Returns a new instance of Boolean.



20
21
22
# File 'lib/ghaki/match/parser/boolean.rb', line 20

def initialize opts={}; super opts
  opts_boolean opts
end

Instance Attribute Details

#boolean_lookupObject



16
17
18
# File 'lib/ghaki/match/parser/boolean.rb', line 16

def boolean_lookup
  @boolean_lookup || []
end

Instance Method Details

#add_falses(*list) ⇒ Object



35
36
37
# File 'lib/ghaki/match/parser/boolean.rb', line 35

def add_falses *list
  add_words( false => [list].flatten )
end

#add_trues(*list) ⇒ Object



31
32
33
# File 'lib/ghaki/match/parser/boolean.rb', line 31

def add_trues *list
  add_words( true => [list].flatten )
end

#opts_boolean(opts) ⇒ Object



24
25
26
27
28
29
# File 'lib/ghaki/match/parser/boolean.rb', line 24

def opts_boolean opts
  @boolean_lookup = opts[:boolean_lookup]
  add_trues(  opts[:boolean_trues ]  ) unless opts[:boolean_trues].nil?
  add_falses( opts[:boolean_falses]  ) unless opts[:boolean_falses].nil?
  add_words(  DEFAULT_VALUES         ) unless opts[:skip_boolean_defaults]
end

#parse_field(key, val, opts = {}, &block) ⇒ Object

Check whether key is a known boolean field, if so, get truthiness.



46
47
48
49
50
51
52
53
# File 'lib/ghaki/match/parser/boolean.rb', line 46

def parse_field key, val, opts={}, &block
  looky = opts[:boolean_lookup] || self.boolean_lookup
  if looky.member?(key)
    parse_value( val, opts, &block )
  else
    val
  end
end

#parse_value(val, opts = {}, &block) ⇒ Object

Determine truthiness, return if it is.

  • Otherwise return original value or yield if block is given.



41
42
43
# File 'lib/ghaki/match/parser/boolean.rb', line 41

def parse_value val, opts={}, &block
  match_text( val, { :default_value => val }.merge(opts), &block )
end