active-fedora 8.7.0 → 9.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (242) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +8 -15
  3. data/Gemfile +5 -5
  4. data/History.txt +0 -80
  5. data/README.md +1 -0
  6. data/Rakefile +0 -3
  7. data/active-fedora.gemspec +8 -7
  8. data/config/fedora.yml +5 -4
  9. data/config/predicate_mappings.yml +5 -5
  10. data/gemfiles/rails4.1.gemfile +10 -0
  11. data/gemfiles/rails4.2.beta.gemfile +10 -0
  12. data/lib/active_fedora.rb +151 -117
  13. data/lib/active_fedora/associations.rb +47 -15
  14. data/lib/active_fedora/associations/association.rb +29 -8
  15. data/lib/active_fedora/associations/association_scope.rb +5 -5
  16. data/lib/active_fedora/associations/belongs_to_association.rb +20 -63
  17. data/lib/active_fedora/associations/builder/association.rb +61 -25
  18. data/lib/active_fedora/associations/builder/belongs_to.rb +7 -94
  19. data/lib/active_fedora/associations/builder/collection_association.rb +11 -43
  20. data/lib/active_fedora/associations/builder/contains.rb +28 -0
  21. data/lib/active_fedora/associations/builder/has_and_belongs_to_many.rb +13 -3
  22. data/lib/active_fedora/associations/builder/has_many.rb +16 -10
  23. data/lib/active_fedora/associations/builder/property.rb +14 -0
  24. data/lib/active_fedora/associations/builder/singular_association.rb +14 -18
  25. data/lib/active_fedora/associations/builder/singular_property.rb +12 -0
  26. data/lib/active_fedora/associations/collection_association.rb +57 -80
  27. data/lib/active_fedora/associations/contains_association.rb +50 -0
  28. data/lib/active_fedora/associations/has_and_belongs_to_many_association.rb +44 -24
  29. data/lib/active_fedora/associations/has_many_association.rb +46 -14
  30. data/lib/active_fedora/associations/rdf.rb +86 -0
  31. data/lib/active_fedora/associations/singular_association.rb +4 -8
  32. data/lib/active_fedora/associations/singular_rdf.rb +15 -0
  33. data/lib/active_fedora/attached_files.rb +195 -0
  34. data/lib/active_fedora/attribute_methods.rb +122 -0
  35. data/lib/active_fedora/attribute_methods/dirty.rb +16 -0
  36. data/lib/active_fedora/attribute_methods/read.rb +61 -0
  37. data/lib/active_fedora/attribute_methods/write.rb +47 -0
  38. data/lib/active_fedora/attributes.rb +93 -44
  39. data/lib/active_fedora/attributes/primary_key.rb +12 -0
  40. data/lib/active_fedora/autosave_association.rb +2 -2
  41. data/lib/active_fedora/base.rb +16 -15
  42. data/lib/active_fedora/callbacks.rb +7 -7
  43. data/lib/active_fedora/change_set.rb +34 -0
  44. data/lib/active_fedora/cleaner.rb +62 -0
  45. data/lib/active_fedora/config.rb +4 -22
  46. data/lib/active_fedora/core.rb +173 -99
  47. data/lib/active_fedora/datastream.rb +4 -117
  48. data/lib/active_fedora/datastreams.rb +2 -263
  49. data/lib/active_fedora/datastreams/nokogiri_datastreams.rb +28 -51
  50. data/lib/active_fedora/{datastream_attribute.rb → delegated_attribute.rb} +57 -26
  51. data/lib/active_fedora/errors.rb +61 -0
  52. data/lib/active_fedora/fedora.rb +19 -0
  53. data/lib/active_fedora/fedora_attributes.rb +58 -26
  54. data/lib/active_fedora/file.rb +318 -0
  55. data/lib/active_fedora/file_configurator.rb +32 -32
  56. data/lib/active_fedora/file_path_builder.rb +24 -0
  57. data/lib/active_fedora/files_hash.rb +82 -0
  58. data/lib/active_fedora/fixity_service.rb +40 -0
  59. data/lib/active_fedora/indexing.rb +55 -82
  60. data/lib/active_fedora/indexing_service.rb +70 -0
  61. data/lib/active_fedora/ldp_resource.rb +26 -0
  62. data/lib/active_fedora/loadable_from_json.rb +112 -0
  63. data/lib/active_fedora/model.rb +5 -19
  64. data/lib/active_fedora/nested_attributes.rb +5 -6
  65. data/lib/active_fedora/nom_datastream.rb +15 -25
  66. data/lib/active_fedora/om_datastream.rb +26 -197
  67. data/lib/active_fedora/persistence.rb +95 -71
  68. data/lib/active_fedora/predicates.rb +4 -4
  69. data/lib/active_fedora/qualified_dublin_core_datastream.rb +17 -18
  70. data/lib/active_fedora/querying.rb +3 -4
  71. data/lib/active_fedora/railtie.rb +3 -6
  72. data/lib/active_fedora/rdf.rb +3 -1
  73. data/lib/active_fedora/rdf/datastream_indexing.rb +11 -0
  74. data/lib/active_fedora/rdf/fcrepo.rb +10 -324
  75. data/lib/active_fedora/rdf/indexing.rb +20 -16
  76. data/lib/active_fedora/rdf/ldp.rb +6 -0
  77. data/lib/active_fedora/rdf/ntriples_rdf_datastream.rb +1 -1
  78. data/lib/active_fedora/rdf/persistence.rb +5 -6
  79. data/lib/active_fedora/rdf/rdf_datastream.rb +44 -37
  80. data/lib/active_fedora/rdf/rdfxml_datastream.rb +13 -0
  81. data/lib/active_fedora/rdf/rels_ext.rb +26 -0
  82. data/lib/active_fedora/reflection.rb +256 -199
  83. data/lib/active_fedora/relation.rb +18 -6
  84. data/lib/active_fedora/relation/finder_methods.rb +69 -38
  85. data/lib/active_fedora/relation/query_methods.rb +7 -3
  86. data/lib/active_fedora/rspec_matchers/belong_to_associated_active_fedora_object_matcher.rb +7 -7
  87. data/lib/active_fedora/rspec_matchers/have_many_associated_active_fedora_objects_matcher.rb +8 -8
  88. data/lib/active_fedora/rspec_matchers/have_predicate_matcher.rb +9 -11
  89. data/lib/active_fedora/simple_datastream.rb +18 -13
  90. data/lib/active_fedora/solr_instance_loader.rb +18 -38
  91. data/lib/active_fedora/solr_service.rb +37 -20
  92. data/lib/active_fedora/sparql_insert.rb +45 -0
  93. data/lib/active_fedora/test_support.rb +1 -22
  94. data/lib/active_fedora/version.rb +1 -1
  95. data/lib/active_fedora/versionable.rb +90 -0
  96. data/lib/active_fedora/with_metadata.rb +37 -0
  97. data/lib/active_fedora/with_metadata/metadata_node.rb +70 -0
  98. data/lib/generators/active_fedora/config/config_generator.rb +0 -1
  99. data/lib/generators/active_fedora/config/solr/solr_generator.rb +1 -1
  100. data/lib/generators/active_fedora/model/model_generator.rb +5 -5
  101. data/lib/generators/active_fedora/model/templates/datastream_spec.rb.erb +1 -1
  102. data/lib/generators/active_fedora/model/templates/model_spec.rb.erb +2 -2
  103. data/lib/tasks/active_fedora_dev.rake +21 -27
  104. data/spec/config_helper.rb +1 -1
  105. data/spec/fixtures/mixed_rdf_descMetadata.nt +6 -6
  106. data/spec/fixtures/rails_root/config/predicate_mappings.yml +3 -19
  107. data/spec/fixtures/solr_rdf_descMetadata.nt +6 -6
  108. data/spec/integration/associations_spec.rb +133 -153
  109. data/spec/integration/attached_files_spec.rb +164 -0
  110. data/spec/integration/attributes_spec.rb +73 -12
  111. data/spec/integration/autosave_association_spec.rb +3 -3
  112. data/spec/integration/base_spec.rb +57 -351
  113. data/spec/integration/belongs_to_association_spec.rb +86 -76
  114. data/spec/integration/bug_spec.rb +3 -3
  115. data/spec/integration/collection_association_spec.rb +4 -4
  116. data/spec/integration/complex_rdf_datastream_spec.rb +54 -56
  117. data/spec/integration/delete_all_spec.rb +18 -15
  118. data/spec/integration/eradicate_spec.rb +54 -0
  119. data/spec/integration/fedora_solr_sync_spec.rb +7 -5
  120. data/spec/integration/field_to_solr_name_spec.rb +5 -5
  121. data/spec/integration/file_fixity_spec.rb +40 -0
  122. data/spec/integration/file_spec.rb +122 -0
  123. data/spec/integration/full_featured_model_spec.rb +53 -63
  124. data/spec/integration/has_and_belongs_to_many_associations_spec.rb +141 -114
  125. data/spec/integration/has_many_associations_spec.rb +142 -64
  126. data/spec/integration/json_serialization_spec.rb +50 -8
  127. data/spec/integration/model_spec.rb +12 -29
  128. data/spec/integration/nested_attribute_spec.rb +28 -20
  129. data/spec/integration/ntriples_datastream_spec.rb +60 -57
  130. data/spec/integration/om_datastream_spec.rb +51 -140
  131. data/spec/integration/rdf_nested_attributes_spec.rb +16 -14
  132. data/spec/integration/relation_delegation_spec.rb +7 -9
  133. data/spec/integration/relation_spec.rb +9 -7
  134. data/spec/integration/scoped_query_spec.rb +26 -26
  135. data/spec/integration/solr_instance_loader_spec.rb +69 -53
  136. data/spec/integration/solr_service_spec.rb +12 -73
  137. data/spec/integration/versionable_spec.rb +477 -0
  138. data/spec/integration/with_metadata_spec.rb +52 -0
  139. data/spec/samples/hydra-mods_article_datastream.rb +10 -6
  140. data/spec/samples/models/mods_article.rb +6 -2
  141. data/spec/samples/oral_history_sample.xml +1 -1
  142. data/spec/samples/oral_history_xml.xml +1 -1
  143. data/spec/samples/special_thing.rb +3 -3
  144. data/spec/spec_helper.rb +22 -12
  145. data/spec/support/an_active_model.rb +3 -7
  146. data/spec/unit/active_fedora_spec.rb +20 -17
  147. data/spec/unit/attached_files_spec.rb +203 -0
  148. data/spec/unit/attributes_spec.rb +286 -207
  149. data/spec/unit/base_active_model_spec.rb +8 -8
  150. data/spec/unit/base_datastream_management_spec.rb +11 -24
  151. data/spec/unit/base_extra_spec.rb +17 -67
  152. data/spec/unit/base_spec.rb +163 -428
  153. data/spec/unit/builder/has_and_belongs_to_many_spec.rb +2 -2
  154. data/spec/unit/callback_spec.rb +38 -23
  155. data/spec/unit/change_set_spec.rb +46 -0
  156. data/spec/unit/code_configurator_spec.rb +5 -5
  157. data/spec/unit/config_spec.rb +9 -14
  158. data/spec/unit/core_spec.rb +59 -8
  159. data/spec/unit/file_configurator_spec.rb +55 -53
  160. data/spec/unit/file_path_builder_spec.rb +18 -0
  161. data/spec/unit/file_spec.rb +221 -0
  162. data/spec/unit/files_hash_spec.rb +53 -0
  163. data/spec/unit/fixity_service_spec.rb +34 -0
  164. data/spec/unit/has_and_belongs_to_many_association_spec.rb +134 -0
  165. data/spec/unit/has_many_association_spec.rb +51 -0
  166. data/spec/unit/indexing_service_spec.rb +23 -0
  167. data/spec/unit/indexing_spec.rb +26 -0
  168. data/spec/unit/inheritance_spec.rb +9 -10
  169. data/spec/unit/model_spec.rb +15 -33
  170. data/spec/unit/nom_datastream_spec.rb +13 -10
  171. data/spec/unit/ntriples_datastream_spec.rb +81 -96
  172. data/spec/unit/om_datastream_spec.rb +137 -227
  173. data/spec/unit/persistence_spec.rb +28 -34
  174. data/spec/unit/predicates_spec.rb +29 -29
  175. data/spec/unit/property_spec.rb +1 -3
  176. data/spec/unit/qualified_dublin_core_datastream_spec.rb +27 -32
  177. data/spec/unit/query_spec.rb +116 -149
  178. data/spec/unit/rdf_datastream_spec.rb +25 -43
  179. data/spec/unit/rdf_resource_datastream_spec.rb +24 -123
  180. data/spec/unit/{rdfxml_rdf_datastream_spec.rb → rdfxml_datastream_spec.rb} +21 -25
  181. data/spec/unit/readonly_spec.rb +23 -0
  182. data/spec/unit/rspec_matchers/belong_to_associated_active_fedora_object_matcher_spec.rb +6 -6
  183. data/spec/unit/rspec_matchers/have_many_associated_active_fedora_objects_matcher_spec.rb +6 -6
  184. data/spec/unit/rspec_matchers/have_predicate_matcher_spec.rb +6 -6
  185. data/spec/unit/serializers_spec.rb +1 -1
  186. data/spec/unit/simple_datastream_spec.rb +12 -23
  187. data/spec/unit/solr_config_options_spec.rb +14 -17
  188. data/spec/unit/solr_service_spec.rb +38 -77
  189. data/spec/unit/sparql_insert_spec.rb +32 -0
  190. data/spec/unit/validations_spec.rb +8 -11
  191. metadata +96 -121
  192. data/lib/active_fedora/auditable.rb +0 -9
  193. data/lib/active_fedora/content_model.rb +0 -70
  194. data/lib/active_fedora/datastream_collections.rb +0 -302
  195. data/lib/active_fedora/datastream_hash.rb +0 -35
  196. data/lib/active_fedora/digital_object.rb +0 -55
  197. data/lib/active_fedora/fixture_exporter.rb +0 -33
  198. data/lib/active_fedora/fixture_loader.rb +0 -48
  199. data/lib/active_fedora/rdf/identifiable.rb +0 -66
  200. data/lib/active_fedora/rdf/project_hydra.rb +0 -12
  201. data/lib/active_fedora/rdf/rdfxml_rdf_datastream.rb +0 -13
  202. data/lib/active_fedora/rdf_xml_writer.rb +0 -49
  203. data/lib/active_fedora/relationship_graph.rb +0 -101
  204. data/lib/active_fedora/reload_on_save.rb +0 -16
  205. data/lib/active_fedora/rels_ext_datastream.rb +0 -100
  206. data/lib/active_fedora/rspec_matchers/match_fedora_datastream_matcher.rb +0 -41
  207. data/lib/active_fedora/rubydora_connection.rb +0 -35
  208. data/lib/active_fedora/semantic_node.rb +0 -164
  209. data/lib/active_fedora/service_definitions.rb +0 -88
  210. data/lib/active_fedora/sharding.rb +0 -58
  211. data/lib/active_fedora/solr_digital_object.rb +0 -68
  212. data/lib/active_fedora/unsaved_digital_object.rb +0 -58
  213. data/lib/generators/active_fedora/config/fedora/fedora_generator.rb +0 -12
  214. data/lib/generators/active_fedora/config/fedora/templates/fedora.yml +0 -38
  215. data/lib/generators/active_fedora/config/fedora/templates/fedora_conf/conf/development/fedora.fcfg +0 -953
  216. data/lib/generators/active_fedora/config/fedora/templates/fedora_conf/conf/test/fedora.fcfg +0 -953
  217. data/lib/tasks/active_fedora.rake +0 -83
  218. data/spec/fixtures/sharded_fedora.yml +0 -11
  219. data/spec/integration/auditable_spec.rb +0 -29
  220. data/spec/integration/datastream_collections_spec.rb +0 -127
  221. data/spec/integration/datastream_spec.rb +0 -90
  222. data/spec/integration/datastreams_spec.rb +0 -173
  223. data/spec/integration/load_from_solr_spec.rb +0 -66
  224. data/spec/integration/rels_ext_datastream_spec.rb +0 -82
  225. data/spec/support/mock_fedora.rb +0 -44
  226. data/spec/unit/content_model_spec.rb +0 -86
  227. data/spec/unit/datastream_collections_spec.rb +0 -420
  228. data/spec/unit/datastream_spec.rb +0 -83
  229. data/spec/unit/datastreams_spec.rb +0 -243
  230. data/spec/unit/has_and_belongs_to_many_collection_spec.rb +0 -96
  231. data/spec/unit/has_many_collection_spec.rb +0 -35
  232. data/spec/unit/rdf_vocab_spec.rb +0 -30
  233. data/spec/unit/rdf_xml_writer_spec.rb +0 -63
  234. data/spec/unit/relationship_graph_spec.rb +0 -115
  235. data/spec/unit/reload_on_save_spec.rb +0 -24
  236. data/spec/unit/rels_ext_datastream_spec.rb +0 -170
  237. data/spec/unit/rspec_matchers/match_fedora_datastream_matcher_spec.rb +0 -44
  238. data/spec/unit/rubydora_connection_spec.rb +0 -12
  239. data/spec/unit/semantic_node_spec.rb +0 -112
  240. data/spec/unit/service_definitions_spec.rb +0 -63
  241. data/spec/unit/solr_digital_object_spec.rb +0 -97
  242. data/spec/unit/unsaved_digital_object_spec.rb +0 -48
