Class: Card::Query::ValueSpec
- Inherits:
-
Spec
show all
- Defined in:
- lib/card/query/value_spec.rb
Instance Attribute Summary
Attributes inherited from Spec
#spec
Instance Method Summary
collapse
Methods inherited from Spec
#cast_type, #match_prep, #quote, #safe_sql
Constructor Details
#initialize(spec, cardspec) ⇒ ValueSpec
Returns a new instance of ValueSpec.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/card/query/value_spec.rb', line 3
def initialize spec, cardspec
@cardspec = cardspec
@spec = case spec
when ValueSpec; spec.instance_variable_get('@spec') when Array; spec
when String; ['=', spec]
when Integer; ['=', spec]
else raise("Invalid Condition Spec #{spec.inspect}")
end
@spec[0] = @spec[0].to_s
if target = OPERATORS[@spec[0]]
@spec[0] = target
end
raise("Invalid Operator #{@spec[0]}") unless OPERATORS.has_key?(@spec[0])
if @spec[0]=='in' and !@spec[1].is_a?(CardSpec) and !@spec[1].is_a?(RefSpec)
@spec = [@spec[0], @spec[1..-1]]
end
end
|
Instance Method Details
30
31
32
|
# File 'lib/card/query/value_spec.rb', line 30
def op
@spec[0]
end
|
#sqlize(v) ⇒ Object
34
35
36
37
38
39
40
|
# File 'lib/card/query/value_spec.rb', line 34
def sqlize(v)
case v
when CardSpec, RefSpec, SqlCond; v.to_sql
when Array; "(" + v.flatten.collect {|x| sqlize(x)}.join(',') + ")"
else quote(v.to_s)
end
end
|
#to_sql(field) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/card/query/value_spec.rb', line 42
def to_sql field
op,v = @spec
v=@cardspec.selfname if v=='_self'
table = @cardspec.table_alias
field, v = case field
when "cond"; return "(#{sqlize(v)})"
when "name"; ["#{table}.key", [v].flatten.map(&:to_name).map(&:key)]
when "content"; ["#{table}.db_content", v]
else; ["#{table}.#{safe_sql(field)}", v]
end
v = v[0] if Array===v && v.length==1 && op != 'in'
if op=='~'
cxn, v = match_prep(v,@cardspec)
%{#{field} #{cxn.match(sqlize(v))}}
else
"#{field} #{op} #{sqlize(v)}"
end
end
|