Created
March 7, 2012 23:33
-
-
Save anithri/1997252 to your computer and use it in GitHub Desktop.
operations dispatch?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def clause(op, field, data) | |
out = [] | |
out << ops_table[op][:field_proc].call(field) | |
out << ops_table[op][:data_proc].call(data) | |
out | |
end | |
#later | |
model.where(*clause(op, field, data)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"eq" => { | |
:name => "equal", | |
:field_proc => lambda{|field| "#{field} = ?"}, | |
:data_proc => lambda{|data| data} | |
}, | |
"cn" => { | |
:name => "contains", | |
:field_proc => lambda{|field| "#{field} LIKE ?"}, | |
:data_proc => lambda{|data| "%#{data}%} | |
} | |
"ne" => { | |
:name => "not equal", | |
:field_proc => nil, | |
:data_proc => nil | |
}, | |
"lt" => { | |
:name => "less", | |
:field_proc => nil, | |
:data_proc => nil | |
}, | |
"le" => { | |
:name => "less or equal", | |
:field_proc => nil, | |
:data_proc => nil | |
}, | |
"gt" => { | |
:name => "greater", | |
:field_proc => nil, | |
:data_proc => nil | |
}, | |
"ge" => { | |
:name => "greater or equal", | |
:field_proc => nil, | |
:data_proc => nil | |
}, | |
"bw" => { | |
:name => "begins with", | |
:field_proc => nil, | |
:data_proc => nil | |
}, | |
"bn" => { | |
:name => "does not begin with", | |
:field_proc => nil, | |
:data_proc => nil | |
}, | |
"in" => { | |
:name => "is in", | |
:field_proc => nil, | |
:data_proc => nil | |
}, | |
"ni" => { | |
:name => "is not in", | |
:field_proc => nil, | |
:data_proc => nil | |
}, | |
"ew" => { | |
:name => "ends with", | |
:field_proc => nil, | |
:data_proc => nil | |
}, | |
"en" => { | |
:name => "does not end with", | |
:field_proc => nil, | |
:data_proc => nil | |
}, | |
"cn" => { | |
:name => "contains", | |
:field_proc => nil, | |
:data_proc => nil | |
}, | |
"nc" => { | |
:name => "does not contain", | |
:field_proc => nil, | |
:data_proc => nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment