active-fedora 9.9.0 → 9.10.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 (136) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +20 -6
  3. data/.travis.yml +2 -6
  4. data/History.txt +146 -0
  5. data/active-fedora.gemspec +11 -8
  6. data/lib/active_fedora/associations/association.rb +44 -14
  7. data/lib/active_fedora/associations/association_scope.rb +6 -2
  8. data/lib/active_fedora/associations/basic_contains_association.rb +2 -3
  9. data/lib/active_fedora/associations/belongs_to_association.rb +23 -14
  10. data/lib/active_fedora/associations/builder/association.rb +95 -41
  11. data/lib/active_fedora/associations/builder/belongs_to.rb +30 -2
  12. data/lib/active_fedora/associations/builder/collection_association.rb +36 -4
  13. data/lib/active_fedora/associations/builder/contains.rb +10 -5
  14. data/lib/active_fedora/associations/builder/directly_contains.rb +6 -8
  15. data/lib/active_fedora/associations/builder/directly_contains_one.rb +27 -20
  16. data/lib/active_fedora/associations/builder/has_and_belongs_to_many.rb +7 -3
  17. data/lib/active_fedora/associations/builder/has_many.rb +8 -45
  18. data/lib/active_fedora/associations/builder/indirectly_contains.rb +6 -8
  19. data/lib/active_fedora/associations/builder/property.rb +6 -12
  20. data/lib/active_fedora/associations/builder/singular_association.rb +3 -5
  21. data/lib/active_fedora/associations/builder/singular_property.rb +5 -4
  22. data/lib/active_fedora/associations/collection_association.rb +22 -26
  23. data/lib/active_fedora/associations/collection_proxy.rb +1 -1
  24. data/lib/active_fedora/associations/contained_finder.rb +1 -2
  25. data/lib/active_fedora/associations/directly_contains_one_association.rb +2 -3
  26. data/lib/active_fedora/associations/has_and_belongs_to_many_association.rb +1 -1
  27. data/lib/active_fedora/associations/has_many_association.rb +24 -5
  28. data/lib/active_fedora/associations/rdf.rb +1 -20
  29. data/lib/active_fedora/associations.rb +1 -1
  30. data/lib/active_fedora/attached_files.rb +2 -2
  31. data/lib/active_fedora/attribute_assignment.rb +55 -0
  32. data/lib/active_fedora/attribute_methods/read.rb +29 -44
  33. data/lib/active_fedora/attribute_methods/write.rb +16 -19
  34. data/lib/active_fedora/attribute_methods.rb +149 -29
  35. data/lib/active_fedora/attributes.rb +6 -18
  36. data/lib/active_fedora/autosave_association.rb +8 -12
  37. data/lib/active_fedora/base.rb +2 -2
  38. data/lib/active_fedora/caching_connection.rb +1 -1
  39. data/lib/active_fedora/callbacks.rb +9 -9
  40. data/lib/active_fedora/common.rb +67 -0
  41. data/lib/active_fedora/core.rb +11 -23
  42. data/lib/active_fedora/default_model_mapper.rb +24 -0
  43. data/lib/active_fedora/errors.rb +42 -0
  44. data/lib/active_fedora/fedora.rb +1 -1
  45. data/lib/active_fedora/file/attributes.rb +9 -10
  46. data/lib/active_fedora/file.rb +19 -56
  47. data/lib/active_fedora/file_persistence.rb +27 -0
  48. data/lib/active_fedora/indexing.rb +15 -9
  49. data/lib/active_fedora/indexing_service.rb +4 -4
  50. data/lib/active_fedora/inheritance.rb +34 -0
  51. data/lib/active_fedora/ldp_resource.rb +1 -0
  52. data/lib/active_fedora/model.rb +18 -16
  53. data/lib/active_fedora/model_classifier.rb +77 -0
  54. data/lib/active_fedora/nested_attributes.rb +145 -18
  55. data/lib/active_fedora/persistence.rb +23 -5
  56. data/lib/active_fedora/predicates.rb +3 -0
  57. data/lib/active_fedora/qualified_dublin_core_datastream.rb +1 -1
  58. data/lib/active_fedora/query_result_builder.rb +12 -28
  59. data/lib/active_fedora/querying.rb +1 -1
  60. data/lib/active_fedora/rdf/datastream_indexing.rb +1 -1
  61. data/lib/active_fedora/reflection.rb +212 -80
  62. data/lib/active_fedora/relation/calculations.rb +1 -5
  63. data/lib/active_fedora/relation/finder_methods.rb +39 -26
  64. data/lib/active_fedora/relation.rb +17 -0
  65. data/lib/active_fedora/scoping/default.rb +117 -0
  66. data/lib/active_fedora/scoping/named.rb +152 -4
  67. data/lib/active_fedora/scoping.rb +85 -2
  68. data/lib/active_fedora/simple_datastream.rb +1 -1
  69. data/lib/active_fedora/solr_hit.rb +71 -0
  70. data/lib/active_fedora/solr_instance_loader.rb +12 -36
  71. data/lib/active_fedora/solr_query_builder.rb +20 -25
  72. data/lib/active_fedora/solr_service.rb +24 -13
  73. data/lib/active_fedora/type/boolean.rb +23 -0
  74. data/lib/active_fedora/type/value.rb +118 -0
  75. data/lib/active_fedora/type.rb +8 -0
  76. data/lib/active_fedora/validations.rb +14 -5
  77. data/lib/active_fedora/version.rb +1 -1
  78. data/lib/active_fedora/versionable.rb +8 -7
  79. data/lib/active_fedora/with_metadata/metadata_node.rb +2 -1
  80. data/lib/active_fedora.rb +35 -15
  81. data/lib/generators/active_fedora/config/fedora/fedora_generator.rb +4 -0
  82. data/lib/generators/active_fedora/config/fedora/templates/.fcrepo_wrapper +3 -0
  83. data/lib/generators/active_fedora/config/solr/solr_generator.rb +4 -0
  84. data/lib/generators/active_fedora/config/solr/templates/.solr_wrapper +5 -0
  85. data/lib/generators/active_fedora/config/solr/templates/solr/config/solrconfig.xml +15 -0
  86. data/solr/config/solrconfig.xml +15 -0
  87. data/spec/config_helper.rb +0 -5
  88. data/spec/integration/associations_spec.rb +5 -5
  89. data/spec/integration/attached_files_spec.rb +4 -4
  90. data/spec/integration/base_spec.rb +4 -4
  91. data/spec/integration/bug_spec.rb +0 -1
  92. data/spec/integration/collection_association_spec.rb +34 -3
  93. data/spec/integration/file_spec.rb +2 -2
  94. data/spec/integration/full_featured_model_spec.rb +4 -4
  95. data/spec/integration/has_and_belongs_to_many_associations_spec.rb +1 -1
  96. data/spec/integration/has_many_associations_spec.rb +30 -1
  97. data/spec/integration/indirect_container_spec.rb +1 -1
  98. data/spec/integration/nested_attribute_spec.rb +6 -0
  99. data/spec/integration/ntriples_datastream_spec.rb +4 -4
  100. data/spec/integration/om_datastream_spec.rb +7 -7
  101. data/spec/integration/persistence_spec.rb +1 -1
  102. data/spec/integration/relation_delegation_spec.rb +1 -1
  103. data/spec/integration/scoped_query_spec.rb +13 -13
  104. data/spec/integration/solr_hit_spec.rb +52 -0
  105. data/spec/samples/hydra-mods_article_datastream.rb +2 -2
  106. data/spec/spec_helper.rb +5 -9
  107. data/spec/unit/active_fedora_spec.rb +0 -26
  108. data/spec/unit/attributes_spec.rb +13 -7
  109. data/spec/unit/base_spec.rb +20 -0
  110. data/spec/unit/builder/has_and_belongs_to_many_spec.rb +2 -3
  111. data/spec/unit/callback_spec.rb +3 -8
  112. data/spec/unit/collection_proxy_spec.rb +1 -1
  113. data/spec/unit/default_model_mapper_spec.rb +39 -0
  114. data/spec/unit/file_spec.rb +21 -2
  115. data/spec/unit/finder_methods_spec.rb +30 -6
  116. data/spec/unit/has_and_belongs_to_many_association_spec.rb +4 -4
  117. data/spec/unit/has_many_association_spec.rb +25 -3
  118. data/spec/unit/indexing_spec.rb +17 -3
  119. data/spec/unit/model_classifier_spec.rb +49 -0
  120. data/spec/unit/model_spec.rb +0 -9
  121. data/spec/unit/ntriples_datastream_spec.rb +16 -16
  122. data/spec/unit/om_datastream_spec.rb +7 -7
  123. data/spec/unit/qualified_dublin_core_datastream_spec.rb +1 -1
  124. data/spec/unit/query_result_builder_spec.rb +4 -10
  125. data/spec/unit/query_spec.rb +28 -28
  126. data/spec/unit/rdf/indexing_service_spec.rb +16 -16
  127. data/spec/unit/reflection_spec.rb +2 -2
  128. data/spec/unit/scoping_spec.rb +67 -0
  129. data/spec/unit/simple_datastream_spec.rb +2 -2
  130. data/spec/unit/solr_config_options_spec.rb +29 -32
  131. data/spec/unit/solr_hit_spec.rb +58 -0
  132. data/spec/unit/solr_query_builder_spec.rb +9 -1
  133. data/spec/unit/solr_service_spec.rb +19 -3
  134. data/spec/unit/validations_spec.rb +1 -1
  135. metadata +82 -20
  136. data/spec/support/freeze_mocks.rb +0 -12