@@ -1,122 +1,9 @@
1
1
  module ActiveFedora
2
+ class Datastream < File
3
+ extend Deprecation
2
4
 
3
- # This class represents a Fedora datastream
4
- class Datastream < Rubydora::Datastream
5
-
6
- attr_writer :digital_object
7
- attr_accessor :last_modified
8
-
9
- # @param digital_object [DigitalObject] the digital object that this object belongs to
10
- # @param dsid [String] the datastream id, if this is nil, a datastream id will be generated.
11
- # @param options [Hash]
12
- # @option options [String,IO] :content the content for the datastream
13
- # @option options [String] :dsLabel label for the datastream
14
- # @option options [String] :dsLocation location for an external or redirect datastream
15
- # @option options [String] :controlGroup a controlGroup for the datastream
16
- # @option options [String] :mimeType the mime-type of the content
17
- # @option options [String] :prefix the prefix for the auto-generated DSID (not to be confused with the solr prefix)
18
- # @option options [Boolean] :versionable is the datastream versionable
19
- def initialize(digital_object=nil, dsid=nil, options={})
20
- prefix = options.delete(:prefix)
21
- dsid = nil if dsid == ''
22
- dsid ||= generate_dsid(digital_object, prefix || "DS")
23
- super(digital_object, dsid, options)
24
- end
25
-
26
- alias_method :realLabel, :label
27
-
28
- def label
29
- Array(realLabel).first
30
- end
31
- alias_method :dsLabel, :label
32
-
33
- def inspect
34
- "#<#{self.class} @pid=\"#{digital_object ? pid : nil}\" @dsid=\"#{dsid}\" @controlGroup=\"#{controlGroup}\" changed=\"#{changed?}\" @mimeType=\"#{mimeType}\" >"
35
- end
36
-
37
- #compatibility method for rails' url generators. This method will
38
- #urlescape escape dots, which are apparently
39
- #invalid characters in a dsid.
40
- def to_param
41
- dsid.gsub(/\./, '%2e')
42
- end
43
-
44
- # @abstract Override this in your concrete datastream class.
45
- # @return [boolean] does this datastream contain metadata (not file data)
46
- def metadata?
47
- false
48
- end
49
-
50
- def save
51
- super
52
- self
53
- end
54
-
55
- def create
56
- super
57
- self
58
- end
59
-
60
- # Freeze datastreams such that they can be loaded from Fedora, but can't be changed
61
- def freeze
62
- @frozen = true
63
- end
64
-
65
- def frozen?
66
- !!@frozen
67
- end
68
-
69
- # serializes any changed data into the content field
70
- def serialize!
71
- end
72
-
73
- def solrize_profile # :nodoc:
74
- profile_hash = {}
75
- profile.each_pair do |property,value|
76
- if property =~ /Date/
77
- value = Time.parse(value) unless value.is_a?(Time)
78
- value = value.xmlschema
79
- end
80
- profile_hash[property] = value
81
- end
82
- profile_hash
83
- end
84
-
85
- def profile_from_hash(profile_hash)
86
- profile_hash.each_pair do |key,value|
87
- profile[key] = value
88
- end
89
- end
90
-
91
- def to_solr(solr_doc = Hash.new)
92
- solr_doc
5
+ def self.inherited(child)
6
+ Deprecation.warn child, "ActiveFedora::Datastream is deprecated and will be removed in active-fedora 10.0. Use ActiveFedora::File instead"
93
7
  end
