Skip to content

Commit

Permalink
alias as_json to to_ordered_hash
Browse files Browse the repository at this point in the history
add shared_example for as_json to relevant specs
move shared examples to spec/support following rspec convention
https://relishapp.com/rspec/rspec-core/docs/example-groups/shared-examples
  • Loading branch information
ConorSheehan1 committed Oct 5, 2018
1 parent d659b73 commit 3b76a54
Show file tree
Hide file tree
Showing 19 changed files with 55 additions and 7 deletions.
5 changes: 3 additions & 2 deletions lib/iiif/presentation/abstract_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def initialize(hsh={})
super(hsh)
end


# Options:
# * force: (true|false). Skips validations.
# * include_context: (true|false). Adds the @context to the top of the
Expand All @@ -71,9 +70,11 @@ def to_ordered_hash(opts={})
end
super(opts)
end

# alias_method has to be included after to_ordered_hash is defined
alias_method :as_json, :to_ordered_hash

end

end
end

3 changes: 3 additions & 0 deletions lib/iiif/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ def to_ordered_hash(opts={})
export_hash
end

# alias_method has to be included after to_ordered_hash is defined
alias_method :as_json, :to_ordered_hash

def self.from_ordered_hash(hsh, default_klass=IIIF::OrderedHash)
# Create a new object (new_object)
type = nil
Expand Down
4 changes: 3 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require 'iiif/presentation'
require 'simplecov'
require 'coveralls'
Dir["#{File.dirname(__FILE__)}/unit/iiif/presentation/shared_examples/*.rb"].each do |f|

Dir["#{File.absolute_path(__dir__)}/support/**/*.rb"].each do |f|
require f
end

require 'vcr'

VCR.configure do |c|
Expand Down
8 changes: 8 additions & 0 deletions spec/support/shared_examples/json_format.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
shared_examples 'it has symmetric as_json and to_json methods' do
describe "#{described_class}.as_json" do
it 'should return a json representation of the object as a ruby hash' do
obj = described_class.new(fixed_values)
expect(obj.as_json).to eq JSON.parse(obj.to_json)
end
end
end
3 changes: 3 additions & 0 deletions spec/unit/iiif/presentation/annotation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
describe IIIF::Presentation::Annotation do

let(:fixed_values) { {} }

describe "#{described_class}.define_methods_for_abstract_resource_only_keys" do
it_behaves_like 'it has the appropriate methods for abstract_resource_only_keys'
end

it_behaves_like 'it has symmetric as_json and to_json methods'
end
1 change: 1 addition & 0 deletions spec/unit/iiif/presentation/canvas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@
end
end

it_behaves_like 'it has symmetric as_json and to_json methods'
end

1 change: 1 addition & 0 deletions spec/unit/iiif/presentation/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
describe '#validate' do
end

it_behaves_like 'it has symmetric as_json and to_json methods'
end


11 changes: 11 additions & 0 deletions spec/unit/iiif/presentation/image_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,15 @@
it_behaves_like 'it has the appropriate methods for integer-only keys'
end

let(:fixed_values) do
{
"@context" => "http://iiif.io/api/presentation/2/context.json",
"@id" => "http://www.example.org/iiif/image",
"label" => "p. 1",
"height" => 1000,
"width" => 750,
}
end

it_behaves_like 'it has symmetric as_json and to_json methods'
end
2 changes: 2 additions & 0 deletions spec/unit/iiif/presentation/layer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@
it_behaves_like 'it has the appropriate methods for any-type keys'
end

it_behaves_like 'it has symmetric as_json and to_json methods'

end

7 changes: 4 additions & 3 deletions spec/unit/iiif/presentation/manifest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ def initialize(hsh={})

let(:fixed_values) do
{
'type' => 'a:SubClass',
'id' => 'http://example.com/prefix/manifest/123',
'context' => IIIF::Presentation::CONTEXT,
'@type' => 'a:SubClass',
'@id' => 'http://example.com/prefix/manifest/123',
'@context' => IIIF::Presentation::CONTEXT,
'label' => 'Book 1',
'description' => 'A longer description of this example book. It should give some real information.',
'thumbnail' => {
Expand Down Expand Up @@ -86,4 +86,5 @@ def initialize(hsh={})
it_behaves_like 'it has the appropriate methods for any-type keys'
end

it_behaves_like 'it has symmetric as_json and to_json methods'
end
1 change: 1 addition & 0 deletions spec/unit/iiif/presentation/range_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
describe '#validate' do
end

it_behaves_like 'it has symmetric as_json and to_json methods'
end


9 changes: 9 additions & 0 deletions spec/unit/iiif/presentation/resource_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
describe IIIF::Presentation::Resource do

let(:fixed_values) do
{
'@id' => 'http://www.example.org/iiif/book1/resource',
# '@type' => 'sc:Range',
'label' => 'Introduction'
}
end

describe "#{described_class}.define_methods_for_abstract_resource_only_keys" do
it_behaves_like 'it has the appropriate methods for abstract_resource_only_keys'
end
Expand All @@ -12,5 +20,6 @@
it_behaves_like 'it has the appropriate methods for string-only keys'
end

it_behaves_like 'it has symmetric as_json and to_json methods'
end

3 changes: 2 additions & 1 deletion spec/unit/iiif/presentation/sequence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def initialize(hsh={})
end
end


it_behaves_like 'it has symmetric as_json and to_json methods'

end

4 changes: 4 additions & 0 deletions spec/unit/iiif/service_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
describe IIIF::Service do

let(:fixed_values) { {} }

describe 'self#get_descendant_class_by_jld_type' do
before do
Expand All @@ -25,4 +27,6 @@ def self.singleton_class?
end
end

it_behaves_like 'it has symmetric as_json and to_json methods'

end

0 comments on commit 3b76a54

Please sign in to comment.