Class: Coupler::Models::Comparison

Inherits:
Sequel::Model
  • Object
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

Methods included from CommonModel

#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

      # NOTE: This assumes that the presence of a field name in the
      # select array implies that the filters for it are already in
      # place.  I don't want to go searching through Sequel's filter
      # expressions to find out what's in there.
      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
    # Figure out which side to apply this comparison to.
    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" # FIXME
        end
      elsif lhs_type == 'field'
        tmp_which = lhs_which == 1 ? 0 : 1
      elsif rhs_type == 'field'
        tmp_which = rhs_which == 1 ? 0 : 1
      else
        # Doesn't matter.  Apply to either side.
      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

Returns:

  • (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

Returns:

  • (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

#fieldsObject



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_labelObject



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
    # FIXME: revisit this!
    #if self[:"#{name}_which"]
      #resource_name += %{<span class="sup">#{self[:"#{name}_which"]}</span>}
    #end
    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_valueObject



25
# File 'lib/coupler/models/comparison.rb', line 25

def lhs_value; lhs_rhs_value("lhs"); end

#operator_symbolObject



53
54
55
# File 'lib/coupler/models/comparison.rb', line 53

def operator_symbol
  OPERATORS[operator]
end

#rhs_labelObject



44
# File 'lib/coupler/models/comparison.rb', line 44

def rhs_label; lhs_rhs_label("rhs"); end

#rhs_valueObject



26
# File 'lib/coupler/models/comparison.rb', line 26

def rhs_value; lhs_rhs_value("rhs"); end