activefedora-aggregation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 74fb6af1b85424431bb3effb4dfa78d4db3d7760
4
+ data.tar.gz: f7c34ca334ebae873b5ba1167ec1d209db901be8
5
+ SHA512:
6
+ metadata.gz: f85e2ecc89fad1ffc071017203b0b935086cbc08675259a8785eeee2cb9e6d1b8e86106de3aed64835b71b899057570bdaa448b2c585e02d62bce785fa0daa0f
7
+ data.tar.gz: 9beabc6d5def85ce0961339fdd8d51bb159989395f6f01d5758793b3a453ad266f9d9c02477cb193ad8ec948f7253ce1ac7cfeb1056d8c388ddb95594354919e
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /jetty
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activefedora-aggregation.gemspec
4
+ gemspec
5
+ gem 'rdf-vocab', github: 'projecthydra-labs/rdf-vocab'
6
+ gem 'byebug' unless ENV['CI']
7
+
data/LICENSE ADDED
@@ -0,0 +1,12 @@
1
+ ##########################################################################
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
data/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # ActiveFedora::Aggregation
2
+
3
+ Aggregations for ActiveFedora.
4
+
5
+ ### Example
6
+ ```ruby
7
+ class GenericFile < ActiveFedora::Base
8
+ end
9
+
10
+ generic_file1 = GenericFile.create
11
+ generic_file2 = GenericFile.create
12
+
13
+ class Image < ActiveFedora::Base
14
+ aggregates :generic_files
15
+ end
16
+
17
+ image = Image.create
18
+ image.generic_files = [generic_file2, generic_file1]
19
+ image.save
20
+
21
+ ```
22
+
23
+ Now the "generic\_files" method returns an ordered list of GenericFile objects.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require "bundler/gem_tasks"
2
+
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'jettywrapper'
9
+ Jettywrapper.hydra_jetty_version = "v8.3.0"
10
+
11
+ desc 'Spin up hydra-jetty and run specs'
12
+ task ci: ['jetty:clean'] do
13
+ puts 'running continuous integration'
14
+ jetty_params = Jettywrapper.load_config
15
+ jetty_params[:startup_wait]= 90
16
+ error = Jettywrapper.wrap(jetty_params) do
17
+ Rake::Task['spec'].invoke
18
+ end
19
+ raise "test failures: #{error}" if error
20
+ end
21
+
22
+
23
+ task default: :ci
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_fedora/aggregation/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activefedora-aggregation"
8
+ spec.version = ActiveFedora::Aggregation::VERSION
9
+ spec.authors = ["Justin Coyne"]
10
+ spec.email = ["justin@curationexperts.com"]
11
+
12
+ spec.summary = %q{Aggregations for active-fedora}
13
+ spec.homepage = "http://github.org/curationexperts/activefedora-aggregation"
14
+ spec.license = "APACHE2"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency 'activesupport'
20
+ spec.add_dependency 'active-fedora'
21
+ spec.add_dependency 'rdf-vocab'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.8"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.2"
26
+ spec.add_development_dependency "jettywrapper"
27
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "activefedora/aggregation"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,21 @@
1
+ require 'active_fedora/aggregation/version'
2
+ require 'active_support'
3
+ require 'active-fedora'
4
+ require 'rdf-vocab'
5
+
6
+ module ActiveFedora
7
+ module Aggregation
8
+ extend ActiveSupport::Autoload
9
+ eager_autoload do
10
+ autoload :Aggregator
11
+ autoload :Association
12
+ autoload :Proxy
13
+ autoload :Builder
14
+ autoload :ThroughAssociation
15
+ autoload :Reflection
16
+ autoload :BaseExtension
17
+ end
18
+
19
+ ActiveFedora::Base.extend BaseExtension
20
+ end
21
+ end
@@ -0,0 +1,96 @@
1
+ module ActiveFedora::Aggregation
2
+ class Aggregator < ActiveFedora::Base
3
+ has_many :proxies, predicate: ::RDF::Vocab::ORE.proxyIn, class_name: 'ActiveFedora::Aggregation::Proxy'
4
+ belongs_to :head, predicate: ::RDF::Vocab::IANA['first'], class_name: 'ActiveFedora::Aggregation::Proxy'
5
+ belongs_to :tail, predicate: ::RDF::Vocab::IANA.last, class_name: 'ActiveFedora::Aggregation::Proxy'
6
+
7
+ def first
8
+ head.target
9
+ end
10
+
11
+ # This can be a very expensive operation. avoid if possible
12
+ def to_a
13
+ @target ||= list_of_proxies.map(&:target)
14
+ end
15
+
16
+ def target= (collection)
17
+ link_target(build_proxies(collection))
18
+ end
19
+
20
+ def target_ids=(object_ids)
21
+ link_target(build_proxies_with_ids(object_ids))
22
+ end
23
+
24
+ # Set the links on the nodes in the list
25
+ def link_target(new_proxies)
26
+ new_proxies.each_with_index do |proxy, idx|
27
+ proxy.next_id = new_proxies[idx+1].id unless new_proxies.length - 1 <= idx
28
+ proxy.prev_id = new_proxies[idx-1].id unless idx == 0
29
+ end
30
+
31
+ self.head = new_proxies.first
32
+ self.tail = new_proxies.last
33
+ self.proxies = new_proxies
34
+ end
35
+
36
+ # TODO clear out the old proxies (or reuse them)
37
+ def build_proxies(objects)
38
+ # need to create the proxies before we can add the links otherwise the linked to resource won't exist
39
+ objects.map do |object|
40
+ Proxy.create(id: mint_proxy_id, target: object)
41
+ end
42
+ end
43
+
44
+ # TODO clear out the old proxies (or reuse them)
45
+ def build_proxies_with_ids(object_ids)
46
+ # need to create the proxies before we can add the links otherwise the linked to resource won't exist
47
+ object_ids.map do |file_id|
48
+ Proxy.create(id: mint_proxy_id, target_id: file_id)
49
+ end
50
+ end
51
+
52
+ def target_ids
53
+ list_of_proxies.map(&:target_id)
54
+ end
55
+
56
+ # @param obj [ActiveFedora::Base]
57
+ def << (obj)
58
+ node = if persisted?
59
+ proxies.create(id: mint_proxy_id, target: obj, prev: tail)
60
+ else
61
+ proxies.build(id: mint_proxy_id, target: obj, prev: tail)
62
+ end
63
+ # set the old tail, if present, to have this new proxy as its next
64
+ self.tail.update(next: node) if tail
65
+ # update the tail to point at the new node
66
+ self.tail = node
67
+ # if this is the first node, set it to be the head
68
+ self.head = node unless head
69
+ reset_target!
70
+ end
71
+
72
+ def mint_proxy_id
73
+ "#{id}/#{SecureRandom.uuid}"
74
+ end
75
+
76
+ def self.find_or_initialize(id)
77
+ find(id)
78
+ rescue ActiveFedora::ObjectNotFoundError
79
+ new(id)
80
+ end
81
+
82
+ def reset_target!
83
+ @proxy_list = nil
84
+ @target = nil
85
+ end
86
+
87
+ # return the proxies in order
88
+ def list_of_proxies
89
+ @proxy_list ||= if head
90
+ head.as_list
91
+ else
92
+ []
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,55 @@
1
+ module ActiveFedora::Aggregation
2
+ class Association
3
+
4
+ # @param [ActiveFedora::Base] parent
5
+ # @param [Reflection] reflection
6
+ # @opts options [String] class_name name of the class in the association
7
+ def initialize(parent, reflection)
8
+ @parent = parent
9
+ @reflection = reflection
10
+ end
11
+
12
+ def klass
13
+ @reflection.klass
14
+ end
15
+
16
+ def == other
17
+ aggregation.to_a == other
18
+ end
19
+
20
+ def create(&block)
21
+ klass.create(&block).tap do |created|
22
+ aggregation << created
23
+ end
24
+ save #causes the (head/tail) pointers on the aggregation to be persisted
25
+ end
26
+
27
+ def save
28
+ aggregation.save
29
+ end
30
+
31
+ def target=(vals)
32
+ aggregation.target=(vals)
33
+ end
34
+
35
+ def target_ids=(vals)
36
+ aggregation.target_ids=(vals)
37
+ end
38
+
39
+ def target_ids
40
+ aggregation.target_ids
41
+ end
42
+
43
+ def aggregation
44
+ @aggregation ||= Aggregator.find_or_initialize(klass.uri_to_id(uri))
45
+ end
46
+
47
+ def first
48
+ aggregation.first
49
+ end
50
+
51
+ def uri
52
+ @parent.uri + '/files'
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,24 @@
1
+ module ActiveFedora::Aggregation
2
+ module BaseExtension
3
+
4
+ ##
5
+ # Create an aggregation association on the class
6
+ # @example
7
+ # class Image < ActiveFedora::Base
8
+ # aggregates :generic_files
9
+ # end
10
+ def aggregates(name, options={})
11
+ Builder.build(self, name, options)
12
+ end
13
+
14
+ def create_reflection(macro, name, options, active_fedora)
15
+ if macro == :aggregation
16
+ Reflection.new(macro, name, options, active_fedora).tap do |reflection|
17
+ add_reflection name, reflection
18
+ end
19
+ else
20
+ super
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,44 @@
1
+ module ActiveFedora::Aggregation
2
+ class Builder < ActiveFedora::Associations::Builder::CollectionAssociation
3
+ self.macro = :aggregation
4
+
5
+ def build
6
+ reflection = super
7
+ configure_dependency
8
+ reflection
9
+ end
10
+
11
+ def self.define_readers(mixin, name)
12
+ super
13
+ mixin.redefine_method("#{name.to_s.singularize}_ids") do
14
+ association(name).ids_reader
15
+ end
16
+ end
17
+
18
+ def self.define_writers(mixin, name)
19
+ super
20
+ mixin.redefine_method("#{name.to_s.singularize}_ids=") do |ids|
21
+ association(name).ids_writer(ids)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def configure_dependency
28
+ define_save_dependency_method
29
+ model.after_save dependency_method_name
30
+ end
31
+
32
+ def define_save_dependency_method
33
+ name = self.name
34
+ model.send(:define_method, dependency_method_name) do
35
+ send(name).save
36
+ end
37
+ end
38
+
39
+ def dependency_method_name
40
+ "aggregator_dependent_for_#{name}"
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,16 @@
1
+ module ActiveFedora::Aggregation
2
+ class Proxy < ActiveFedora::Base
3
+ belongs_to :aggregator, predicate: ::RDF::Vocab::ORE.proxyIn, class_name: 'ActiveFedora::Aggregation::Proxy'
4
+ belongs_to :target, predicate: ::RDF::Vocab::ORE.proxyFor, class_name: 'ActiveFedora::Base'
5
+ belongs_to :next, predicate: ::RDF::Vocab::IANA.next, class_name: 'ActiveFedora::Aggregation::Proxy'
6
+ belongs_to :prev, predicate: ::RDF::Vocab::IANA.prev, class_name: 'ActiveFedora::Aggregation::Proxy'
7
+
8
+ def as_list
9
+ if self.next
10
+ [self] + self.next.as_list
11
+ else
12
+ [self]
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ module ActiveFedora::Aggregation
2
+ class Reflection < ActiveFedora::Reflection::AssociationReflection
3
+ def association_class
4
+ ThroughAssociation
5
+ end
6
+
7
+ def collection?
8
+ true
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ module ActiveFedora::Aggregation
2
+ class ThroughAssociation
3
+ def initialize(owner, reflection)
4
+ @owner, @reflection = owner, reflection
5
+ end
6
+
7
+ # has_one :aggregation
8
+ # has_many :generic_files, through: :aggregation
9
+ def reader
10
+ @file_association ||= Association.new(@owner, @reflection)
11
+ end
12
+
13
+ def writer(vals)
14
+ reader.target = vals
15
+ end
16
+
17
+ def ids_writer(vals)
18
+ reader.target_ids = vals
19
+ end
20
+
21
+ def ids_reader
22
+ reader.target_ids
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveFedora
2
+ module Aggregation
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activefedora-aggregation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Coyne
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: active-fedora
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rdf-vocab
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: jettywrapper
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - justin@curationexperts.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - CODE_OF_CONDUCT.md
122
+ - Gemfile
123
+ - LICENSE
124
+ - README.md
125
+ - Rakefile
126
+ - activefedora-aggregation.gemspec
127
+ - bin/console
128
+ - bin/setup
129
+ - lib/active_fedora/aggregation.rb
130
+ - lib/active_fedora/aggregation/aggregator.rb
131
+ - lib/active_fedora/aggregation/association.rb
132
+ - lib/active_fedora/aggregation/base_extension.rb
133
+ - lib/active_fedora/aggregation/builder.rb
134
+ - lib/active_fedora/aggregation/proxy.rb
135
+ - lib/active_fedora/aggregation/reflection.rb
136
+ - lib/active_fedora/aggregation/through_association.rb
137
+ - lib/active_fedora/aggregation/version.rb
138
+ homepage: http://github.org/curationexperts/activefedora-aggregation
139
+ licenses:
140
+ - APACHE2
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.4.5
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Aggregations for active-fedora
162
+ test_files: []
163
+ has_rdoc: