diff --git a/VERSION b/VERSION index 4a36342..fd2a018 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.0.0 +3.1.0 diff --git a/lib/spira/base.rb b/lib/spira/base.rb index 63d6fbf..6bf3e5a 100644 --- a/lib/spira/base.rb +++ b/lib/spira/base.rb @@ -32,8 +32,6 @@ class Base # @return [RDF::URI] attr_reader :subject - attr_accessor :attributes - class << self attr_reader :reflections, :properties @@ -119,21 +117,26 @@ def id # @see RDF::Node#as def initialize(props = {}, options = {}) @subject = props.delete(:_subject) || RDF::Node.new + @attrs = {} - @attributes = {} reload props yield self if block_given? end + # Returns the attributes + def attributes + @attrs + end + # Freeze the attributes hash such that associations are still accessible, even on destroyed records. def freeze - @attributes.freeze; self + @attrs.freeze; self end # Returns +true+ if the attributes hash has been frozen. def frozen? - @attributes.frozen? + @attrs.frozen? end ## @@ -257,7 +260,7 @@ def to_node # @param [RDF::Resource] new_subject # @return [Spira::Base] copy def copy(new_subject) - self.class.new(attributes.merge(:_subject => new_subject)) + self.class.new(@attrs.merge(:_subject => new_subject)) end ## @@ -284,20 +287,19 @@ def assign_attributes(attrs) private def reset_changes - @previously_changed = changes - @changed_attributes.clear + clear_changes_information end def write_attribute(name, value) name = name.to_s if self.class.properties[name] - if attributes[name].is_a?(Promise) - changed_attributes[name] = attributes[name] unless changed_attributes.include?(name) - attributes[name] = value + if @attrs[name].is_a?(Promise) + changed_attributes[name] = @attrs[name] unless changed_attributes.include?(name) + @attrs[name] = value else if value != read_attribute(name) attribute_will_change!(name) - attributes[name] = value + @attrs[name] = value end end else @@ -309,7 +311,7 @@ def write_attribute(name, value) # Get the current value for the given attribute # def read_attribute(name) - value = attributes[name.to_s] + value = @attrs[name.to_s] refl = self.class.reflections[name] if refl && !value diff --git a/spec/base_uri_spec.rb b/spec/base_uri_spec.rb index 04cda57..f873220 100644 --- a/spec/base_uri_spec.rb +++ b/spec/base_uri_spec.rb @@ -74,7 +74,7 @@ class ::HashBaseURITest < Spira::Base context "classes without a base URI" do before :all do class ::NoBaseURITest < Spira::Base - property :name, :predicate => RDFS.label + property :name, :predicate => RDF::RDFS.label end end diff --git a/spec/non_model_data_spec.rb b/spec/non_model_data_spec.rb index e761853..96fd852 100644 --- a/spec/non_model_data_spec.rb +++ b/spec/non_model_data_spec.rb @@ -7,7 +7,7 @@ class ::ExtraDataTest < Spira::Base configure :base_uri => "http://example.org/example" property :property, :predicate => RDF::Vocab::FOAF.age, :type => Integer - has_many :list, :predicate => RDFS.label + has_many :list, :predicate => RDF::RDFS.label end end let(:extra_repo) {RDF::Repository.load(fixture('non_model_data.nt'))} diff --git a/spec/psych_spec.rb b/spec/psych_spec.rb index ebb6ba5..7bfbb9a 100644 --- a/spec/psych_spec.rb +++ b/spec/psych_spec.rb @@ -8,12 +8,12 @@ before :all do class PsychPerson < Spira::Base configure :base_uri => "http://example.org/example/people" - property :name, :predicate => RDFS.label + property :name, :predicate => RDF::RDFS.label property :age, :predicate => RDF::Vocab::FOAF.age, :type => Integer end - + class PsychEmployee < Spira::Base - property :name, :predicate => RDFS.label + property :name, :predicate => RDF::RDFS.label property :age, :predicate => RDF::Vocab::FOAF.age, :type => Integer end diff --git a/spec/rdf_types_spec.rb b/spec/rdf_types_spec.rb index f7e37d3..93ffec1 100644 --- a/spec/rdf_types_spec.rb +++ b/spec/rdf_types_spec.rb @@ -1,5 +1,5 @@ require "spec_helper" -# These classes are to test finding based on rdfs.type +# These classes are to test finding based on RDF::RDFS.type class Cars < RDF::Vocabulary('http://example.org/cars/') @@ -19,16 +19,16 @@ class Cars < RDF::Vocabulary('http://example.org/cars/') before :all do class ::Car < Spira::Base type Cars.car - property :name, :predicate => RDFS.label + property :name, :predicate => RDF::RDFS.label end - + class ::Van < Spira::Base type Cars.van - property :name, :predicate => RDFS.label + property :name, :predicate => RDF::RDFS.label end - + class ::Wagon < Spira::Base - property :name, :predicate => RDFS.label + property :name, :predicate => RDF::RDFS.label end class ::MultiCar < Spira::Base @@ -51,7 +51,7 @@ class ::XYZ < Spira::Base it "should provide a class method which returns the type" do expect(Car).to respond_to :type end - + it "should return the correct type" do expect(Car.type).to eql Cars.car end diff --git a/spec/relations_spec.rb b/spec/relations_spec.rb index 5480f59..4d1e9b2 100644 --- a/spec/relations_spec.rb +++ b/spec/relations_spec.rb @@ -3,20 +3,20 @@ describe "Spira resources" do before :all do - + class ::CDs < RDF::Vocabulary('http://example.org/') property :artist property :cds property :artists property :has_cd end - + class ::CD < Spira::Base configure :base_uri => CDs.cds property :name, :predicate => RDF::Vocab::DC.title, :type => String property :artist, :predicate => CDs.artist, :type => 'Artist' end - + class ::Artist < Spira::Base configure :base_uri => CDs.artists property :name, :predicate => RDF::Vocab::DC.title, :type => String @@ -45,7 +45,7 @@ class ::RootNSTest < Spira::Base test.name = RootNSTest.new test.name.save! test.save! - + test = subject.as(RootNSTest) expect(test.name).to be_a RootNSTest end @@ -86,7 +86,7 @@ module ::NSTestA class A < Spira::Base end end - end + end it "should find a class based on the string version of the name" do test = NSTest::Z.new @@ -129,7 +129,7 @@ class A < Spira::Base context "with a one-to-many relationship" do subject(:artist) {Artist.for "nirvana"} subject(:cd) {CD.for 'nevermind'} - + before :each do Spira.repository = RDF::Repository.load(fixture('relations.nt')) @cd = CD.for 'nevermind' @@ -188,7 +188,7 @@ class A < Spira::Base before {Spira.repository = invalid_repo} context "when accessing a field named for a non-existant class" do - + before do class ::RelationsTestA < Spira::Base configure :base_uri => CDs.cds @@ -218,7 +218,7 @@ class ::RelationsTestB < Spira::Base property :invalid, :predicate => RDF::Vocab::DC.title, :type => 'Object' end end - + it "should should raise a TypeError when saving an object with the invalid property" do expect { RelationsTestB.new(:invalid => Object.new).save! }.to raise_error TypeError end diff --git a/spec/update_spec.rb b/spec/update_spec.rb index 54bbae5..c6294c4 100644 --- a/spec/update_spec.rb +++ b/spec/update_spec.rb @@ -7,7 +7,7 @@ before :all do class ::UpdateTest < Spira::Base configure :base_uri => "http://example.org/example/people" - property :name, :predicate => RDFS.label + property :name, :predicate => RDF::RDFS.label property :age, :predicate => RDF::Vocab::FOAF.age, :type => Integer end end diff --git a/spec/validations_spec.rb b/spec/validations_spec.rb index 82fd78c..0ed4974 100644 --- a/spec/validations_spec.rb +++ b/spec/validations_spec.rb @@ -8,7 +8,7 @@ class ::Bank < Spira::Base configure :default_vocabulary => RDF::URI.new('http://example.org/banks/vocab') - property :title, :predicate => RDFS.label + property :title, :predicate => RDF::RDFS.label property :balance, :type => Integer validate :validate_bank