@@ -14,7 +14,13 @@ module ActiveFedora
14
14
  force_reload = opts
15
15
  end
16
16
  reload if force_reload || stale_target?
17
- @proxy ||= CollectionProxy.new(self)
17
+ if null_scope?
18
+ # Cache the proxy separately before the owner has an id
19
+ # or else a post-save proxy will still lack the id
20
+ @null_proxy ||= CollectionProxy.new(self)
21
+ else
22
+ @proxy ||= CollectionProxy.new(self)
23
+ end
18
24
  end
19
25
 
20
26
  # Implements the writer method, e.g. foo.items= for Foo.has_many :items
@@ -27,9 +33,7 @@ module ActiveFedora
27
33
  if loaded?
28
34
  load_target.map(&:id)
29
35
  else
30
- load_from_solr.map do |solr_record|
31
- solr_record['id']
32
- end
36
+ load_from_solr.map(&:id)
33
37
  end
34
38
  end
35
39
 
@@ -68,10 +72,10 @@ module ActiveFedora
68
72
  # This method is abstract in the sense that it relies on
69
73
  # +count_records+, which is a method descendants have to provide.
70
74
  def size
71
- if @owner.new_record? && @target
72
- @target.size
73
- elsif !loaded? && @target.is_a?(Array)
74
- unsaved_records = @target.select(&:new_record?)
75
+ if !find_target? || loaded?
76
+ target.size
77
+ elsif !loaded? && target.is_a?(Array)
78
+ unsaved_records = target.select(&:new_record?)
75
79
  unsaved_records.size + count_records
