Skip to content

Commit

Permalink
Blocks improvements: change the context of blocks passed with configu…
Browse files Browse the repository at this point in the history
…ration

to be instance instead of class.

Issue #63
  • Loading branch information
krasnoukhov committed Jan 9, 2015
1 parent 96297e6 commit 3105f90
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/sax-machine/sax_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def elements(name, options = {}, &block)
else
if block_given?
define_method("add_#{real_name}") do |value|
send(real_name).send(:<<, yield(value))
send(real_name).send(:<<, instance_exec(value, &block))
end
else
define_method("add_#{real_name}") do |value|
Expand Down Expand Up @@ -126,7 +126,7 @@ def create_attr(real_name, &block)
if !method_defined?("#{real_name}=")
if block_given?
define_method("#{real_name}=") do |value|
instance_variable_set("@#{real_name}", (yield value))
instance_variable_set("@#{real_name}", instance_exec(value, &block))
end
else
attr_writer(real_name)
Expand Down
9 changes: 9 additions & 0 deletions spec/sax-machine/sax_document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ class BlockParser
"#{title}!!!"
end

element :scope do |scope|
"#{title} #{scope}"
end

attribute :id do |id|
id.to_i
end
Expand All @@ -251,6 +255,11 @@ class BlockParser
end
end

it "has instance as a block context" do
document = BlockParser.parse("<title>SAX</title><scope>something</scope>")
expect(document.scope).to eq("SAX!!! something")
end

it "uses block for element" do
document = BlockParser.parse("<title>SAX</title>")
expect(document.title).to eq("SAX!!!")
Expand Down

0 comments on commit 3105f90

Please sign in to comment.