diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..6e64999 --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1 @@ +service_name: travis-ci \ No newline at end of file diff --git a/.gitignore b/.gitignore index c1b86eb..f2c22f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,27 @@ -.bundle/ log/*.log -pkg/ -Gemfile.lock -tmp/* .sass-cache +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp +*.bundle +*.so +*.o +*.a +mkmf.log .idea +.ruby-gemset +.ruby-version \ No newline at end of file diff --git a/Gemfile b/Gemfile index 345e3aa..f68a903 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,3 @@ source "https://rubygems.org" gemspec -# GETTING FROM GEMFILE UNTIL ActiveTriples master CODE IS RELEASED (>0.4.0), THEN MOVE THIS BACK TO *.gemspec FILE -# Use active-triple for handling of triple persistence -gem 'active-triples', :git => 'git@github.com:ActiveTriples/ActiveTriples.git', :branch => 'master' - diff --git a/README.md b/README.md index f2c397d..6225c81 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # ActiveTriples::LocalName [![Build Status](https://travis-ci.org/ActiveTriples/active_triples-local_name.png?branch=master)](https://travis-ci.org/ActiveTriples/active_triples-local_name) +[![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. diff --git a/active_triples-local_name.gemspec b/active_triples-local_name.gemspec index 7dabef6..5094130 100644 --- a/active_triples-local_name.gemspec +++ b/active_triples-local_name.gemspec @@ -14,14 +14,14 @@ Gem::Specification.new do |s| s.license = "APACHE2" s.required_ruby_version = '>= 1.9.3' - # GETTING FROM GEMFILE UNTIL persistent check CODE IS RELEASED - # s.add_dependency('active-triples', '~> 0.4') + s.add_dependency('active-triples', '~> 0.5') s.add_dependency('deprecation', '~> 0.1') s.add_dependency('activesupport', '>= 3.0.0') s.add_development_dependency('rdoc') s.add_development_dependency('rspec') + s.add_development_dependency('coveralls') s.add_development_dependency('guard-rspec') s.files = `git ls-files`.split("\n") diff --git a/lib/active_triples/local_name/version.rb b/lib/active_triples/local_name/version.rb index 8549c04..8ad8668 100644 --- a/lib/active_triples/local_name/version.rb +++ b/lib/active_triples/local_name/version.rb @@ -1,5 +1,5 @@ module ActiveTriples module LocalName - VERSION = "0.1.0" + VERSION = "0.5.0" end end diff --git a/spec/active_triples/local_name/minter_spec.rb b/spec/active_triples/local_name/minter_spec.rb index c99d10b..612e255 100644 --- a/spec/active_triples/local_name/minter_spec.rb +++ b/spec/active_triples/local_name/minter_spec.rb @@ -81,10 +81,55 @@ def self.timeHashMinter3_method( *args ) ActiveTriples::Repositories.clear_repositories! end - context "when class doesn't have base_uri defined" do - it "should raise an Exception" do - expect{ ActiveTriples::LocalName::Minter.generate_local_name(DummyResource) }.to raise_error(RuntimeError, 'Requires base_uri to be defined in for_class.') + context "when one or more arguments are invalid" do +# def self.generate_local_name(for_class, max_tries=10, *minter_args, &minter_block) + + context "and all arguments are missing" do + it "should raise error" do + # NOTE: ruby < 2 puts out (0 for 1); ruby >= 2 puts out (0 for 1+) + expect{ ActiveTriples::LocalName::Minter.generate_local_name() }. + to raise_error(ArgumentError, /wrong number of arguments \(0 for 1\+?\)/) + end + end + + context "and max_tries is negative" do + it "should raise error" do + expect{ ActiveTriples::LocalName::Minter.generate_local_name(DummyResource,-1) }. + to raise_error(ArgumentError, 'Argument max_tries must be >= 1 if passed in') + end end + + context "and class doesn't inherit from ActiveTriples::Resource" do + before do + class DummyNonResource + end + end + after do + Object.send(:remove_const, "DummyNonResource") if Object + end + + it "should raise error" do + expect{ ActiveTriples::LocalName::Minter.generate_local_name(DummyNonResource) }. + to raise_error(ArgumentError, 'Argument for_class must inherit from ActiveTriples::Resource') + end + end + + + context "and class doesn't have base_uri defined" do + it "should raise error" do + expect{ ActiveTriples::LocalName::Minter.generate_local_name(DummyResource) }. + to raise_error(RuntimeError, 'Requires base_uri to be defined in for_class.') + end + end + + ## TODO: Can't see how to test this. The test complains if I pass anything other than a Proc instead of complaining that the passing in thing isn't callable. + # context "block isn't callable" do + # it "should raise error" do + # x = "foo" + # expect{ ActiveTriples::LocalName::Minter.generate_local_name(DummyResourceWithBaseURI,10,{:prefix => 'a'},&x) }. + # to raise_error(ArgumentError, 'Invalid minter_block.') + # end + # end end context "when all IDs available" do @@ -93,7 +138,7 @@ def self.timeHashMinter3_method( *args ) id = ActiveTriples::LocalName::Minter.generate_local_name(DummyResourceWithBaseURI) expect(id).to be_kind_of String expect(id.length).to eq 36 - id.should match /[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ + expect(id).to match /[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ end end @@ -291,7 +336,7 @@ def self.timeHashMinter3_method( *args ) id = ActiveTriples::LocalName::Minter.generate_local_name(DummyResourceWithBaseURI,10,{:prefix=>"s"}) expect(id).to be_kind_of String expect(id.length).to eq 37 - id.should match /s[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ + expect(id).to match /s[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ end end @@ -300,7 +345,7 @@ def self.timeHashMinter3_method( *args ) id = ActiveTriples::LocalName::Minter.generate_local_name(DummyResourceWithBaseURI,10,1) expect(id).to be_kind_of String expect(id.length).to eq 36 - id.should match /[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ + expect(id).to match /[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ end end @@ -309,7 +354,7 @@ def self.timeHashMinter3_method( *args ) id = ActiveTriples::LocalName::Minter.generate_local_name(DummyResourceWithBaseURI,10,{:prefix=>1.5}) expect(id).to be_kind_of String expect(id.length).to eq 36 - id.should match /[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ + expect(id).to match /[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ end end @@ -318,7 +363,7 @@ def self.timeHashMinter3_method( *args ) id = ActiveTriples::LocalName::Minter.generate_local_name(DummyResourceWithBaseURI,10,{:prefix=>{"a"=>"b"}}) expect(id).to be_kind_of String expect(id.length).to eq 36 - id.should match /[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ + expect(id).to match /[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ end end @@ -327,10 +372,68 @@ def self.timeHashMinter3_method( *args ) id = ActiveTriples::LocalName::Minter.generate_local_name(DummyResourceWithBaseURI,10,{:prefix=>[1,2,3]}) expect(id).to be_kind_of String expect(id.length).to eq 36 - id.should match /[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ + expect(id).to match /[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ end end end end + + describe "default_minter" do + context "when options are empty" do + it "creates a uuid" do + id = ActiveTriples::LocalName::Minter.default_minter + expect(id).to be_kind_of String + expect(id.length).to eq 36 + expect(id).to match /[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ + end + end + + context "when options is passed a prefix" do + context "and prefix is string" do + it "should generate a prefixed ID" do + id = ActiveTriples::LocalName::Minter.default_minter({:prefix=>"s"}) + expect(id).to be_kind_of String + expect(id.length).to eq 37 + expect(id).to match /s[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ + end + end + + context "and prefix is integer" do + it "should generate ID ignoring prefix" do + id = ActiveTriples::LocalName::Minter.default_minter({:prefix=>1}) + expect(id).to be_kind_of String + expect(id.length).to eq 36 + expect(id).to match /[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ + end + end + + context "and prefix is float" do + it "should generate ID ignoring prefix" do + id = ActiveTriples::LocalName::Minter.default_minter({:prefix=>1.5}) + expect(id).to be_kind_of String + expect(id.length).to eq 36 + expect(id).to match /[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ + end + end + + context "and prefix is hash" do + it "should generate ID ignoring prefix" do + id = ActiveTriples::LocalName::Minter.default_minter({:prefix=>{"a"=>"b"}}) + expect(id).to be_kind_of String + expect(id.length).to eq 36 + expect(id).to match /[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ + end + end + + context "and prefix is array" do + it "should generate ID ignoring prefix" do + id = ActiveTriples::LocalName::Minter.default_minter({:prefix=>[1,2,3]}) + expect(id).to be_kind_of String + expect(id.length).to eq 36 + expect(id).to match /[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/ + end + end + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9d71271..ba4d642 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,6 @@ +require 'coveralls' +Coveralls.wear! + require 'bundler/setup' Bundler.setup diff --git a/spec/support/active_model_lint.rb b/spec/support/active_model_lint.rb deleted file mode 100644 index 1b3f99d..0000000 --- a/spec/support/active_model_lint.rb +++ /dev/null @@ -1,81 +0,0 @@ -shared_examples_for "an ActiveModel" do - subject { described_class.new } - - describe '#to_key' do - it 'should respond' do - expect(subject).to respond_to :to_key - end - - it 'should return nil when #persisted? is false ' do - def subject.persisted?() false end - expect(subject.to_key).to eq nil - end - end - - describe '#to_param' do - it 'should respond' do - expect(subject).to respond_to :to_param - end - - it 'should return nil when #persisted? is false ' do - def subject.persisted?() false end - expect(subject.to_param).to eq nil - end - end - - describe '#model_name' do - let(:model_name) { subject.class.model_name } - - it 'should have a model name' do - expect(model_name).to respond_to :to_str - end - - it 'should have a human name' do - expect(model_name.human).to respond_to :to_str - end - - it 'should have a singular name' do - expect(model_name.singular).to respond_to :to_str - end - - it 'should have a plural name' do - expect(model_name.plural).to respond_to :to_str - end - end - - describe '#to_partial_path' do - it 'should return a string' do - expect(subject.to_partial_path).to be_a String - end - end - - describe '#persisted?' do - it 'should return a boolean' do - expect(match_boolean(subject.persisted?)).to be true - end - end - - describe '#valid?' do - it 'should return a boolean' do - expect(match_boolean(subject.valid?)).to be true - end - end - - describe '#new_record' do - it 'should return a boolean' do - expect(match_boolean(subject.new_record?)).to be true - end - end - - describe '#destroyed?' do - it 'should return a boolean' do - expect(match_boolean(subject.destroyed?)).to be true - end - end - - private - - def match_boolean(result) - result == true || result == false - end -end