Class: Coupler::Models::Comparison
- Inherits:
-
Sequel::Model
- Object
- Sequel::Model
- Coupler::Models::Comparison
show all
- Includes:
- CommonModel
- Defined in:
- lib/coupler/models/comparison.rb
Constant Summary
collapse
- OPERATORS =
{
"equals" => "=",
"does_not_equal" => "!=",
"greater_than" => ">",
"less_than" => "<",
}
- TYPES =
%w{field integer string}
Instance Method Summary
collapse
#after_destroy, #after_save, #before_create, #before_update, included, #save!, #touch!
Instance Method Details
#apply(dataset, which = nil) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/coupler/models/comparison.rb', line 57
def apply(dataset, which = nil)
lhs = lhs_type == 'field' ? lhs_value.name.to_sym : lhs_value
rhs = rhs_type == 'field' ? rhs_value.name.to_sym : rhs_value
if !blocking?
filters = []
tmp = dataset.opts
opts = {
:select => tmp[:select] ? tmp[:select].dup : [],
:order => tmp[:order] ? tmp[:order].dup : []
}
fields =
case which
when nil then lhs == rhs ? [lhs] : [lhs, rhs]
when 0 then [lhs]
when 1 then [rhs]
end
fields.each_with_index do |field, i|
index = i == 0 ? 0 : -1
if !opts[:select].include?(field)
opts[:select].push(field)
opts[:order].push(field)
opts[:modified] = true
filters.push(~{field => nil})
end
end
if opts.delete(:modified)
dataset = dataset.clone(opts).filter(*filters)
end
else
tmp_which = nil
if !which.nil?
if lhs_type == 'field' && rhs_type == 'field'
if lhs_which == rhs_which
tmp_which = lhs_which == 1 ? 0 : 1
else
raise "unsupported" end
elsif lhs_type == 'field'
tmp_which = lhs_which == 1 ? 0 : 1
elsif rhs_type == 'field'
tmp_which = rhs_which == 1 ? 0 : 1
else
end
end
if which.nil? || tmp_which.nil? || which == tmp_which
expr = Sequel::SQL::BooleanExpression.new(operator_symbol.to_sym, lhs, rhs)
dataset = dataset.filter(expr)
end
end
dataset
end
|
#blocking? ⇒ Boolean
118
119
120
|
# File 'lib/coupler/models/comparison.rb', line 118
def blocking?
lhs_type != 'field' || rhs_type != 'field' || lhs_which == rhs_which || operator != 'equals'
end
|
#cross_match? ⇒ Boolean
122
123
124
|
# File 'lib/coupler/models/comparison.rb', line 122
def cross_match?
lhs_type == 'field' && rhs_type == 'field' && lhs_which != rhs_which && lhs_value.id != rhs_value.id && lhs_value.resource_id == rhs_value.resource_id
end
|
#fields ⇒ Object
46
47
48
49
50
51
|
# File 'lib/coupler/models/comparison.rb', line 46
def fields
result = []
result << lhs_value if lhs_type == 'field'
result << rhs_value if rhs_type == 'field'
result
end
|
#lhs_label ⇒ Object
43
|
# File 'lib/coupler/models/comparison.rb', line 43
def lhs_label; lhs_rhs_label("lhs"); end
|
#lhs_rhs_label(name) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/coupler/models/comparison.rb', line 28
def lhs_rhs_label(name)
case self[:"#{name}_type"]
when "field"
field = lhs_rhs_value(name)
result = field.name
resource_name = field.resource.name
result += " (#{resource_name})"
else
lhs_rhs_value(name).inspect
end
end
|
#lhs_rhs_value(name) ⇒ Object
17
18
19
20
21
22
23
24
|
# File 'lib/coupler/models/comparison.rb', line 17
def lhs_rhs_value(name)
case self[:"#{name}_type"]
when "field"
Field[:id => send("raw_#{name}_value")]
else
send("raw_#{name}_value")
end
end
|
#lhs_value ⇒ Object
25
|
# File 'lib/coupler/models/comparison.rb', line 25
def lhs_value; lhs_rhs_value("lhs"); end
|
#operator_symbol ⇒ Object
53
54
55
|
# File 'lib/coupler/models/comparison.rb', line 53
def operator_symbol
OPERATORS[operator]
end
|
#rhs_label ⇒ Object
44
|
# File 'lib/coupler/models/comparison.rb', line 44
def rhs_label; lhs_rhs_label("rhs"); end
|
#rhs_value ⇒ Object
26
|
# File 'lib/coupler/models/comparison.rb', line 26
def rhs_value; lhs_rhs_value("rhs"); end
|