Class: Wice::Columns::ConditionsGeneratorColumnInteger

Inherits:
ConditionsGeneratorColumn show all
Defined in:
lib/wice/columns/column_integer.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ConditionsGeneratorColumn

#initialize

Constructor Details

This class inherits a constructor from Wice::Columns::ConditionsGeneratorColumn

Class Method Details

.get_value(val) ⇒ Object

Note: also used in ColumnRange, hence class method



34
35
36
37
38
39
40
# File 'lib/wice/columns/column_integer.rb', line 34

def self.get_value(val) #:nodoc:
  # Try to determine localized separator using I18n and replace it with default one
  separator = I18n.t!('number.format.separator') rescue nil
  val = val.sub(separator, '.') if val.respond_to?(:sub) && separator

  Integer(val) rescue nil
end

Instance Method Details

#generate_conditions(table_alias, opts) ⇒ Object

:nodoc:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/wice/columns/column_integer.rb', line 60

def generate_conditions(table_alias, opts)   #:nodoc:
  unless opts.is_a? Hash
    Wice.log 'invalid parameters for the grid integer filter - must be a hash'
    return false
  end
  conditions = [[]]
  if opts[:eq]
    op, num = get_op_and_value(opts[:eq])
    if op && num
      conditions[0] << " #{@column_wrapper.alias_or_table_name(table_alias)}.#{@column_wrapper.name} " + op + ' ? '
      conditions << num
    else
      opts.delete(:eq)
    end
  end

  if conditions.size == 1
    return false
  end

  conditions[0] = conditions[0].join(' and ')

  conditions
end

#get_op_and_value(val) ⇒ Object

:nodoc:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wice/columns/column_integer.rb', line 42

def get_op_and_value(val) #:nodoc:
  num = nil
  op  = nil

  # remove spaces
  val = val.gsub(' ', '')

  start_of_num = val =~ /[0-9.-]/ # first digit, dot or negative sign
  if start_of_num
    op = val[0...start_of_num]
    op = '=' if op == ''
    num = ConditionsGeneratorColumnInteger.get_value(val[start_of_num..-1])

    op = nil unless ['<', '>', '<=', '>=', '='].include?(op)
  end
  [op, num]
end