From 0cac69bc4a9d2d58afac32e426675e7e909d5c3a Mon Sep 17 00:00:00 2001 From: Gregg Kellogg Date: Sat, 10 Sep 2011 16:10:16 -0700 Subject: [PATCH] Added support for YAML serialization/de-serialization of Spira::Base instances using Psych. --- lib/spira/resource/instance_methods.rb | 26 +++++++++++++++ spec/psych.spec | 44 ++++++++++++++++++++++++++ spec/spec_helper.rb | 9 ++++++ 3 files changed, 79 insertions(+) create mode 100644 spec/psych.spec diff --git a/lib/spira/resource/instance_methods.rb b/lib/spira/resource/instance_methods.rb index f6b37b1..a11cc9e 100644 --- a/lib/spira/resource/instance_methods.rb +++ b/lib/spira/resource/instance_methods.rb @@ -563,5 +563,31 @@ def rename!(new_subject) include Spira::Resource::Validations end + + ## + # Support for Psych (YAML) custom serializer. + # + # This causes the subject and all attributes to be saved to a YAML or JSON serialization + # in such a way that they can be restored in the future. + # + # @param [Psych::Coder] coder + def encode_with(coder) + coder["subject"] = subject + attributes.each {|p,v| coder[p.to_s] = v if v} + end + + ## + # Support for Psych (YAML) custom de-serializer. + # + # Updates a previously allocated Spira::Base instance to that of a previously + # serialized instance. + # + # @param [Psych::Coder] coder + def init_with(coder) + self.instance_variable_set(:"@subject", coder["subject"]) + self.reload + attributes.each {|p,v| self.attribute_set(p, coder.map[p.to_s])} + end + end end diff --git a/spec/psych.spec b/spec/psych.spec new file mode 100644 index 0000000..2c50713 --- /dev/null +++ b/spec/psych.spec @@ -0,0 +1,44 @@ +require File.dirname(File.expand_path(__FILE__)) + '/spec_helper' + +# Tests serializing and de-serializing with Psych (YAML). + +describe Spira, :ruby => "1.9" do + require 'psych' + + before :all do + class ::Person + include Spira::Resource + base_uri "http://example.org/example/people" + property :name, :predicate => RDFS.label + property :age, :predicate => FOAF.age, :type => Integer + end + + class Employee + include Spira::Resource + property :name, :predicate => RDFS.label + property :age, :predicate => FOAF.age, :type => Integer + end + + require 'rdf/ntriples' + @person_repository = RDF::Repository.load(fixture('bob.nt')) + Spira.add_repository(:default, @person_repository) + end + + before :each do + @person_repository = RDF::Repository.load(fixture('bob.nt')) + Spira.add_repository(:default, @person_repository) + + @person = Person.for(RDF::URI.new('http://example.org/newperson')) + end + + it "serializes to YAML" do + yaml = Psych.dump(@person) + yaml.should be_a(String) + end + + it "de-serializes from YAML" do + yaml = Psych.dump(@person) + person2 = Psych.load(yaml) + person2.should == @person + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 44a7943..f5029a8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,11 +3,20 @@ #$:.unshift(File.join(File.dirname(__FILE__),'..','..','rdf-spec','lib')) #$:.unshift(File.join(File.dirname(__FILE__),'..','..','rdf','lib')) #$:.unshift(File.join(File.dirname(__FILE__),'..','..','rdf-isomorphic','lib')) +require "bundler/setup" rescue nil require 'spira' require 'rdf/spec/enumerable' require 'rdf/spec' require 'rdf/isomorphic' +RSpec.configure do |config| + config.filter_run :focus => true + config.run_all_when_everything_filtered = true + config.exclusion_filter = { + :ruby => lambda { |version| RUBY_VERSION.to_s !~ /^#{version}/}, + } +end + def fixture(filename) File.join(File.dirname(__FILE__),'fixtures',filename) end