76
80
  else
77
81
  count_records
@@ -96,7 +100,7 @@ module ActiveFedora
96
100
  # Replace this collection with +other_array+
97
101
  # This will perform a diff and delete/add only records that have changed.
98
102
  def replace(other_array)
99
- other_array.each { |val| raise_on_type_mismatch(val) }
103
+ other_array.each { |val| raise_on_type_mismatch!(val) }
100
104
 
101
105
  load_target
102
106
  other = other_array.size < 100 ? other_array : other_array.to_set
@@ -152,8 +156,7 @@ module ActiveFedora
152
156
  result = true
153
157
 
154
158
  records.flatten.each do |record|
155
- raise_on_type_mismatch(record)
156
- run_type_validator(record)
159
+ raise_on_type_mismatch!(record)
157
160
  add_to_target(record) do |_r|
158
161
  result &&= insert_record(record) unless owner.new_record?
159
162
  end
@@ -208,7 +211,7 @@ module ActiveFedora
208
211
  if attrs.is_a?(Array)
209
212
  attrs.collect { |attr| create(attr) }
210
213
  else
211
- create_record(attrs) do |record|
214
+ _create_record(attrs) do |record|
212
215
  yield(record) if block_given?
213
216
  record.save
214
217
  end
@@ -216,7 +219,7 @@ module ActiveFedora
216
219
  end
