Class: OnCondition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(join_table_name, on_table_name = nil) ⇒ OnCondition

Returns a new instance of OnCondition.



5
6
7
8
9
10
11
# File 'lib/sql/on_condition.rb', line 5

def initialize join_table_name, on_table_name = nil
  @join_table_name = join_table_name
  @on_table_name   = on_table_name
  @texts = []
  @curent_logic_op = 'and'
  add_default_relation_text
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(table_name, values_mapper) ⇒ Object (private)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sql/on_condition.rb', line 57

def method_missing table_name, values_mapper
  create_method table_name do |values_mapper|
    statement = values_mapper.map {|col, value|
      if value.is_a? Array or value.is_a? Range
        range = value.map {|v| quote v}.join(',')
        "#{table_name}.#{col} in (#{range})"
      else
        "#{table_name}.#{col} = #{quote(value)}"
      end
    }.join(" and ")
    
    text statement
  end
  
 send table_name, values_mapper
end

Instance Attribute Details

#current_logic_opObject (readonly)

Returns the value of attribute current_logic_op.



3
4
5
# File 'lib/sql/on_condition.rb', line 3

def current_logic_op
  @current_logic_op
end

#join_table_nameObject (readonly)

Returns the value of attribute join_table_name.



3
4
5
# File 'lib/sql/on_condition.rb', line 3

def join_table_name
  @join_table_name
end

#on_table_nameObject (readonly)

Returns the value of attribute on_table_name.



3
4
5
# File 'lib/sql/on_condition.rb', line 3

def on_table_name
  @on_table_name
end

#textsObject (readonly)

Returns the value of attribute texts.



3
4
5
# File 'lib/sql/on_condition.rb', line 3

def texts
  @texts
end

Instance Method Details

#add_default_relation_textObject



13
14
15
# File 'lib/sql/on_condition.rb', line 13

def add_default_relation_text
  add_text default_relation_text if default_relation_text
end

#add_text(text) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/sql/on_condition.rb', line 21

def add_text text
  if not texts.include? text
    if texts.size > 0
      texts << [current_logic_op, text].join(" ")
    else
      texts << text 
    end
  end
end

#default_relation_textObject



36
37
38
39
40
41
# File 'lib/sql/on_condition.rb', line 36

def default_relation_text
  if on_table_name
    foreign_key = get_foreign_key on_table_name
    "#{join_table_name}.#{foreign_key} = #{on_table_name}.id"
  end
end

#text(str) ⇒ Object



17
18
19
# File 'lib/sql/on_condition.rb', line 17

def text str
  add_text str
end

#to_statementObject



31
32
33
34
# File 'lib/sql/on_condition.rb', line 31

def to_statement
  statement = texts.join(" ")
  [join_table_name, "on", statement].join(" ")
end