Skip to content

Commit

Permalink
Release 3.1.0:
Browse files Browse the repository at this point in the history
* Add support of activemodel 5.2
  • Loading branch information
abrisse committed Jul 12, 2018
2 parents 25efccc + 87b2ad7 commit 1aa855f
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 36 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.0
3.1.0
28 changes: 15 additions & 13 deletions lib/spira/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class Base
# @return [RDF::URI]
attr_reader :subject

attr_accessor :attributes

class << self
attr_reader :reflections, :properties

Expand Down Expand Up @@ -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

##
Expand Down Expand Up @@ -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

##
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/base_uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/non_model_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'))}
Expand Down
6 changes: 3 additions & 3 deletions spec/psych_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 7 additions & 7 deletions spec/rdf_types_spec.rb
Original file line number Diff line number Diff line change
@@ -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/')
Expand All @@ -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
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions spec/relations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1aa855f

Please sign in to comment.