Class: Wherelizer
- Inherits:
-
Object
show all
- Defined in:
- lib/wherelizer.rb
Defined Under Namespace
Classes: ConvertException
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Wherelizer.
14
15
16
17
|
# File 'lib/wherelizer.rb', line 14
def initialize(orig)
@orig = orig
@ruby2ruby = Ruby2Ruby.new
end
|
Instance Attribute Details
#final ⇒ Object
Returns the value of attribute final.
12
13
14
|
# File 'lib/wherelizer.rb', line 12
def final
@final
end
|
#orig ⇒ Object
Returns the value of attribute orig.
12
13
14
|
# File 'lib/wherelizer.rb', line 12
def orig
@orig
end
|
#parsed ⇒ Object
Returns the value of attribute parsed.
12
13
14
|
# File 'lib/wherelizer.rb', line 12
def parsed
@parsed
end
|
#ruby2ruby ⇒ Object
Returns the value of attribute ruby2ruby.
12
13
14
|
# File 'lib/wherelizer.rb', line 12
def ruby2ruby
@ruby2ruby
end
|
Instance Method Details
#convert ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/wherelizer.rb', line 19
def convert
@parsed = RubyParser.new.parse(orig)
assignment = handle_assignment
receiver = ruby2ruby.process(parsed.)
method_name = handle_method_name
options_hash = parsed.to_hash
where = parse_conditions(options_hash.delete(:conditions)) if options_hash[:conditions]
others = options_hash.map{|key, value| parse_basic_param(key, value)}.join
"#{assignment}#{receiver}#{where}#{others}#{method_name}"
rescue => e
raise ConvertException.new("Unable to convert: #{e.message}")
end
|
#final_assignment(assignment_target) ⇒ Object
59
60
61
|
# File 'lib/wherelizer.rb', line 59
def final_assignment(assignment_target)
"#{assignment_target} = "
end
|
#final_method_name(method_name) ⇒ Object
55
56
57
|
# File 'lib/wherelizer.rb', line 55
def final_method_name(method_name)
".#{method_name}" unless method_name == :all
end
|
#handle_assignment ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/wherelizer.rb', line 35
def handle_assignment
if parsed.is_type? [:lasgn, :iasgn]
assignment = final_assignment(parsed.)
@parsed = @parsed[2]
end
assignment
end
|
#handle_method_name ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/wherelizer.rb', line 43
def handle_method_name
method_name = parsed.
if method_name == :find
first_param = parsed[3]
@parsed = @parsed[4]
final_method_name(first_param.)
else
@parsed = @parsed[3]
final_method_name(method_name)
end
end
|
#parse_basic_param(name, value) ⇒ Object
85
86
87
88
|
# File 'lib/wherelizer.rb', line 85
def parse_basic_param name, value
method_name = name.to_s == 'include' ? 'includes' : name.to_s
".#{method_name}(#{ruby2ruby.process(value)})"
end
|
#parse_conditions(node) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/wherelizer.rb', line 63
def parse_conditions node
where = ''
if node.is_type? :hash
node.to_hash(false).each do |key, val|
if key.is_type? :lit
where += ".where(#{key.}: #{ruby2ruby.process(val)})"
else
where += ".where(#{ruby2ruby.process(key)} => #{ruby2ruby.process(val)})"
end
end
elsif node.is_type? :array
where = ".where(" + node.to_array.map{ |el| ruby2ruby.process(el) }.join(', ') + ")"
elsif node.is_type? :str
where = ".where(#{ruby2ruby.process(node)})"
else
raise "Unexpected type for conditions parameter. Expecting hash, array, or string."
end
where
end
|