94
-
95
- protected
96
-
97
- # return a valid dsid that is not currently in use. Uses a prefix (default "DS") and an auto-incrementing integer
98
- # Example: if there are already datastreams with IDs DS1 and DS2, this method will return DS3. If you specify FOO as the prefix, it will return FOO1.
99
- def generate_dsid(digital_object, prefix)
100
- return unless digital_object
101
- matches = digital_object.datastreams.keys.map {|d| data = /^#{prefix}(\d+)$/.match(d); data && data[1].to_i}.compact
102
- val = matches.empty? ? 1 : matches.max + 1
103
- format_dsid(prefix, val)
104
- end
105
-
106
- ### Provided so that an application can override how generated pids are formatted (e.g DS01 instead of DS1)
107
- def format_dsid(prefix, suffix)
108
- sprintf("%s%i", prefix,suffix)
109
- end
110
-
111
- # The string to prefix all solr fields with. Override this method if you want
112
- # a prefix other than the default
113
- def prefix
114
- raise RuntimeError, "to_solr requires the dsid to be set" unless dsid
115
- "#{dsid.underscore}__"
116
- end
117
-
118
- end
119
-
120
- class DatastreamConcurrencyException < Exception # :nodoc:
121
8
  end
122
9
  end
@@ -1,269 +1,8 @@
1
1
  module ActiveFedora
2
2
  module Datastreams
3
- extend ActiveSupport::Concern
3
+ extend ActiveSupport::Autoload
4
+ # extend Deprecation
4
5
 
5
6
  autoload :NokogiriDatastreams, 'active_fedora/datastreams/nokogiri_datastreams'
6
-
7
- included do
8
- class_attribute :ds_specs
9
- self.ds_specs = {'RELS-EXT'=> {:type=> ActiveFedora::RelsExtDatastream, :label=>"Fedora Object-to-Object Relationship Metadata", :block=>nil}}
10
-
11
- before_save :add_disseminator_location_to_datastreams
12
- #before_save :serialize_datastreams
13
- end
14
-
15
- module ClassMethods
16
- def inherited(kls) #:nodoc:
17
- super
18
- kls.ds_specs = kls.ds_specs.dup
19
- end
20
- end
21
-
22
- def ds_specs
23
- self.class.ds_specs
24
- end
25
-
26
- def serialize_datastreams
27
- datastreams.each {|k, ds| ds.serialize! }
28
- end
29
-
30
- # Adds the disseminator location to the datastream after the pid has been established
31
- def add_disseminator_location_to_datastreams
32
- self.ds_specs.each do |name,ds_config|
33
- ds = datastreams[name]
34
- if ds && ds.controlGroup == 'E' && ds_config[:disseminator].present?
35
- ds.dsLocation= "#{ActiveFedora.config_for_environment[:url]}/objects/#{pid}/methods/#{ds_config[:disseminator]}"
36
- end
37
- end
38
- true
39
- end
40
-
41
- #
42
- # Datastream Management
43
- #
44
-
45
- # Returns all known datastreams for the object. If the object has been
46
- # saved to fedora, the persisted datastreams will be included.
47
- # Datastreams that have been modified in memory are given preference over
48
- # the copy in Fedora.
49
- def datastreams
50
- @datastreams ||= DatastreamHash.new(self)
51
- end
52
-
53
- def configure_datastream(ds, ds_spec=nil)
54
- ds_spec ||= self.ds_specs[ds.dsid]
55
- if ds_spec
56
- ds.model = self if ds_spec[:type] == RelsExtDatastream
57
- # If you called has_metadata with a block, pass the block into the Datastream class
58
- if ds_spec[:block].class == Proc
59
- ds_spec[:block].call(ds)
60
- end
61
- end
62
- end
63
-
64
- def datastream_from_spec(ds_spec, name)
65
- inner_object.datastream_object_for name, {}, ds_spec
66
- end
67
-
68
- def load_datastreams
69
- local_ds_specs = self.ds_specs.dup
70
- inner_object.datastreams.each do |dsid, ds|
71
- self.add_datastream(ds)
72
- configure_datastream(datastreams[dsid])
73
- local_ds_specs.delete(dsid)
74
- end
75
- local_ds_specs.each do |name,ds_spec|
76
- ds = datastream_from_spec(ds_spec, name)
77
- self.add_datastream(ds)
78
- configure_datastream(ds, ds_spec)
79
- end
80
- end
81
-
82
- # Adds datastream to the object.
83
- # @return [String] dsid of the added datastream
84
- def add_datastream(datastream, opts={})
85
- datastreams[datastream.dsid] = datastream
86
- datastream.dsid
87
- end
88
-
89
- # @return [Array] all datastreams that return true for `metadata?` and are not Rels-ext
90
- def metadata_streams
91
- datastreams.select { |k, ds| ds.metadata? }.reject { |k, ds| ds.kind_of?(ActiveFedora::RelsExtDatastream) }.values
92
- end
93
-
94
-
95
- # Returns the RELS-EXT Datastream
96
- # Tries to grab from in-memory datastreams first
97
- # Failing that, attempts to load from Fedora and addst to in-memory datastreams
98
- # Failing that, creates a new RelsExtDatastream and adds it to the object
99
- def rels_ext
100
- if !datastreams.has_key?("RELS-EXT")
101
- ds = ActiveFedora::RelsExtDatastream.new(@inner_object,'RELS-EXT')
102
- ds.model = self
103
- add_datastream(ds)
104
- end
105
- return datastreams["RELS-EXT"]
106
- end
107
-
108
- #
109
- # File Management
110
- #
111
-
112
- # Add the given file as a datastream in the object
113
- #
114
- # @param [File] file the file to add
115
- # @param [Hash] opts options: :dsid, :label, :mimeType, :prefix, :checksumType
116
- def add_file_datastream(file, opts={})
117
- label = opts.has_key?(:label) ? opts[:label] : ""
118
- attrs = {:dsLabel => label, :controlGroup => 'M', :blob => file, :prefix=>opts[:prefix]}
119
- if opts.has_key?(:mime_type)
120
- attrs.merge!({:mimeType=>opts[:mime_type]})
121
- elsif opts.has_key?(:mimeType)
122
- attrs.merge!({:mimeType=>opts[:mimeType]})
123
- elsif opts.has_key?(:content_type)
124
- attrs.merge!({:mimeType=>opts[:content_type]})
125
- end
126
- attrs[:checksumType] = opts[:checksumType] if opts[:checksumType]
127
- attrs[:versionable] = opts[:versionable] unless opts[:versionable].nil?
128
- ds = create_datastream(self.class.datastream_class_for_name(opts[:dsid]), opts[:dsid], attrs)
129
- add_datastream(ds).tap do |dsid|
130
- self.class.build_datastream_accessor(dsid) unless respond_to? dsid
131
- end
132
- end
133
-
134
-
135
- # Creates a datastream to be added to the object
136
- # @param type [Class] the class of the datastream object to be created (or a String that can eval to a class)
137
- # @param dsid [String] the dsid of the datastream to be created, or a generated value if false
138
- # @param opts [Hash] the ds options to be assigned to the created object, cf. Rubydora::Datastream.DS_ATTRIBUTES
139
- # @return [ActiveFedora::Datastream] a datastream object of the type indicated in the parameters
140
- def create_datastream(type, dsid, opts={})
141
- klass = type.kind_of?(Class) ? type : type.constantize
142
- raise ArgumentError, "Argument dsid must be of type string" if dsid && !dsid.kind_of?(String)
143
- ds = klass.new(inner_object, dsid, prefix: opts[:prefix])
144
- [:mimeType, :controlGroup, :dsLabel, :dsLocation, :checksumType, :versionable].each do |key|
145
- ds.send("#{key}=".to_sym, opts[key]) unless opts[key].nil?
146
- end
147
- if ds.managed? || ds.inline?
148
- blob = opts[:blob]
149
- if blob
150
- if !ds.mimeType.present?
151
- ##TODO, this is all done by rubydora -- remove
152
- ds.mimeType = blob.respond_to?(:content_type) ? blob.content_type : "application/octet-stream"
153
- end
154
- if !ds.dsLabel.present? && blob.respond_to?(:path)
155
- ds.dsLabel = File.basename(blob.path)
156
- end
157
- end
158
- ds.content = blob || ""
159
- end
160
- ds
161
- end
162
-
163
- module ClassMethods
164
- # @param [String] dsid the datastream id
165
- # @return [Class] the class of the datastream
166
- def datastream_class_for_name(dsid)
167
- ds_specs[dsid] ? ds_specs[dsid].fetch(:type, ActiveFedora::Datastream) : ActiveFedora::Datastream
168
- end
169
-
170
- # This method is used to specify the details of a datastream.
171
- # You can pass the name as the first argument and a hash of options as the second argument
172
- # or you can pass the :name as a value in the args hash. Either way, name is required.
173
- # Note that this method doesn't actually execute the block, but stores it, to be executed
174
- # by any the implementation of the datastream(specified as :type)
175
- #
176
- # @param [Hash] args
177
- # @option args [Class] :type The class that will represent this datastream, should extend ``Datastream''
178
- # @option args [String] :name the handle to refer to this datastream as
179
- # @option args [String] :label sets the fedora label
180
- # @option args [String] :control_group must be one of 'E', 'X', 'M', 'R'
181
- # @option args [String] :disseminator Sets the disseminator location see {#add_disseminator_location_to_datastreams}
182
- # @option args [String] :url
183
- # @option args [Boolean] :autocreate Always create this datastream on new objects
184
- # @option args [Boolean] :versionable Should versioned datastreams be stored
185
- # @yield block executed by some kinds of datastreams
186
- def has_metadata(*args, &block)
187
- @metadata_ds_defaults ||= {
188
- :autocreate => false,
189
- :type=>nil,
190
- :label=>"",
191
- :control_group=>nil,
192
- :disseminator=>"",
193
- :url=>"",
194
- :name=>nil
195
- }
196
- spec_datastream(args, @metadata_ds_defaults, &block)
197
- end
198
-
199
-
200
- # @overload has_file_datastream(name, args)
201
- # Declares a file datastream exists for objects of this type
202
- # @param [String] name
203
- # @param [Hash] args
204
- # @option args :type (ActiveFedora::Datastream) The class the datastream should have
205
- # @option args :label ("File Datastream") The default value to put in the dsLabel field
206
- # @option args :control_group ("M") The type of controlGroup to store the datastream as. Defaults to M
207
- # @option args [Boolean] :autocreate Always create this datastream on new objects
208
- # @option args [Boolean] :versionable Should versioned datastreams be stored
209
- # @overload has_file_datastream(args)
210
- # Declares a file datastream exists for objects of this type
211
- # @param [Hash] args
212
- # @option args :name ("content") The dsid of the datastream
213
- # @option args :type (ActiveFedora::Datastream) The class the datastream should have
214
- # @option args :label ("File Datastream") The default value to put in the dsLabel field
215
- # @option args :control_group ("M") The type of controlGroup to store the datastream as. Defaults to M
216
- # @option args [Boolean] :autocreate Always create this datastream on new objects
217
- # @option args [Boolean] :versionable Should versioned datastreams be stored
218
- def has_file_datastream(*args)
219
- @file_ds_defaults ||= {
220
- :autocreate => false,
221
- :type=>ActiveFedora::Datastream,
222
- :label=>"File Datastream",
223
- :control_group=>"M",
224
- :name=>"content"
225
- }
226
- spec_datastream(args, @file_ds_defaults)
227
- end
228
-
229
- def build_datastream_accessor(dsid)
230
- name = name_for_dsid(dsid)
231
- define_method name do
232
- datastreams[dsid]
233
- end
234
- end
235
-
236
-
237
- private
238
-
239
- # Creates a datastream spec combining the given args and default values
240
- # @param args [Array] either [String, Hash] or [Hash]; the latter must .has_key? :name
241
- # @param defaults [Hash] the default values for the datastream spec
242
- # @yield block executed by some kinds of datastreams
243
- def spec_datastream(args, defaults, &block)
244
- if args.first.is_a? String
245
- name = args.first
246
- args = args[1] || {}
247
- args[:name] = name
248
- else
249
- args = args.first || {}
250
- end
251
- spec = defaults.merge(args.select {|key, value| defaults.has_key? key})
252
- name = spec.delete(:name)
253
- raise ArgumentError, "You must provide a name (dsid) for the datastream" unless name
254
- raise ArgumentError, "You must provide a :type property for the datastream '#{name}'" unless spec[:type]
255
- spec[:versionable] = args[:versionable] if args.has_key? :versionable
256
- spec[:block] = block if block
257
- build_datastream_accessor(name)
258
- ds_specs[name]= spec
259
- end
260
-
261
- ## Given a dsid return a standard name
262
- def name_for_dsid(dsid)
263
- dsid.gsub('-', '_')
264
- end
265
-
266
- end
267
-
268
7
  end
269
8
  end
@@ -5,26 +5,6 @@ module ActiveFedora
5
5
 
6
6
  module ClassMethods
7
7
 
8
- # Create an instance of this class based on xml content
9
- # @param [String, File, Nokogiri::XML::Node] xml the xml content to build from
10
- # @param [ActiveFedora::OmDatastream] tmpl the Datastream object that you are building @default a new instance of this class
11
- # Careful! If you call this from a constructor, be sure to provide something 'ie. self' as the @tmpl. Otherwise, you will get an infinite loop!
12
- def from_xml(xml, tmpl=nil)
13
- tmpl = self.new if tmpl.nil? ## This path is used only for unit testing (e.g. MarpaDCDatastream.from_xml(fixture("data.xml")) )
14
-
15
- if !xml.present?
16
- tmpl.ng_xml = self.xml_template
17
- elsif xml.kind_of? Nokogiri::XML::Node || xml.kind_of?(Nokogiri::XML::Document)
18
- tmpl.ng_xml = xml
19
- else
20
- tmpl.ng_xml = Nokogiri::XML::Document.parse(xml)
21
- end
22
-
23
- tmpl.ng_xml_doesnt_change!
24
-
25
- return tmpl
26
- end
27
-
28
8
  def xml_template
29
9
  Nokogiri::XML::Document.parse("<xml/>")
30
10
  end
@@ -37,11 +17,11 @@ module ActiveFedora
37
17
 
38
18
  def ng_xml
39
19
  @ng_xml ||= begin
40
- if new?
20
+ if new_record?
41
21
  ## Load up the template
42
22
  xml = self.class.xml_template
43
23
  else
44
- xml = Nokogiri::XML::Document.parse(datastream_content)
24
+ xml = Nokogiri::XML::Document.parse(remote_content)
45
25
  end
46
26
  self.class.decorate_ng_xml xml
47
27
  end
@@ -49,7 +29,8 @@ module ActiveFedora
49
29
 
50
30
  def ng_xml=(new_xml)
51
31
  # before we set ng_xml, we load the datastream so we know if the new value differs.
52
- local_or_remote_content(true)
32
+ # TODO reinstate this
33
+ #local_or_remote_content(true)
53
34
 
54
35
  case new_xml
55
36
  when Nokogiri::XML::Document
@@ -64,45 +45,42 @@ module ActiveFedora
64
45
  end
65
46
  end
66
47
 
67
- # don't want content eagerly loaded by proxy, so implementing methods that would be implemented by define_attribute_methods
48
+ def refresh_attributes
49
+ changed_attributes.clear
50
+ @ng_xml = nil
51
+ end
52
+
53
+ # don't want content eagerly loaded by proxy, so implementing methods that would be implemented by define_attribute_methods
68
54
  def ng_xml_will_change!
69
- attributes_changed_by_setter[:ng_xml] = nil
55
+ changed_attributes['ng_xml'] = nil
70
56
  end
71
57
 
72
58
  def ng_xml_doesnt_change!
73
- clear_attribute_changes([:ng_xml])
59
+ changed_attributes.delete('ng_xml')
74
60
  end
75
-
76
- # don't want content eagerly loaded by proxy, so implementing methods that would be implemented by define_attribute_methods
61
+
62
+ # don't want content eagerly loaded by proxy, so implementing methods that would be implemented by define_attribute_methods
77
63
  def ng_xml_changed?
78
- changed_attributes.has_key? :ng_xml
64
+ changed_attributes.has_key? 'ng_xml'
79
65
  end
80
66
 
81
- def datastream_content
82
- @datastream_content ||= Nokogiri::XML(super).to_xml {|config| config.no_declaration}.strip
67
+ def remote_content
68
+ @datastream_content ||= Nokogiri::XML(super).to_xml {|config| config.no_declaration}.strip
83
69
  end
84
-
70
+
85
71
  def content=(new_content)
86
- if inline?
87
- # inline datastreams may be transformed by fedora 3, so we test for equivalence instead of equality
88
- if !EquivalentXml.equivalent?(datastream_content, new_content)
89
- ng_xml_will_change!
90
- @ng_xml = Nokogiri::XML::Document.parse(new_content)
91
- super(@ng_xml.to_s)
92
- end
93
- else
94
- if datastream_content != new_content
95
- ng_xml_will_change!
96
- @ng_xml = Nokogiri::XML::Document.parse(new_content)
97
- super(@ng_xml.to_s)
98
- end
72
+ if remote_content != new_content.to_s
73
+ ng_xml_will_change!
74
+ @ng_xml = Nokogiri::XML::Document.parse(new_content)
75
+ super(@ng_xml.to_s.strip)
99
76
  end
100
77
  self.class.decorate_ng_xml @ng_xml
101
78
  end
102
79
 
103
80
  def content_changed?
104
- return false if !xml_loaded
105
- super
81
+ return true if autocreate? && new_record?
82
+ return false unless xml_loaded
83
+ ng_xml_changed?
106
84
  end
107
85
 
108
86
  def to_xml(xml = nil)
@@ -124,11 +102,11 @@ module ActiveFedora
124
102
  raise "You can only pass instances of Nokogiri::XML::Node into this method. You passed in #{xml}"
125
103
  end
126
104
  end
127
-
128
- return xml.to_xml {|config| config.no_declaration}.strip
105
+
106
+ return xml.to_xml.strip
129
107
  end
130
108
 
131
- def local_or_remote_content(ensure_fetch = true)
109
+ def content
132
110
  @content = to_xml if ng_xml_changed? || autocreate?
133
111
  super
134
112
  end
@@ -140,7 +118,6 @@ module ActiveFedora
140
118
  def xml_loaded
141
119
  instance_variable_defined? :@ng_xml
142
120
  end
143
-
144
121
  end
145
122
  end
146
123
  end