active-fedora 11.0.0 → 12.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +12 -2
  3. data/.rubocop_todo.yml +73 -18
  4. data/.travis.yml +8 -9
  5. data/CONTRIBUTING.md +66 -20
  6. data/History.txt +51 -0
  7. data/README.md +11 -14
  8. data/active-fedora.gemspec +14 -13
  9. data/lib/active_fedora/aggregation/base_extension.rb +3 -1
  10. data/lib/active_fedora/aggregation/list_source.rb +1 -1
  11. data/lib/active_fedora/associations/association.rb +1 -2
  12. data/lib/active_fedora/associations/basic_contains_association.rb +12 -0
  13. data/lib/active_fedora/associations/builder/association.rb +1 -2
  14. data/lib/active_fedora/associations/collection_association.rb +3 -3
  15. data/lib/active_fedora/associations/collection_proxy.rb +10 -10
  16. data/lib/active_fedora/associations/contained_finder.rb +2 -1
  17. data/lib/active_fedora/associations/has_and_belongs_to_many_association.rb +1 -1
  18. data/lib/active_fedora/associations/null_validator.rb +1 -2
  19. data/lib/active_fedora/associations/rdf.rb +1 -1
  20. data/lib/active_fedora/attached_files.rb +4 -0
  21. data/lib/active_fedora/attribute_methods.rb +1 -1
  22. data/lib/active_fedora/attributes/property_builder.rb +7 -5
  23. data/lib/active_fedora/caching_connection.rb +1 -1
  24. data/lib/active_fedora/cleaner.rb +1 -1
  25. data/lib/active_fedora/common.rb +10 -0
  26. data/lib/active_fedora/containers/container.rb +1 -2
  27. data/lib/active_fedora/core.rb +12 -5
  28. data/lib/active_fedora/errors.rb +4 -0
  29. data/lib/active_fedora/fedora.rb +19 -5
  30. data/lib/active_fedora/file/attributes.rb +5 -0
  31. data/lib/active_fedora/file.rb +3 -3
  32. data/lib/active_fedora/file_configurator.rb +5 -5
  33. data/lib/active_fedora/file_io.rb +120 -0
  34. data/lib/active_fedora/fixity_service.rb +36 -5
  35. data/lib/active_fedora/indexers/null_indexer.rb +1 -2
  36. data/lib/active_fedora/indexing/default_descriptors.rb +128 -0
  37. data/lib/active_fedora/indexing/descendant_fetcher.rb +105 -0
  38. data/lib/active_fedora/indexing/descriptor.rb +44 -0
  39. data/lib/active_fedora/indexing/field_mapper.rb +146 -0
  40. data/lib/active_fedora/indexing/inserter.rb +40 -0
  41. data/lib/active_fedora/indexing/map.rb +11 -3
  42. data/lib/active_fedora/indexing/suffix.rb +81 -0
  43. data/lib/active_fedora/indexing.rb +39 -15
  44. data/lib/active_fedora/indexing_service.rb +8 -3
  45. data/lib/active_fedora/inheritance.rb +1 -1
  46. data/lib/active_fedora/initializing_connection.rb +1 -1
  47. data/lib/active_fedora/ldp_resource.rb +1 -2
  48. data/lib/active_fedora/model_classifier.rb +1 -1
  49. data/lib/active_fedora/null_logger.rb +13 -0
  50. data/lib/active_fedora/orders/ordered_list.rb +1 -1
  51. data/lib/active_fedora/persistence.rb +16 -3
  52. data/lib/active_fedora/property.rb +1 -2
  53. data/lib/active_fedora/railtie.rb +6 -2
  54. data/lib/active_fedora/rake_support.rb +1 -1
  55. data/lib/active_fedora/rdf/field_map.rb +2 -2
  56. data/lib/active_fedora/rdf/field_map_entry.rb +15 -7
  57. data/lib/active_fedora/rdf/indexing_service.rb +15 -13
  58. data/lib/active_fedora/rdf/value_caster.rb +15 -0
  59. data/lib/active_fedora/rdf.rb +1 -0
  60. data/lib/active_fedora/relation/finder_methods.rb +28 -40
  61. data/lib/active_fedora/solr_hit.rb +1 -1
  62. data/lib/active_fedora/solr_service.rb +20 -2
  63. data/lib/active_fedora/type/value.rb +2 -4
  64. data/lib/active_fedora/version.rb +1 -1
  65. data/lib/active_fedora/with_metadata/metadata_node.rb +4 -1
  66. data/lib/active_fedora.rb +23 -9
  67. data/lib/generators/active_fedora/config/fedora/templates/.fcrepo_wrapper +1 -1
  68. data/lib/generators/active_fedora/config/solr/templates/solr/config/schema.xml +0 -6
  69. data/lib/generators/active_fedora/config/solr/templates/solr.yml +3 -3
  70. data/spec/integration/associations_spec.rb +8 -8
  71. data/spec/integration/attached_files_spec.rb +2 -3
  72. data/spec/integration/base_spec.rb +43 -39
  73. data/spec/integration/basic_contains_association_spec.rb +16 -3
  74. data/spec/integration/collection_association_spec.rb +1 -1
  75. data/spec/integration/direct_container_spec.rb +2 -2
  76. data/spec/integration/fedora_solr_sync_spec.rb +1 -1
  77. data/spec/integration/file_spec.rb +46 -13
  78. data/spec/integration/generators/fedora_generator_spec.rb +1 -1
  79. data/spec/integration/generators/solr_generator_spec.rb +1 -1
  80. data/spec/integration/has_and_belongs_to_many_associations_spec.rb +2 -2
  81. data/spec/integration/has_many_associations_spec.rb +4 -5
  82. data/spec/integration/indexing/descendant_fetcher_spec.rb +64 -0
  83. data/spec/integration/indexing_spec.rb +22 -2
  84. data/spec/integration/indirect_container_spec.rb +2 -2
  85. data/spec/integration/query_result_builder_spec.rb +2 -2
  86. data/spec/integration/scoping_spec.rb +20 -14
  87. data/spec/integration/solr_hit_spec.rb +1 -1
  88. data/spec/integration/versionable_spec.rb +2 -2
  89. data/spec/lib/active_fedora/null_logger_spec.rb +10 -0
  90. data/spec/unit/active_fedora/indexing/inserter_spec.rb +30 -0
  91. data/spec/unit/attached_files_spec.rb +36 -8
  92. data/spec/unit/attributes_spec.rb +8 -14
  93. data/spec/unit/base_spec.rb +7 -7
  94. data/spec/unit/builder/has_and_belongs_to_many_spec.rb +1 -1
  95. data/spec/unit/callback_spec.rb +2 -2
  96. data/spec/unit/change_set_spec.rb +1 -2
  97. data/spec/unit/collection_proxy_spec.rb +2 -1
  98. data/spec/unit/core/fedora_id_translator_spec.rb +2 -2
  99. data/spec/unit/core/fedora_uri_translator_spec.rb +2 -2
  100. data/spec/unit/core/logger_spec.rb +18 -0
  101. data/spec/unit/core_spec.rb +46 -12
  102. data/spec/unit/default_model_mapper_spec.rb +1 -1
  103. data/spec/unit/fedora_spec.rb +12 -0
  104. data/spec/unit/file_configurator_spec.rb +0 -9
  105. data/spec/unit/file_io_spec.rb +137 -0
  106. data/spec/unit/file_path_builder_spec.rb +3 -3
  107. data/spec/unit/file_spec.rb +70 -26
  108. data/spec/unit/files_hash_spec.rb +1 -2
  109. data/spec/unit/filter_spec.rb +2 -2
  110. data/spec/unit/finder_methods_spec.rb +1 -1
  111. data/spec/unit/fixity_service_spec.rb +65 -5
  112. data/spec/unit/has_and_belongs_to_many_association_spec.rb +1 -2
  113. data/spec/unit/has_many_association_spec.rb +2 -2
  114. data/spec/unit/indexing/map/index_object_spec.rb +25 -0
  115. data/spec/unit/indexing/map_spec.rb +27 -0
  116. data/spec/unit/indexing_spec.rb +3 -4
  117. data/spec/unit/inheritance_spec.rb +28 -1
  118. data/spec/unit/model_classifier_spec.rb +1 -1
  119. data/spec/unit/pathing_spec.rb +1 -2
  120. data/spec/unit/query_result_builder_spec.rb +1 -2
  121. data/spec/unit/query_spec.rb +4 -4
  122. data/spec/unit/rdf/indexing_service_spec.rb +18 -4
  123. data/spec/unit/reflection_spec.rb +2 -2
  124. data/spec/unit/solr_hit_spec.rb +1 -2
  125. data/spec/unit/solr_service_spec.rb +52 -4
  126. data/spec/unit/sparql_insert_spec.rb +1 -1
  127. data/spec/unit/validations_spec.rb +27 -4
  128. data/spec/unit/with_metadata/metadata_node_spec.rb +26 -1
  129. metadata +99 -29
