| Module | Condition |
| In: |
lib/condition.rb
|
モデルのクラスに include される。 インスタンスに対する respond_to? で正しいことが仮定されるメッセージ:
条件を文字列として返す。 指定されていない場合には false を返す。
# File lib/condition.rb, line 15
15: def extract
16: return false unless condition_pattern
17: return false if condition_pattern == "none"
18: case condition_pattern
19: when "eq"
20: return "#{table_name}.#{column_name} IN (#{inline_values.join(',')})" if reference
21: if c = option_category
22: x = inline_values.map do |v|
23: "EXISTS (SELECT 1 FROM input_options WHERE category = '#{c}' AND name_po = #{v} AND value = #{table_name}.#{column_name})"
24: end
25: else
26: x = inline_values.map {|v| "#{table_name}.#{column_name} = #{v}"}
27: end
28: y = x.join(" OR ")
29: "(#{y})"
30: when "neq"
31: return "#{table_name}.#{column_name} NOT IN (#{inline_values.join(',')})" if reference
32: if c = option_category
33: x = inline_values.map do |v|
34: "EXISTS (SELECT 1 FROM input_options WHERE category = '#{c}' AND name_po <> #{v} AND value = #{table_name}.#{column_name})"
35: end
36: else
37: x = inline_values.map {|v| "#{table_name}.#{column_name} <> #{v}"}
38: end
39: y = x.join(" AND ")
40: "(#{y})"
41: when "head", "tail", "include"
42: return "#{table_name}.#{column_name} IN (#{inline_values.join(',')})" if reference
43: if c = option_category
44: x = inline_values.map do |v|
45: "EXISTS (SELECT 1 FROM input_options WHERE category = '#{c}' AND name_po = #{v} AND value = #{table_name}.#{column_name})"
46: end
47: y = x.join(" OR ")
48: return "(#{y})"
49: end
50: "(#{table_name}.#{column_name} LIKE #{inline_values.first})"
51: when "exclude"
52: return "#{table_name}.#{column_name} NOT IN (#{inline_values.join(',')})" if reference
53: if c = option_category
54: x = inline_values.map do |v|
55: "EXISTS (SELECT 1 FROM input_options WHERE category = '#{c}' AND name_po <> #{v} AND value = #{table_name}.#{column_name})"
56: end
57: y = x.join(" AND ")
58: return "(#{y})"
59: end
60: "(#{table_name}.#{column_name} IS NULL OR #{table_name}.#{column_name} NOT LIKE #{inline_values.first})"
61: when "ge"
62: "(#{table_name}.#{column_name} >= #{inline_values.first})"
63: when "le"
64: "(#{table_name}.#{column_name} <= #{inline_values.first})"
65: when "gt"
66: "(#{table_name}.#{column_name} > #{inline_values.first})"
67: when "lt"
68: "(#{table_name}.#{column_name} < #{inline_values.first})"
69: when "null"
70: "(#{table_name}.#{column_name} IS NULL)"
71: when "not-null"
72: "(#{table_name}.#{column_name} IS NOT NULL)"
73: else
74: raise ArgumentError, "unknown condition_pattern: #{condition_pattern}"
75: end
76: end
# File lib/condition.rb, line 78
78: def inline_values
79: case condition_pattern
80: when "eq", "neq"
81: raw = (condition_value.empty?) ? [""] : condition_value.split(" ")
82: if r = reference
83: return raw.map {|name| r.find(:all).select {|x| x.name == name}.map(&:id)}.flatten
84: elsif c = option_category
85: return raw.map {|name| InputOption.name_po_with_category_and_name(c, name)}
86: elsif checkbox?
87: ons = %w|ON On on|
88: offs = %w|OFF Off off|
89: on, off = input_parameter.split(",")
90: ons.unshift(on) if on
91: offs.unshift(off) if off
92: return raw.map do |x|
93: if ons.include?(x)
94: self.class.connection.quoted_true
95: elsif offs.include?(x)
96: self.class.connection.quoted_false
97: else
98: quote_value x
99: end
100: end
101: end
102: when "head"
103: if r = reference
104: return r.find(:all).select {|x| x.name.index(condition_value) == 0}.map(&:id)
105: elsif c = option_category
106: return InputOption.name_pos_with_category_and_head(c, condition_value)
107: end
108: raw = [condition_value + "%"]
109: when "tail"
110: if r = reference
111: return r.find(:all).select {|x| x.name.rindex(condition_value) == x.name.length - condition_value.length}.map(&:id)
112: elsif c = option_category
113: return InputOption.name_pos_with_category_and_tail(c, condition_value)
114: end
115: raw = ["%" + condition_value]
116: when "include", "exclude"
117: if r = reference
118: return r.find(:all).select {|x| x.name.include?(condition_value)}.map(&:id)
119: elsif c = option_category
120: return InputOption.name_pos_with_category_and_include(c, condition_value)
121: end
122: raw = ["%" + condition_value + "%"]
123: when "ge", "le", "gt", "lt"
124: raw = [condition_value]
125: when "null", "not-null"
126: raw = []
127: end
128: return raw.map {|v| quote_value v}
129: end