Class: ConditionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/condition_builder.rb

Constant Summary collapse

DEFAULT_KIND =
:and
SUPPORTED_KINDS =
[ :and, :or, :xor, :not ]

Instance Method Summary collapse

Constructor Details

#initialize(kind = DEFAULT_KIND) ⇒ ConditionBuilder

Returns a new instance of ConditionBuilder.



8
9
10
11
12
13
14
15
# File 'lib/condition_builder.rb', line 8

def initialize(kind = DEFAULT_KIND)
  super()
  set_kind(kind)
  @_cond = []
  @_crit = {}
  @_join = []
  @_ordr = []
end

Instance Method Details

#add(condition, criteria, order = nil) ⇒ Object

TODO: add << as an alias for add - probably need to do some splatting to handle the parms alias_method :‘<<’, :add See README for usage.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/condition_builder.rb', line 20

def add(condition, criteria, order = nil)
  crit_name = condition[/^(\w|\.)+/].gsub(/\s/, '_').to_s
  crit_sym = crit_name.to_sym
  raise ArgumentError, 'condition already exists' if @_crit.has_key?(crit_sym)
  if criteria.is_a?(Array)
    @_cond << "#{condition} (:#{crit_name})"
  else
    @_cond << "#{condition} :#{crit_name}"
  end
  @_crit[crit_sym] = criteria
  @_ordr << "#{crit_name} #{order.to_s.upcase}" if [ :asc, :desc ].include?(order)
end

#add_join(left_table, left_field, right_table, right_field) ⇒ Object

Add tables and fields for a simple INNER JOIN



34
35
36
37
# File 'lib/condition_builder.rb', line 34

def add_join(left_table, left_field, right_table, right_field)
  # TODO: move the joins into a proper structure and only generate when it gets accessed
  @_join << "INNER JOIN #{right_table} ON #{left_table}.#{left_field} = #{right_table}.#{right_field}"
end

#conditionsObject

Returns the condition string used for the :conditions parameter. See README for usage.



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

def conditions
  @_cond.join(@join)
end

#criteriaObject

Returns the criteria hash. See README for usage.



45
46
47
# File 'lib/condition_builder.rb', line 45

def criteria
  @_crit
end

#joinsObject

Returns the join string used for the :joins parameter. See README for usage.



50
51
52
# File 'lib/condition_builder.rb', line 50

def joins
  @_join.join(' ')
end

#kindObject

Gets the kind of condition



75
76
77
# File 'lib/condition_builder.rb', line 75

def kind
  @kind
end

#kind=(value) ⇒ Object

Sets the kind of condition



80
81
82
# File 'lib/condition_builder.rb', line 80

def kind=(value)
  set_kind(value)
end

#orderObject

Returns the order string used for the :order parameter. See README for usage.



55
56
57
# File 'lib/condition_builder.rb', line 55

def order
  @_ordr.join(', ')
end

#statementsObject

Returns the number of valid statements



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

def statements
  return valid? ? @_cond.length : 0
end

#to_sObject

Returns a string representation - normally used for debugging



65
66
67
# File 'lib/condition_builder.rb', line 65

def to_s
  "conditions: #{@_cond.to_json}, criteria: #{@_crit.to_json}, ordering: #{@_ordr.to_json}"
end

#valid?Boolean

Returns true if it will generate usable output

Returns:

  • (Boolean)


70
71
72
# File 'lib/condition_builder.rb', line 70

def valid?
  @_cond.length > 0 && (@_cond.length == @_crit.length)
end