@@ -115,7 +115,7 @@ module ActiveFedora
115
115
  end
116
116
 
117
117
  # Finds an object in the collection responding to the +id+. Uses the same
118
- # rules as <tt>ActiveFedora::Base.find</tt>. Returns <tt>ActiveFedora::RecordNotFound</tt>
118
+ # rules as <tt>ActiveFedora::Base.find</tt>. Returns <tt>ActiveFedora::ObjectNotFoundError</tt>
119
119
  # error if the object can not be found.
120
120
  #
121
121
  # class Person < ActiveFedora::Base
@@ -130,7 +130,7 @@ module ActiveFedora
130
130
  # # ]
131
131
  #
132
132
  # person.pets.find(1) # => #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>
133
- # person.pets.find(4) # => ActiveFedora::RecordNotFound: Couldn't find Pet with id=4
133
+ # person.pets.find(4) # => ActiveFedora::ObjectNotFoundError: Couldn't find Pet with id=4
134
134
  #
135
135
  # person.pets.find(2) { |pet| pet.name.downcase! }
136
136
  # # => #<Pet id: 2, name: "fancy-fancy", person_id: 1>
@@ -394,7 +394,7 @@ module ActiveFedora
394
394
  # # ]
395
395
  #
396
396
  # Pet.find(1, 2, 3)
