Permalink
Browse files

Merge branch 'github.com:RipTheJacker/sax-machine'

Conflicts:
	Rakefile
	lib/sax-machine/sax_document.rb
	spec/sax-machine/sax_document_spec.rb
	spec/spec_helper.rb
  • Loading branch information...
2 parents d54ff86 + b417d52 commit 817b8476ecdb8c68554d15bd7bae601b94072817 Philip Mcmahon committed Nov 4, 2010
Showing with 43 additions and 15 deletions.
  1. +5 −5 lib/sax-machine/sax_document.rb
  2. +38 −10 spec/sax-machine/sax_document_spec.rb
@@ -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
@@ -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
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/../spec_helper'
+require 'spec_helper'
describe "SAXMachine" do
describe "element" do
@@ -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
@@ -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

0 comments on commit 817b847

Please sign in to comment.