217
220
 
218
221
  def create!(attrs = {})
219
- create_record(attrs) do |record|
222
+ _create_record(attrs) do |record|
220
223
  yield(record) if block_given?
221
224
  record.save!
222
225
  end
@@ -225,7 +228,7 @@ module ActiveFedora
225
228
  # Count all records using solr. Construct options and pass them with
226
229
  # scope to the target class's +count+.
227
230
  def count(_options = {})
228
- @reflection.klass.count(conditions: construct_query)
231
+ scope.count
229
232
  end
230
233
 
231
234
  # Sets the target of this proxy to <tt>\target</tt>, and the \loaded flag to +true+.
@@ -336,7 +339,7 @@ module ActiveFedora
336
339
  end
337
340
  end
338
341
 
339
- def create_record(attributes, raise = false)
342
+ def _create_record(attributes, raise = false)
340
343
  attributes.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash)
341
344
  ensure_owner_is_not_new
342
345
 
@@ -352,7 +355,7 @@ module ActiveFedora
352
355
 
353
356
  def delete_or_destroy(records, method)
354
357
  records = records.flatten.select { |x| load_target.include?(x) }
355
- records.each { |record| raise_on_type_mismatch(record) }
358
+ records.each { |record| raise_on_type_mismatch!(record) }
356
359
  existing_records = records.select(&:persisted?)
357
360
 
358
361
  records.each { |record| callback(:before_remove, record) }
@@ -370,20 +373,13 @@ module ActiveFedora
370
373
 
371
374
  def callback(method, record)
372
375
  callbacks_for(method).each do |callback|
373
- case callback
374
- when Symbol
375
- @owner.send(callback, record)
376
- when Proc
377
- callback.call(@owner, record)
378
- else
379
- callback.send(method, @owner, record)
380
- end
376
+ callback.call(method, owner, record)
381
377
  end
382
378
  end
383
379
 
384
380
  def callbacks_for(callback_name)
385
- full_callback_name = "#{callback_name}_for_#{@reflection.name}"
386
- @owner.class.send(full_callback_name.to_sym) || []
381
+ full_callback_name = "#{callback_name}_for_#{reflection.name}"
382
+ owner.class.send(full_callback_name)
387
383
  end
388
384
 
389
385
  def ensure_owner_is_not_new
@@ -38,7 +38,7 @@ module ActiveFedora
38
38
  def initialize(association)
39
39
  @association = association
40
40
  super association.klass
41
- merge! association.scope
41
+ merge! association.scope(nullify: false)
42
42
  end
43
43
 
44
44
  def target
@@ -42,8 +42,7 @@ module ActiveFedora::Associations
42
42
  query = ActiveFedora::SolrQueryBuilder.construct_query_for_rel(
43
43
  [[:has_model, proxy_class.to_class_uri], [:proxyFor, record.id]]
44
44
  )
45
- results = ActiveFedora::SolrService.query(query, fl: 'id')
46
- results.map { |res| ::RDF::URI(ActiveFedora::Base.id_to_uri(res['id'])) }
45
+ ActiveFedora::SolrService.query(query, fl: 'id').map(&:rdf_uri)
47
46
  end
48
47
  end
49
48
  end
@@ -33,8 +33,7 @@ module ActiveFedora
33
33
  # Ensures that this association's +type+ is set on the record and adds the record to the association's DirectContainer
34
34
  def replace(record, *)
35
35
  if record
36
- raise_on_type_mismatch(record)
37
- run_type_validator(record)
36
+ raise_on_type_mismatch!(record)
38
37
  remove_existing_target
39
38
  add_type_to_record(record, options[:type])
40
39
  add_to_container(record)
@@ -66,7 +65,7 @@ module ActiveFedora
66
65
 
67
66
  # Returns the Reflection corresponding to the direct container association that's being filtered
68
67
  def container_reflection
69
- @container_reflection ||= @owner.class.reflect_on_association(@reflection.options[:through])
68
+ @container_reflection ||= @owner.class._reflect_on_association(@reflection.options[:through])
70
69
  end