397
- # # => ActiveFedora::RecordNotFound
397
+ # # => ActiveFedora::ObjectNotFoundError
398
398
  #
399
399
  # If it is set to <tt>:delete_all</tt>, all the objects are deleted
400
400
  # *without* calling their +destroy+ method.
@@ -419,7 +419,7 @@ module ActiveFedora
419
419
  # # ]
420
420
  #
421
421
  # Pet.find(1, 2, 3)
422
- # # => ActiveFedora::RecordNotFound
422
+ # # => ActiveFedora::ObjectNotFoundError
423
423
  def delete_all
424
424
  @association.delete_all
425
425
  end
@@ -511,7 +511,7 @@ module ActiveFedora
511
511
  # # => [#<Pet id: 2, name: "Spook", person_id: 1>]
512
512
  #
513
513
  # Pet.find(1, 3)
514
- # # => ActiveFedora::RecordNotFound: Couldn't find all Pets with IDs (1, 3)
514
+ # # => ActiveFedora::ObjectNotFoundError: Couldn't find all Pets with IDs (1, 3)
515
515
  #
516
516
  # If it is set to <tt>:delete_all</tt>, all the +records+ are deleted
517
517
  # *without* calling their +destroy+ method.
@@ -539,9 +539,9 @@ module ActiveFedora
539
539
  # # ]
540
540
  #
541
541
  # Pet.find(1)
542
- # # => ActiveFedora::RecordNotFound: Couldn't find Pet with id=1
542
+ # # => ActiveFedora::ObjectNotFoundError: Couldn't find Pet with id=1
543
543
  #
544
- # You can pass +Fixnum+ or +String+ values, it finds the records
544
+ # You can pass +Integer+ or +String+ values, it finds the records
545
545
  # responding to the +id+ and executes delete on them.
546
546
  #
547
547
  # class Person < ActiveFedora::Base
@@ -603,9 +603,9 @@ module ActiveFedora
603
603
  # person.pets.size # => 0
604
604
  # person.pets # => []
605
605
  #
606
- # Pet.find(1, 2, 3) # => ActiveFedora::RecordNotFound: Couldn't find all Pets with IDs (1, 2, 3)
606
+ # Pet.find(1, 2, 3) # => ActiveFedora::ObjectNotFoundError: Couldn't find all Pets with IDs (1, 2, 3)
607
607
  #
608
- # You can pass +Fixnum+ or +String+ values, it finds the records
608
+ # You can pass +Integer+ or +String+ values, it finds the records
609
609
  # responding to the +id+ and then deletes them from the database.
610
610
  #
611
611
  # person.pets.size # => 3
@@ -635,7 +635,7 @@ module ActiveFedora
635
635
  # person.pets.size # => 0
636
636
  # person.pets # => []
637
637
  #
638
- # Pet.find(4, 5, 6) # => ActiveFedora::RecordNotFound: Couldn't find all Pets with IDs (4, 5, 6)
638
+ # Pet.find(4, 5, 6) # => ActiveFedora::ObjectNotFoundError: Couldn't find all Pets with IDs (4, 5, 6)
639
639
  def destroy(*records)
640
640
  @association.destroy(*records)
641
641
  end
@@ -42,7 +42,8 @@ module ActiveFedora::Associations
42
42
  query = ActiveFedora::SolrQueryBuilder.construct_query_for_rel(
43
43
  [[:has_model, proxy_class.to_rdf_representation], [:proxyFor, record.id]]
44
44
  )
