Skip to content
This repository has been archived by the owner on Nov 28, 2018. It is now read-only.

Commit

Permalink
Fix up self.foo op= val
Browse files Browse the repository at this point in the history
  • Loading branch information
nevir committed Mar 3, 2013
1 parent 87dbced commit 06d2696
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# v0.2.19 2013-03-1

* [change] Bump dependencies
* [fixed] Fix op assign 2 operators (self.foo ||= bar, etc)

[Compare v0.2.18..v0.2.19](https://github.com/mbj/to_source/compare/v0.2.18...v0.2.19)

Expand Down
18 changes: 17 additions & 1 deletion lib/to_source/emitter/op_assign2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,26 @@ def dispatch
visit(util.receiver)
emit('.')
emit(util.name)
emit(' |= ')
emit(" #{operator} ")
visit(util.value)
end

MAPPING = {
:or => :'||',
:and => :'&&'
}.freeze

# Return operator
#
# @return [String]
#
# @api private
#
def operator
op = node.op
"#{MAPPING.fetch(op, op)}="
end

end
end
end
13 changes: 8 additions & 5 deletions spec/unit/to_source/class_methods/run_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,15 @@ def foo
end
end

context 'conditional element assignment' do
assert_source 'foo[key] ||= bar'
end

context 'attribute assignment on merge' do
assert_source 'self.foo |= bar'
%w(|= ||= &= &&= += -= *= /= **= %=).each do |op|
context "conditional attribute #{op} assignment" do
assert_source "self.foo #{op} bar"
end

context "conditional element #{op} assignment" do
assert_source "foo[key] #{op} bar"
end
end


Expand Down

0 comments on commit 06d2696

Please sign in to comment.