71
70
 
72
71
  # Returns the DirectContainerAssociation corresponding to the direct container that's being filtered
@@ -37,7 +37,7 @@ module ActiveFedora
37
37
  result = true
38
38
 
39
39
  records.flatten.each do |record|
40
- raise_on_type_mismatch(record)
40
+ raise_on_type_mismatch!(record)
41
41
  add_to_target(record) do |_r|
42
42
  result &&= insert_record(record)
43
43
  end
@@ -14,11 +14,7 @@ module ActiveFedora
14
14
  # If the collection is empty the target is set to an empty array and
15
15
  # the loaded flag is set to true as well.
16
16
  def count_records
17
- count = if loaded?
18
- @target.size
19
- else
20
- @reflection.klass.count(conditions: construct_query)
21
- end
17
+ count = scope.count
22
18
 
23
19
  # If there's nothing in the database and @target has no new records
24
20
  # we are certain the current target is an empty array. This is a
@@ -61,6 +57,29 @@ module ActiveFedora
61
57
  end
62
58
  end
63
59
 
60
+ def handle_dependency
61
+ case options[:dependent]
62
+ when :restrict_with_exception
63
+ raise ActiveFedora::DeleteRestrictionError, reflection.name unless empty?
64
+
65
+ when :restrict_with_error
66
+ unless empty?
67
+ record = owner.class.human_attribute_name(reflection.name).downcase
68
+ owner.errors.add(:base, message || :'restrict_dependent_destroy.has_many', record: record)
69
+ throw(:abort)
70
+ end
71
+
72
+ else
73
+ if options[:dependent] == :destroy
74
+ # No point in executing the counter update since we're going to destroy the parent anyway
75
+ load_target.each { |t| t.destroyed_by_association = reflection }
76
+ destroy_all
77
+ else
78
+ delete_all
79
+ end
80
+ end
81
+ end
82
+
64
83
  protected
65
84
 
66
85
  def find_polymorphic_inverse(record)
@@ -60,26 +60,7 @@ module ActiveFedora
60
60
  ids = candidate_uris.map { |uri| ActiveFedora::Base.uri_to_id(uri) }
61
61
  results = ActiveFedora::SolrService.query(ActiveFedora::SolrQueryBuilder.construct_query_for_ids(ids), rows: 10_000)
62
62
 
63
- docs = results.select do |result|
64
- ActiveFedora::QueryResultBuilder.classes_from_solr_document(result).any? do |klass|
65
- class_ancestors(klass).include? reflection.klass
66
- end
67
- end
68
-
69
- docs.map { |doc| ::RDF::URI.new(ActiveFedora::Base.id_to_uri(doc['id'])) }
70
- end
71
-
72
- ##
73
- # Returns a list of all the ancestor classes up to ActiveFedora::Base including the class itself
74
- # @param [Class] klass
75
- # @return [Array<Class>]
76
- # @example
77
- # class Car < ActiveFedora::Base; end
78
- # class SuperCar < Car; end
79
- # class_ancestors(SuperCar)
80
- # # => [SuperCar, Car, ActiveFedora::Base]
81
- def class_ancestors(klass)
82
- klass.ancestors.select { |k| k.instance_of?(Class) } - [Object, BasicObject]
63
+ results.select { |result| result.model? reflection.klass }.map(&:rdf_uri)
83
64
  end
84
65
  end
85
66
  end
@@ -69,7 +69,7 @@ module ActiveFedora
69
69
  association = association_instance_get(name)
70
70
 
71
71
  if association.nil?
72
- reflection = self.class.reflect_on_association(name)
72
+ reflection = self.class._reflect_on_association(name)
73
73
  association = reflection.association_class.new(self, reflection) if reflection
74
74
  association_instance_set(name, association) if association
75
75
  end
@@ -49,7 +49,7 @@ module ActiveFedora
49
49
  deprecation_deprecate :clear_datastreams
50
50
 
51
51
  def contains_assertions
52
- resource.query(subject: resource, predicate: Ldp.contains).objects.map(&:to_s)
52
+ resource.query(subject: resource, predicate: ::RDF::Vocab::LDP.contains).objects.map(&:to_s)
53
53
  end
