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

Fix up self.foo op= val #1

Merged
merged 1 commit into from
Mar 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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