Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/spira/resource/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 44 additions & 0 deletions spec/psych.spec
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down