From c344d97a55dbaf26240f55996420a9e0e3cee69c Mon Sep 17 00:00:00 2001 From: Mathias Gawlista Date: Tue, 19 Feb 2013 16:10:50 +0100 Subject: [PATCH] Provide mass assignment through initialize method. --- lib/sax-machine/sax_document.rb | 9 +++++++++ spec/sax-machine/sax_document_spec.rb | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/lib/sax-machine/sax_document.rb b/lib/sax-machine/sax_document.rb index 6ae240c..37e5c31 100644 --- a/lib/sax-machine/sax_document.rb +++ b/lib/sax-machine/sax_document.rb @@ -3,6 +3,7 @@ module SAXMachine def self.included(base) + base.send(:include, InstanceMethods) base.extend ClassMethods end @@ -13,6 +14,14 @@ def parse(xml_text, on_error = nil, on_warning = nil) self end + module InstanceMethods + def initialize(attributes = {}) + attributes.each do |name, value| + send("#{name}=", value) + end + end + end + module ClassMethods def inherited(subclass) diff --git a/spec/sax-machine/sax_document_spec.rb b/spec/sax-machine/sax_document_spec.rb index 7d92f01..8ef7968 100644 --- a/spec/sax-machine/sax_document_spec.rb +++ b/spec/sax-machine/sax_document_spec.rb @@ -10,6 +10,11 @@ end end + it "should provide mass assignment through initialize method" do + document = @klass.new(title: 'Title') + document.title.should == 'Title' + end + it "should provide an accessor" do document = @klass.new document.title = "Title"