Skip to content

Commit

Permalink
Merge pull request #3 from ActiveTriples/upgrade_to_at_0.7
Browse files Browse the repository at this point in the history
Upgrade to be compatible with ActiveTriples 0.5, 0.6, 0.7, and 0.8
  • Loading branch information
elrayle committed Jul 10, 2015
2 parents db884e3 + e86b25b commit 1fe90d1
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 37 deletions.
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ script: "bundle exec rspec spec"
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- 2.1.1
- 2.1.6
- 2.2.2
- ruby-head
- jruby-19mode
matrix:
allow_failures:
- rvm: jruby-19mode
- rvm: ruby-head
- rvm: jruby-19mode
3 changes: 1 addition & 2 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* Tom Johnson ([email protected])
* Trey Terrell
* E. Lynette Rayle ([email protected])
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
[![Coverage Status](https://coveralls.io/repos/ActiveTriples/active_triples-local_name/badge.png?branch=master)](https://coveralls.io/r/ActiveTriples/active_triples-local_name?branch=master)
[![Gem Version](https://badge.fury.io/rb/active_triples-local_name.svg)](http://badge.fury.io/rb/active_triples-local_name)

Provides utilities for working with local names under the [ActiveTriples](https://github.com/ActiveTriples/ActiveTriples)
framework. Includes a default implementation of a local name minter.
Provides utilities for working with local names under the [ActiveTriples](https://github.com/ActiveTriples/ActiveTriples) framework. Includes a default implementation of a local name minter.


## Installation
Expand Down Expand Up @@ -60,15 +59,11 @@ localname = ActiveTriples::LocalName::Minter.generate_local_name(

Parameter NOTES:
* for_class = DummyResourceWithBaseURI - resource class must have base_uri configured
* max_tries = 10 - If using default minter, it should easily find an available local name in 10 tries.
If your minter algorithm gets lots of clashes with existing URIs and max_tries is set high, you may
run into performance issues.
* max_tries = 10 - If using default minter, it should easily find an available local name in 10 tries.
If your minter algorithm gets lots of clashes with existing URIs and max_tries is set high, you may run into performance issues.
* minter_args (optional) = {:prefix=>'d'} - The default minter takes a single hash argument. You can
define minters that take no arguments, multiple arguments, or a multiple item hash argument.
* minter_block = nil - When minter_block is not passed in, the default minter algorithm, which produces
a UUID, will be used. Best practice is to start local names with an alpha character. UUIDs generate
with either an alpha or numeric as the first character. Passing in a prefix of 'd' forces the local
name to start with the character 'd'.
* minter_block = nil - When minter_block is not passed in, the default minter algorithm, which produces a UUID, will be used. Best practice is to start local names with an alpha character. UUIDs generate with either an alpha or numeric as the first character. Passing in a prefix of 'd' forces the local name to start with the character 'd'.


#### Example: Passing in a block as minter
Expand Down
8 changes: 5 additions & 3 deletions active_triples-local_name.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ Gem::Specification.new do |s|
s.license = "APACHE2"
s.required_ruby_version = '>= 1.9.3'

s.add_dependency('active-triples', '~> 0.5')
s.add_dependency('active-triples')

s.add_dependency('deprecation', '~> 0.1')
s.add_dependency('activesupport', '>= 3.0.0')
s.add_dependency('deprecation')
s.add_dependency('activesupport')

s.add_development_dependency('pry')
# s.add_development_dependency('pry-byebug') # uncomment for ruby >= 2.0
s.add_development_dependency('rdoc')
s.add_development_dependency('rspec')
s.add_development_dependency('coveralls')
Expand Down
15 changes: 2 additions & 13 deletions lib/active_triples/local_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,8 @@ module LocalName
autoload :Minter
end

def self.class_from_string(class_name, container_class=Kernel)
container_class = container_class.name if container_class.is_a? Module
container_parts = container_class.split('::')
(container_parts + class_name.split('::')).flatten.inject(Kernel) do |mod, class_name|
if mod == Kernel
Object.const_get(class_name)
elsif mod.const_defined? class_name.to_sym
mod.const_get(class_name)
else
container_parts.pop
class_from_string(class_name, container_parts.join('::'))
end
end
def self.post_ActiveTriples_0_7?
ActiveTriples.constants.include?(:RDFSource)
end
end
end
Expand Down
13 changes: 10 additions & 3 deletions lib/active_triples/local_name/minter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Minter
# Generate a random localname that combined with a class' base_uri does not already exist in
# registered triplestores.
#
# @param [Class] the class inheriting from <tt>ActiveTriples::Reource</tt> whose configuration
# @param [Class] the class inheriting from <tt>ActiveTriples::Reource</tt> (ActiveTriples < 0.7) or including <tt>ActiveTriples::RDFSource</tt> (ActiveTriples >= 0.7) whose configuration
# is used to generate the full URI for testing for uniqueness of the generated local name
# @param [Integer] the maximum number of attempts to make a unique local name
# @yieldparam the arguments to pass to the minter block (optional)
Expand All @@ -21,7 +21,7 @@ class Minter
# @return [String] the generated local name
#
# @raise [ArgumentError] if maximum allowed tries is less than 0
# @raise [ArgumentError] if for_class does not inherit from ActiveTriples::Resources
# @raise [ArgumentError] if for_class does not inherit from ActiveTriples::Resources (ActiveTriples < 0.7) or include ActiveTriples:RDFSource (ActiveTriples >= 0.7)
# @raise [ArgumentError] if minter_block is not a block (does not respond to call)
# @raise [Exception] if for_class does not have base_uri configured
# @raise [Exception] if an available local name is not found in the maximum allowed tries.
Expand All @@ -35,7 +35,14 @@ class Minter
def self.generate_local_name(for_class, max_tries=10, *minter_args, &minter_block)
raise ArgumentError, 'Argument max_tries must be >= 1 if passed in' if max_tries <= 0

raise ArgumentError, 'Argument for_class must inherit from ActiveTriples::Resource' unless for_class < ActiveTriples::Resource
if( ActiveTriples::LocalName.post_ActiveTriples_0_7? ) # Supports ActiveTriples >= 0.7
raise ArgumentError, 'Argument for_class must inherit from ActiveTriples::Resource or include module ActiveTriples::RDFSource' unless
( for_class < ActiveTriples::Resource || for_class.included_modules.include?(ActiveTriples::RDFSource) )
else # Supports ActiveTriples < 0.7
raise ArgumentError, 'Argument for_class must inherit from ActiveTriples::Resource' unless
( for_class < ActiveTriples::Resource )
end

raise 'Requires base_uri to be defined in for_class.' unless for_class.base_uri

raise ArgumentError, 'Invalid minter_block.' if minter_block && !minter_block.respond_to?(:call)
Expand Down
2 changes: 1 addition & 1 deletion lib/active_triples/local_name/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module ActiveTriples
module LocalName
VERSION = "0.5.0"
VERSION = "0.6.0"
end
end
40 changes: 38 additions & 2 deletions spec/active_triples/local_name/minter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,20 @@ class DummyNonResource
end

it "should raise error" do
if( ActiveTriples::LocalName.post_ActiveTriples_0_7? ) # for ActiveTriples >= 0.7
err_msg = 'Argument for_class must inherit from ActiveTriples::Resource or include module ActiveTriples::RDFSource'
else # for ActiveTriples < 0.7
err_msg = 'Argument for_class must inherit from ActiveTriples::Resource'
end
expect{ ActiveTriples::LocalName::Minter.generate_local_name(DummyNonResource) }.
to raise_error(ArgumentError, 'Argument for_class must inherit from ActiveTriples::Resource')
to raise_error(ArgumentError, err_msg)
end
end

it "should not raise error when inheriting from ActiveTriples::Resource" do
expect{ ActiveTriples::LocalName::Minter.generate_local_name(DummyResourceWithBaseURI) }.
not_to raise_error
end
end

context "and class doesn't have base_uri defined" do
it "should raise error" do
Expand Down Expand Up @@ -436,4 +445,31 @@ class DummyNonResource
end
end
end

describe "test validation when ActiveTriples >= 0.7" do
before do
if( ActiveTriples::LocalName.post_ActiveTriples_0_7? ) # Check that ActiveTriples >= 0.7
class DummyResourceInclude
include ActiveTriples::RDFSource
configure :base_uri => "http://example.org",
:type => RDF::URI('http://example.org/SomeClass')
property :title, :predicate => RDF::DC.title
end
end
end
after do
if( ActiveTriples::LocalName.post_ActiveTriples_0_7? ) # Check that ActiveTriples >= 0.7
Object.send(:remove_const, "DummyResourceInclude") if Object
end
end

it "should not raise error when including from ActiveTriples::RDFSource" do
unless( ActiveTriples::LocalName.post_ActiveTriples_0_7? ) # Supports ActiveTriples >= 0.7
skip ('Cannot test include ActiveTriples::RDFSource when ActiveTriples < 0.7 installed')
else
expect{ ActiveTriples::LocalName::Minter.generate_local_name(DummyResourceInclude) }.
not_to raise_error
end
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

require 'active_triples/local_name'
require 'active_triples'

require 'pry' unless ENV['CI']

Dir['./spec/support/**/*.rb'].each { |f| require f }

Expand Down

0 comments on commit 1fe90d1

Please sign in to comment.