Module: BarkestCore::BooleanParser

Included in:
MsSqlFunction
Defined in:
lib/barkest_core/concerns/boolean_parser.rb

Overview

This module will add boolean parsing functions to a class.

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/barkest_core/concerns/boolean_parser.rb', line 34

def self.included(base)
  base.class_eval do

    ##
    # Parses for a 3-way boolean.
    #
    # If the value is nil, then nil is returned.
    # If the value is 'true', 'yes', 'on', '1', 't', or 'y' then true is returned.
    # Otherwise false is returned.
    #
    def self.parse_for_boolean_column(value)
      BarkestCore::BooleanParser.parse_for_boolean_column value
    end

    ##
    # Parses the value for a 3-way SQL filter.
    #
    # If the value is nil, then 'NULL' is returned.
    # If the value parses to true, then '1' is returned.
    # Otherwise '0' is returned.
    #
    def self.parse_for_boolean_filter(value)
      BarkestCore::BooleanParser.parse_for_boolean_filter value
    end

  end
end

.parse_for_boolean_column(value) ⇒ Object

Parses for a 3-way boolean.

If the value is nil, then nil is returned. If the value is ‘true’, ‘yes’, ‘on’, ‘1’, ‘t’, or ‘y’ then true is returned. Otherwise false is returned.



14
15
16
17
18
# File 'lib/barkest_core/concerns/boolean_parser.rb', line 14

def self.parse_for_boolean_column(value)
  return nil if value.to_s.blank?
  value = value.to_s.downcase
  %w(true yes on 1 -1 t y).include? value
end

.parse_for_boolean_filter(value) ⇒ Object

Parses the value for a 3-way SQL filter.

If the value is nil, then ‘NULL’ is returned. If the value parses to true, then ‘1’ is returned. Otherwise ‘0’ is returned.



27
28
29
30
31
# File 'lib/barkest_core/concerns/boolean_parser.rb', line 27

def self.parse_for_boolean_filter(value)
  value = parse_for_boolean_column(value)
  return 'NULL' if value.nil?
  value ? '1' : '0'
end