45
- ActiveFedora::SolrService.query(query, fl: 'id').map(&:rdf_uri)
45
+ rows = ActiveFedora::SolrService::MAX_ROWS
46
+ ActiveFedora::SolrService.query(query, fl: 'id', rows: rows).map(&:rdf_uri)
46
47
  end
47
48
  end
48
49
  end
@@ -21,7 +21,7 @@ module ActiveFedora
21
21
  if reflection.options[:inverse_of]
22
22
  inverse = reflection.inverse_of
23
23
  if owner.new_record?
24
- ActiveFedora::Base.logger.warn("has_and_belongs_to_many #{reflection.inspect} is cowardly refusing to insert the inverse relationship into #{record}, because #{owner} is not persisted yet.") if ActiveFedora::Base.logger
24
+ ActiveFedora::Base.logger.warn("has_and_belongs_to_many #{reflection.inspect} is cowardly refusing to insert the inverse relationship into #{record}, because #{owner} is not persisted yet.")
25
25
  elsif inverse.has_and_belongs_to_many?
26
26
  record[inverse.foreign_key] ||= []
27
27
  record[inverse.foreign_key] += [owner.id]
@@ -2,7 +2,6 @@ module ActiveFedora::Associations
2
2
  ##
3
3
  # An association type validator which does no validation.
4
4
  class NullValidator
5
- def self.validate!(_reflection, _object)
6
- end
5
+ def self.validate!(_reflection, _object); end
7
6
  end
8
7
  end
@@ -61,7 +61,7 @@ module ActiveFedora
61
61
  def filter_by_class(candidate_uris)
62
62
  return [] if candidate_uris.empty?
63
63
  ids = candidate_uris.map { |uri| ActiveFedora::Base.uri_to_id(uri) }
64
- results = ActiveFedora::SolrService.query(ActiveFedora::SolrQueryBuilder.construct_query_for_ids(ids), rows: 10_000)
64
+ results = ActiveFedora::SolrService.query(ActiveFedora::SolrQueryBuilder.construct_query_for_ids(ids), rows: SolrService::MAX_ROWS)
65
65
 
66
66
  results.select { |result| result.model? reflection.klass }.map(&:rdf_uri)
67
67
  end
@@ -33,7 +33,11 @@ module ActiveFedora
33
33
  resource.query(subject: resource, predicate: ::RDF::Vocab::LDP.contains).objects.map(&:to_s)
34
34
  end
35
35
 
36
+ # Load any undeclared relationships or has_subresource relationships. These are non-idiomatic LDP
37
+ # because we are going to find the subresource by knowing it's subpath ahead of time.
38
+ # Does nothing if this object is using idiomatic basic containment, by declaring `is_a_container`
36
39
  def load_attached_files
40
+ return if reflections[:contains] && reflections[:contains].macro == :is_a_container
37
41
  contains_assertions.each do |file_uri|
38
42
  path = file_uri.to_s.sub(uri + '/', '')
39
43
  next if association(path.to_sym)
@@ -235,7 +235,7 @@ module ActiveFedora
235
235
  # person = Person.new
236
236
  # person[:age] = '22'
237
237
  # person[:age] # => 22
238
- # person[:age] # => Fixnum
238
+ # person[:age] # => Integer
239
239
  def []=(attr_name, value)
240
240
  write_attribute(attr_name, value)
241
241
  end
@@ -13,10 +13,11 @@ module ActiveFedora::Attributes
13
13
  end
14
14
 
15
15
  def self.define_writers(mixin, name)
16
+ # RDF Terms are singular, even if they respond to `#each`
16
17
  mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
17
18
  def #{name}=(value)
18
- if value.present? && !value.respond_to?(:each)
19
- raise ArgumentError, "You attempted to set the property `#{name}' to a scalar value. However, this property is declared as being multivalued."
19
+ unless value.nil? || (value.respond_to?(:each) && !(value.respond_to?(:term?) && value.term?))
20
+ raise ArgumentError, "You attempted to set the property `#{name}' of \#{id} to a scalar value. However, this property is declared as being multivalued."
20
21
  end