54
54
 
55
55
  def load_attached_files
@@ -131,7 +131,7 @@ module ActiveFedora
131
131
 
132
132
  def create_singleton_association(file_path)
133
133
  undeclared_files << file_path.to_sym
134
- association = Associations::BasicContainsAssociation.new(self, Reflection::AssociationReflection.new(:contains, file_path, { class_name: 'ActiveFedora::File' }, self.class))
134
+ association = Associations::BasicContainsAssociation.new(self, Reflection::AssociationReflection.new(:contains, file_path, nil, { class_name: 'ActiveFedora::File' }, self.class))
135
135
  @association_cache[file_path.to_sym] = association
136
136
 
137
137
  singleton_class.send :define_method, accessor_name(file_path) do
@@ -0,0 +1,55 @@
1
+ require 'active_support/core_ext/hash/keys'
2
+
3
+ module ActiveFedora
4
+ module AttributeAssignment
5
+ include ActiveModel::ForbiddenAttributesProtection
6
+
7
+ # Alias for assign_attributes.
8
+ def attributes=(attributes)
9
+ assign_attributes(attributes)
10
+ end
11
+
12
+ # Allows you to set all the attributes by passing in a hash of attributes with
13
+ # keys matching the attribute names.
14
+ #
15
+ # If the passed hash responds to <tt>permitted?</tt> method and the return value
16
+ # of this method is +false+ an <tt>ActiveModel::ForbiddenAttributesError</tt>
17
+ # exception is raised.
18
+ #
19
+ # class Cat
20
+ # include ActiveModel::AttributeAssignment
21
+ # attr_accessor :name, :status
22
+ # end
23
+ #
24
+ # cat = Cat.new
25
+ # cat.assign_attributes(name: "Gorby", status: "yawning")
26
+ # cat.name # => 'Gorby'
27
+ # cat.status => 'yawning'
28
+ # cat.assign_attributes(status: "sleeping")
29
+ # cat.name # => 'Gorby'
30
+ # cat.status => 'sleeping'
31
+ def assign_attributes(new_attributes)
32
+ unless new_attributes.respond_to?(:stringify_keys)
33
+ raise ArgumentError, "When assigning attributes, you must pass a hash as an argument."
34
+ end
35
+ return if new_attributes.nil? || new_attributes.empty?
36
+
37
+ attributes = new_attributes.stringify_keys
38
+ _assign_attributes(sanitize_for_mass_assignment(attributes))
39
+ end
40
+
41
+ private
42
+
43
+ def _assign_attributes(attributes)
44
+ attributes.each do |k, v|
45
+ _assign_attribute(k, v)
46
+ end
47
+ end
48
+
49
+ def _assign_attribute(k, v)
50
+ raise UnknownAttributeError.new(self, k) unless respond_to?("#{k}=")
51
+
52
+ public_send("#{k}=", v)
53
+ end
54
+ end
55
+ end
@@ -1,61 +1,46 @@
1
1
  module ActiveFedora
2
2
  module AttributeMethods
3
3
  module Read
