Skip to content

Commit

Permalink
Merge branch 'github.com:RipTheJacker/sax-machine'
Browse files Browse the repository at this point in the history
Conflicts:
	Rakefile
	lib/sax-machine/sax_document.rb
	spec/sax-machine/sax_document_spec.rb
	spec/spec_helper.rb
  • Loading branch information
Philip Mcmahon committed Nov 4, 2010
2 parents d54ff86 + b417d52 commit 817b847
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
10 changes: 5 additions & 5 deletions lib/sax-machine/sax_document.rb
Expand Up @@ -25,9 +25,9 @@ def element(name, options = {})

# we only want to insert the getter and setter if they haven't defined it from elsewhere.
# this is how we allow custom parsing behavior. So you could define the setter
# and have it parse the string into a date or whatever.
attr_reader options[:as] unless instance_methods.include?(options[:as].to_s)
attr_writer options[:as] unless instance_methods.include?("#{options[:as]}=")
# and have it parse the string into a date or whatever.
attr_reader options[:as] unless instance_methods.detect{|im| im.to_s == options[:as].to_s }
attr_writer options[:as] unless instance_methods.detect{|im| im.to_s == "#{options[:as]}="}
end

def columns
Expand Down Expand Up @@ -63,15 +63,15 @@ def add_#{options[:as]}(value)
sax_config.add_top_level_element(name, options.merge(:collection => true))
end

if !instance_methods.include?(options[:as].to_s)
if !instance_methods.detect{|im| im.to_s == options[:as].to_s }
class_eval <<-SRC
def #{options[:as]}
@#{options[:as]} ||= []
end
SRC
end

attr_writer options[:as] unless instance_methods.include?("#{options[:as]}=")
attr_writer options[:as] unless instance_methods.detect{|im| im.to_s == "#{options[:as]}="}
end

def sax_config
Expand Down
48 changes: 38 additions & 10 deletions spec/sax-machine/sax_document_spec.rb
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/../spec_helper'
require 'spec_helper'

describe "SAXMachine" do
describe "element" do
Expand All @@ -19,19 +19,20 @@
it "should allow introspection of the elements" do
@klass.column_names.should =~ [:title]
end

it "should not overwrite the setter if there is already one present" do
@klass = Class.new do

it "should not overwrite the getter is there is already one present" do
@klass = Class.new do
def title
"#{@title} ***"
end
include SAXMachine
element :title
def title=(val)
@title = "#{val} **"
end
end
document = @klass.new
end
document = @klass.new
document.title = "Title"
document.title.should == "Title **"
document.title.should == "Title ***"
end

describe "the class attribute" do
before(:each) do
@klass = Class.new do
Expand Down Expand Up @@ -105,6 +106,33 @@ def title=(val)
document.name.should == "Paul"
document.title.should == "My Title"
end

it "should not overwrite the getter is there is already one present" do
@klass = Class.new do
def items
[]
end
include SAXMachine
elements :items
end
document = @klass.new
document.items = [1,2,3,4]
document.items.should == []
end

it "should not overwrite the setter if there is already one present" do
@klass = Class.new do
def items=(val)
@items = [1, *val]
end
include SAXMachine
elements :items
end
document = @klass.new
document.items = [2,3]
document.items.should == [1,2,3]
end

end

describe "when using options for parsing elements" do
Expand Down

0 comments on commit 817b847

Please sign in to comment.