21
22
  set_value(:#{name}, value)
22
23
  end
@@ -28,7 +29,7 @@ module ActiveFedora::Attributes
28
29
  def #{name}(*args)
29
30
  vals = get_values(:#{name})
30
31
  return nil unless vals
31
- raise ActiveFedora::ConstraintError, "Expected \\"#{name}\\" to have 0-1 statements, but there are \#{vals.size}" if vals.size > 1
32
+ raise ActiveFedora::ConstraintError, "Expected \\"#{name}\\" of \#{id} to have 0-1 statements, but there are \#{vals.size}" if vals.size > 1
32
33
  vals.first
33
34
  end
34
35
  CODE
@@ -43,10 +44,11 @@ module ActiveFedora::Attributes
43
44
  end
44
45
 
45
46
  def self.define_singular_writers(mixin, name)
47
+ # RDF Terms are singular, even if they respond to `#each`
46
48
  mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
47
49
  def #{name}=(value)
48
- if value.respond_to?(:each) # singular
49
- raise ArgumentError, "You attempted to set the property `#{name}' to an enumerable value. However, this property is declared as singular."
50
+ if value.respond_to?(:each) && !(value.respond_to?(:term?) && value.term?)
51
+ raise ArgumentError, "You attempted to set the property `#{name}' of \#{id} to an enumerable value. However, this property is declared as singular."
50
52
  end
51
53
  set_value(:#{name}, value)
52
54
  end
@@ -10,7 +10,7 @@ module ActiveFedora
10
10
  if @cache_enabled
11
11
  cache_resource(url) { super }
12
12
  else
13
- super
13
+ log(url) { super }
14
14
  end
15
15
  end
16
16
 
@@ -48,7 +48,7 @@ module ActiveFedora
48
48
  end
49
49
 
50
50
  def self.log(message)
51
- ActiveFedora::Base.logger.debug message if ActiveFedora::Base.logger
51
+ ActiveFedora::Base.logger.debug message
52
52
  end
53
53
  end
54
54
  end
@@ -17,6 +17,16 @@ module ActiveFedora
17
17
  end
18
18
  end
19
19
 
20
+ ##
21
+ # @return [String] the etag from the response headers
22
+ #
23
+ # @raise [RuntimeError] when the resource is new and has no etag
24
+ # @raise [Ldp::Gone] when the resource is deleted
25
+ def etag
26
+ raise 'Unable to produce an etag for a unsaved object' if ldp_source.new?
27
+ ldp_source.head.etag
28
+ end
29
+
20
30
  def ldp_source
21
31
  @ldp_source
22
32
  end
@@ -28,7 +28,6 @@ module ActiveFedora
28
28
  private
29
29
 
30
30
  # Don't allow directly setting contained
31
- def contained=(*_args)
32
- end
31
+ def contained=(*_args); end
33
32
  end
34
33
  end
@@ -8,13 +8,19 @@ module ActiveFedora
8
8
  autoload :FedoraUriTranslator
9
9
 
10
10
  included do
11
- ##
12
- # :singleton-method:
13
- #
11
+ mattr_accessor :belongs_to_required_by_default, instance_accessor: false
12
+
14
13
  # Accepts a logger conforming to the interface of Log4r which can be
15
14
  # retrieved on both a class and instance level by calling +logger+.
16
- mattr_accessor :logger, instance_writer: false
17
- mattr_accessor :belongs_to_required_by_default, instance_accessor: false
15
+ class_attribute :logger
16
+
17
+ # Use NullLogger as the default and all messages sent to it are retuned as nil
18
+ self.logger = ActiveFedora::NullLogger.new
19
+
20
+ # All instances default to the class-level logger
21
+ def logger
22
+ self.class.logger
23
+ end
18
24
  end
19
25
 
20
26
  # Constructor. You may supply a custom +:id+, or we call the Fedora Rest API for the
@@ -24,6 +30,7 @@ module ActiveFedora
24
30
  # the given namespace.
25
31
  def initialize(attributes = nil, &_block)
26
32
  init_internals
33
+ attributes = attributes.dup if attributes # can't dup nil in Ruby 2.3
27
34
  id = attributes && (attributes.delete(:id) || attributes.delete('id'))
28
35
  @ldp_source = build_ldp_resource(id)
29
36
  raise IllegalOperation, "Attempting to recreate existing ldp_source: `#{ldp_source.subject}'" unless ldp_source.new?
@@ -40,6 +40,10 @@ module ActiveFedora #:nodoc:
40
40
  class AssociationNotFoundError < ConfigurationError #:nodoc:
41
41
  end
42
42
 
43
+ # Raised when an object is loaded from Fedora by an incompatible class
44
+ class ModelMismatch < ActiveFedoraError
45
+ end
46
+
43
47
  # This error is raised when trying to destroy a parent instance in N:1 or 1:1 associations
44
48
  # (has_many, has_one) when there is at least 1 child associated instance.
45
49
  # ex: if @project.tasks.size > 0, DeleteRestrictionError will be raised when trying to destroy @project
@@ -1,3 +1,6 @@
1
+ require 'faraday'
2
+ require 'faraday-encoding'
3
+
1
4
  module ActiveFedora
2
5
  class Fedora
3
6
  class << self
@@ -23,11 +26,15 @@ module ActiveFedora
23
26
  end
24
27
 
25
28
  def host
26
- @config[:url]
29
+ @config[:url].sub(/\/$/, BLANK)
27
30
  end
28
31
 
29
32
  def base_path
30
- @config[:base_path] || '/'
33
+ @config[:base_path] || SLASH
34
+ end
35
+
36
+ def base_uri
37
+ host + base_path.sub(/\/$/, BLANK)
31
38
  end
32
39
 
33
40
  def user
@@ -42,6 +49,10 @@ module ActiveFedora
42
49
  @config[:ssl]
43
50
  end
44
51
 
52
+ def request_options
53
+ @config[:request]
54
+ end
55
+
45
56
  def connection
46
57
  @connection ||= begin
47
58
  build_connection
@@ -79,9 +90,12 @@ module ActiveFedora
79
90
  def authorized_connection
80
91
  options = {}
81
92
  options[:ssl] = ssl_options if ssl_options
82
- connection = Faraday.new(host, options)
83
- connection.basic_auth(user, password)
84
- connection
93
+ options[:request] = request_options if request_options
94
+ Faraday.new(host, options) do |conn|
95
+ conn.response :encoding # use Faraday::Encoding middleware
96
+ conn.adapter Faraday.default_adapter # net/http
97
+ conn.basic_auth(user, password)
98
+ end
85
99
  end
86
100
 
87
101
  def validate_options
@@ -49,6 +49,11 @@ module ActiveFedora::File::Attributes
49
49
  created && created.first
50
50
  end
51
51
 
52
+ def modified_date
53
+ modified = metadata.attributes["http://fedora.info/definitions/v4/repository#lastModified"]
54
+ modified && modified.first
55
+ end
56
+
52
57
  private
53
58
 
54
59
  # Fcrepo4.digest was used by Fedora < 4.3, but it was removed
@@ -31,6 +31,7 @@ module ActiveFedora
31
31
  # @yieldparam [File] self the newly created file
32
32
  def initialize(identifier = nil, &_block)
33
33
  identifier = identifier.delete(:id) if identifier.is_a? Hash
34
+ identifier = identifier.uri if identifier.respond_to? :uri
34
35
  run_callbacks(:initialize) do
35
36
  case identifier
36
37
  when nil, ::RDF::URI
@@ -39,7 +40,7 @@ module ActiveFedora
39
40
  id = ActiveFedora::Associations::IDComposite.new([identifier], translate_uri_to_id).first
40
41
  @ldp_source = build_ldp_resource id
41
42
  else
42
- raise "The first argument to #{self} must be a Hash, String or RDF::URI. You provided a #{uri.class}"
43
+ raise "The first argument to #{self} must be a Hash, String or RDF::URI. You provided a #{identifier.class}"
43
44
  end
44
45
 
45
46
  @attributes = {}.with_indifferent_access
@@ -139,8 +140,7 @@ module ActiveFedora
139
140
  end
140
141
 
141
142
  # serializes any changed data into the content field
142
- def serialize!
143
- end
143
+ def serialize!; end
144
144
 
145
145
  def to_solr(solr_doc = {}, _opts = {})
146
146
  solr_doc
@@ -99,7 +99,7 @@ module ActiveFedora
99
99
  def load_fedora_config
100
100
  return @fedora_config unless @fedora_config.empty?
101
101
  @fedora_config_path = config_path(:fedora)
102
- ActiveFedora::Base.logger.info("ActiveFedora: loading fedora config from #{::File.expand_path(@fedora_config_path)}") if ActiveFedora::Base.logger
102
+ ActiveFedora::Base.logger.info("ActiveFedora: loading fedora config from #{::File.expand_path(@fedora_config_path)}")
103
103
 
104
104
  begin
105
105
  config_erb = ERB.new(IO.read(@fedora_config_path)).result(binding)
@@ -108,7 +108,7 @@ module ActiveFedora
108
108
  end
109
109
 
110
110
  begin
111
- fedora_yml = YAML.load(config_erb)
111
+ fedora_yml = YAML.safe_load(config_erb, [], [], true) # allow YAML aliases
112
112
  rescue Psych::SyntaxError => e
113
113
  raise "fedora.yml was found, but could not be parsed. " \
114
114
  "Error #{e.message}"
@@ -124,7 +124,7 @@ module ActiveFedora
124
124
  return @solr_config unless @solr_config.empty?
125
125
  @solr_config_path = config_path(:solr)
126
126
 
127
- ActiveFedora::Base.logger.info "ActiveFedora: loading solr config from #{::File.expand_path(@solr_config_path)}" if ActiveFedora::Base.logger
127
+ ActiveFedora::Base.logger.info "ActiveFedora: loading solr config from #{::File.expand_path(@solr_config_path)}"
128
128
  begin
129
129
  config_erb = ERB.new(IO.read(@solr_config_path)).result(binding)
130
130
  rescue StandardError
@@ -132,7 +132,7 @@ module ActiveFedora
132
132
  end
133
133
 
134
134
  begin
135
- solr_yml = YAML.load(config_erb)
135
+ solr_yml = YAML.safe_load(config_erb, [], [], true) # allow YAML aliases
136
136
  rescue StandardError
137
137
  raise("solr.yml was found, but could not be parsed.\n")
138
138
  end
@@ -188,7 +188,7 @@ module ActiveFedora
188
188
  # Last choice, check for the default config file
189
189
  config_path = ::File.join(ActiveFedora.root, "config", "#{config_type}.yml")
190
190
  if ::File.file? config_path
191
- ActiveFedora::Base.logger.warn "Using the default #{config_type}.yml that comes with active-fedora. If you want to override this, pass the path to #{config_type}.yml to ActiveFedora - ie. ActiveFedora.init(:#{config_type}_config_path => '/path/to/#{config_type}.yml') - or set Rails.root and put #{config_type}.yml into \#{Rails.root}/config." if ActiveFedora::Base.logger
191
+ ActiveFedora::Base.logger.warn "Using the default #{config_type}.yml that comes with active-fedora. If you want to override this, pass the path to #{config_type}.yml to ActiveFedora - ie. ActiveFedora.init(:#{config_type}_config_path => '/path/to/#{config_type}.yml') - or set Rails.root and put #{config_type}.yml into \#{Rails.root}/config."
192
192
  return config_path
193
193
  else
194
194
  raise ConfigurationError, "Couldn't load #{config_type} config file!"
@@ -0,0 +1,120 @@
1
+ module ActiveFedora
2
+ ##
3
+ # IO like object for reading Fedora files.
4
+ # Use ActiveFedora::FileIO.new(fedora_file) to create one. You can then call
5
+ # read on it or use it with IO.copy_stream and the like.
6
+ #
7
+ # @Note The stream will always be in binmode and return ASCII-8BIT content.
8
+ class FileIO
9
+ attr_reader :pos
10
+
11
+ ##
12
+ # @param [ActiveFedora::File] the file which is wrapped in this IO object.
13
+ def initialize(fedora_file)
14
+ @fedora_file = fedora_file
15
+ @closed = false
16
+ rewind # this initialises various variables
17
+ end
18
+
19
+ def size
20
+ @fedora_file.size
21
+ end
22
+
23
+ alias length size
24
+
25
+ def binmode
26
+ # Do nothing, just return self. The stream is essentially always in binmode.
27
+ self
28
+ end
29
+
30
+ def binmode?
31
+ true
32
+ end
33
+
34
+ ##
35
+ # Read bytes from the file. See IO.read for more information.
36
+ # @param [Integer] the number of bytes to read. If nil or omitted, the
37
+ # entire contents will be read.
38
+ # @param [String] a string in which the contents are read. Can be omitted.
39
+ # @return [String] the read bytes. If number of bytes to read was not
40
+ # specified then always returns a String, possibly an empty
41
+ # one. If number of bytes was specified then the returned
42
+ # string will always be at least one byte long. If no bytes
43
+ # are left in the file returns nil instead.
44
+ def read(amount = nil, buf = nil)
45
+ raise(IOError, "closed stream") if @closed
46
+
47
+ buf ||= ''.force_encoding("ASCII-8BIT")
48
+ buf.clear
49
+
50
+ if amount.nil?
51
+ read_to_buf(nil, buf) # read the entire file, returns buf
52
+ elsif amount < 0
53
+ raise(ArgumentError, "negative length #{amount} given")
54
+ elsif amount.zero?
55
+ ''
56
+ else
57
+ read_to_buf(amount, buf)
58
+ # if amount was specified but we reached eof before reading anything
59
+ # then we must return nil
60
+ buf.empty? ? nil : buf
61
+ end
62
+ end
63
+
64
+ ##
65
+ # Rewinds the io object to the beginning. Read will return bytes from the
66
+ # start of the file again.
67
+ def rewind
68
+ raise(IOError, "closed stream") if @closed
69
+ @pos = 0
70
+ @buffer = nil
71
+ @stream_fiber = Fiber.new do
72
+ @fedora_file.stream.each do |chunk|
73
+ Fiber.yield chunk
74
+ end
75
+ @stream_fiber = nil
76
+ # last value from Fiber is the return value of the block which should be nil
77
+ end
78
+ 0
79
+ end
80
+
81
+ ##
82
+ # Closes the file. No further action can be taken on the file.
83
+ def close
84
+ @closed = true
85
+ @stream_fiber = nil
86
+ nil
87
+ end
88
+
89
+ private
90
+
91
+ def read_to_buf(amount, buf)
92
+ while (amount.nil? || buf.length < amount) && fill_buffer
93
+ buf << consume_buffer(amount.nil? ? nil : (amount - buf.length))
94
+ end
95
+ buf
96
+ end
97
+
98
+ def consume_buffer(count = nil)
99
+ if count.nil? || count >= @buffer.length
100
+ @pos += @buffer.length
101
+ @buffer .tap do
102
+ @buffer = nil
103
+ end
104
+ else
105
+ @buffer.slice!(0, count) .tap do |slice|
106
+ @pos += slice.length
107
+ end
108
+ end
109
+ end
110
+
111
+ def fill_buffer
112
+ return true if @buffer.present?
113
+ # Ruby Net library doesn't seem to like it if we modify the returned
114
+ # chunk in any way, hence dup.
115
+ @buffer = @stream_fiber.try(:resume).try(:dup)
116
+ @buffer.try(:force_encoding, 'ASCII-8BIT')
117
+ !@buffer.nil?
118
+ end
119
+ end
120
+ end
@@ -2,26 +2,57 @@ module ActiveFedora
2
2
  class FixityService
3
3
  extend ActiveSupport::Concern
4
4
 
5
- attr_accessor :target, :response
5
+ attr_accessor :target
6
6
 
7
- # @param [String, RDF::URI] target url for a Fedora resource
7
+ # @param [String, RDF::URI] target url for a Fedora resource.
8
8
  def initialize(target)
9
9
  raise ArgumentError, 'You must provide a uri' unless target
10
10
  @target = target.to_s
11
11
  end
12
12
 
13
- # Executes a fixity check on Fedora and saves the Faraday::Response.
13
+ def response
14
+ @response ||= fixity_response_from_fedora
15
+ end
16
+
17
+ # For backwards compat, check always insists on doing a new request.
18
+ # you might want verified? instead which uses a cached request.
14
19
  # @return true or false
15
20
  def check
16
- @response = fixity_response_from_fedora
21
+ @response = nil
22
+ verified?
23
+ end
24
+
25
+ # Executes a fixity check on Fedora
26
+ # @return true or false
27
+ def verified?
17
28
  status.include?(success)
18
29
  end
19
30
 
31
+ # An array of 1 or more literals reported by Fedora.
32
+ # See 'success' for which one indicates fixity check is good.
20
33
  def status
21
34
  fixity_graph.query(predicate: premis_status_predicate).map(&:object) +
22
35
  fixity_graph.query(predicate: fedora_status_predicate).map(&:object)
23
36
  end
24
37
 
38
+ # the currently calculated checksum, as a string URI, like
39
+ # "urn:sha1:09a848b79f86f3a4f3f301b8baafde455d6f8e0e"
40
+ def expected_message_digest
41
+ fixity_graph.query(predicate: ::RDF::Vocab::PREMIS.hasMessageDigest).first.try(:object).try(:to_s)
42
+ end
43
+
44
+ # integer, as reported by fedora. bytes maybe?
45
+ def expected_size
46
+ fixity_graph.query(predicate: ::RDF::Vocab::PREMIS.hasSize).first.try(:object).try(:to_s).try(:to_i)
47
+ end
48
+
49
+ # Fedora response as an ::RDF::Graph object. Public API, so consumers
50
+ # can do with it what they will, especially if future fedora versions
51
+ # add more things to it.
52
+ def response_graph
53
+ fixity_graph
54
+ end
55
+
25
56
  private
26
57
 
27
58
  def premis_status_predicate
@@ -46,7 +77,7 @@ module ActiveFedora
46
77
  end
47
78
 
48
79
  def fixity_graph
49
- ::RDF::Graph.new << ::RDF::Reader.for(:ttl).new(response.body)
80
+ @fixity_graph ||= ::RDF::Graph.new << ::RDF::Reader.for(:ttl).new(response.body)
50
81
  end
51
82
 
52
83
  # See https://jira.duraspace.org/browse/FCREPO-1247
@@ -7,7 +7,6 @@ module ActiveFedora::Indexers
7
7
  self
8
8
  end
9
9
 
10
- def index(_)
11
- end
10
+ def index(_); end
12
11
  end
13
12
  end