4
- ReaderMethodCache = Class.new(AttributeMethodCache) do
5
- private
6
-
7
- # We want to generate the methods via module_eval rather than
8
- # define_method, because define_method is slower on dispatch.
9
- # Evaluating many similar methods may use more memory as the instruction
10
- # sequences are duplicated and cached (in MRI). define_method may
11
- # be slower on dispatch, but if you're careful about the closure
12
- # created, then define_method will consume much less memory.
13
- #
14
- # But sometimes the database might return columns with
15
- # characters that are not allowed in normal method names (like
16
- # 'my_column(omg)'. So to work around this we first define with
17
- # the __temp__ identifier, and then use alias method to rename
18
- # it to what we want.
19
- #
20
- # We are also defining a constant to hold the frozen string of
21
- # the attribute name. Using a constant means that we do not have
22
- # to allocate an object on each call to the attribute method.
23
- # Making it frozen means that it doesn't get duped when used to
24
- # key the @attributes_cache in read_attribute.
25
- def method_body(method_name, const_name)
26
- <<-EOMETHOD
27
- def #{method_name}
28
- name = ::ActiveFedora::AttributeMethods::AttrNames::ATTR_#{const_name}
29
- read_attribute(name) { |n| missing_attribute(n, caller) }
4
+ module ClassMethods
5
+ protected
6
+
7
+ def define_method_attribute(name)
8
+ name = name.to_s
9
+ safe_name = name.unpack('h*'.freeze).first
10
+ temp_method = "__temp__#{safe_name}"
11
+
12
+ ActiveFedora::AttributeMethods::AttrNames.set_name_cache safe_name, name
13
+
14
+ generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
15
+ def #{temp_method}
16
+ name = ::ActiveRecord::AttributeMethods::AttrNames::ATTR_#{safe_name}
17
+ _read_attribute(name) { |n| missing_attribute(n, caller) }
18
+ end
19
+ STR
20
+
21
+ generated_attribute_methods.module_eval do
22
+ alias_method name, temp_method
23
+ undef_method temp_method
24
+ end
30
25
  end
31
- EOMETHOD
32
- end
33
- end.new
26
+ end
34
27
 
35
28
  extend ActiveSupport::Concern
36
29
 
37
30
  # Returns the value of the attribute identified by <tt>attr_name</tt> after
38
31
  # it has been typecast (for example, "2004-12-12" in a date column is cast
39
32
  # to a date object, like Date.new(2004, 12, 12)).
40
- def read_attribute(attr_name)
33
+ def read_attribute(attr_name, &block)
41
34
  name = attr_name.to_s
42
- @attributes.fetch(name, nil)
35
+ _read_attribute(name, &block)
43
36
  end
44
37
 
45
- private
46
-
47
- def attribute(attribute_name)
48
- read_attribute(attribute_name)
49
- end
38
+ def _read_attribute(attr_name) # :nodoc:
39
+ @attributes.fetch(attr_name.to_s) { |n| yield n if block_given? }
40
+ end
50
41
 
51
- module ClassMethods
52
- def define_method_attribute(name)
53
- method = ReaderMethodCache[name.to_s]
54
- generated_attribute_methods.module_eval do
55
- define_method name, method
56
- end
57
- end
58
- end
42
+ alias attribute _read_attribute
43
+ private :attribute
59
44
  end
60
45
  end
61
46
  end
@@ -1,18 +1,24 @@
1
1
  module ActiveFedora
2
2
  module AttributeMethods
3
3
  module Write
4
- WriterMethodCache = Class.new(AttributeMethodCache) do
5
- private
4
+ module ClassMethods
5
+ protected
6
6
 
7
- def method_body(method_name, const_name)
8
- <<-EOMETHOD
9
- def #{method_name}(value)
10
- name = ::ActiveFedora::AttributeMethods::AttrNames::ATTR_#{const_name}
11
- write_attribute(name, value)
7
+ def define_method_attribute=(name)
8
+ name = name.to_s
9
+ safe_name = name.unpack('h*'.freeze).first
10
+ ActiveFedora::AttributeMethods::AttrNames.set_name_cache safe_name, name
11
+
12
+ generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
13
+ def __temp__#{safe_name}=(value)
14
+ name = ::ActiveRecord::AttributeMethods::AttrNames::ATTR_#{safe_name}
15
+ write_attribute(name, value)
16
+ end
17
+ alias_method #{(name + '=').inspect}, :__temp__#{safe_name}=
18
+ undef_method :__temp__#{safe_name}=
19
+ STR
12
20
  end
13
- EOMETHOD
14
- end
15
- end.new
21
+ end
16
22
 
17
23
  extend ActiveSupport::Concern
18
24
 
@@ -33,15 +39,6 @@ module ActiveFedora
33
39
  def attribute=(attribute_name, value)
34
40
  write_attribute(attribute_name, value)
35
41
  end
36
-
37
- module ClassMethods
38
- def define_method_attribute=(name)
39
- method = WriterMethodCache[name.to_s]
40
- generated_attribute_methods.module_eval do
41
- define_method "#{name}=", method
42
- end
43
- end
44
- end
45
42
  end
46
43
  end
47
44
  end