Class: ActiveFedora::Attributes::PropertyBuilder

Inherits:
ActiveTriples::PropertyBuilder
  • Object
show all
Defined in:
lib/active_fedora/attributes/property_builder.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_accessors(model, reflection, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/active_fedora/attributes/property_builder.rb', line 3

def self.define_accessors(model, reflection, options = {})
  if reflection.multiple?
    super
  else
    mixin = model.generated_property_methods
    name = reflection.term
    define_singular_readers(mixin, name)
    define_singular_id_reader(mixin, name) unless options[:cast] == false
    define_singular_writers(mixin, name)
  end
end

.define_singular_id_reader(mixin, name) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/active_fedora/attributes/property_builder.rb', line 38

def self.define_singular_id_reader(mixin, name)
  mixin.class_eval "    def \#{name}_id(*args)\n      get_values(:\#{name}, :cast => false)\n    end\n  CODE\nend\n", __FILE__, __LINE__ + 1

.define_singular_readers(mixin, name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/active_fedora/attributes/property_builder.rb', line 27

def self.define_singular_readers(mixin, name)
  mixin.class_eval "    def \#{name}(*args)\n      vals = get_values(:\#{name})\n      return nil unless vals\n      raise ActiveFedora::ConstraintError, \"Expected \\\\\"\#{name}\\\\\" of \\\#{id} to have 0-1 statements, but there are \\\#{vals.size}\" if vals.size > 1\n      vals.first\n    end\n  CODE\nend\n", __FILE__, __LINE__ + 1

.define_singular_writers(mixin, name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_fedora/attributes/property_builder.rb', line 46

def self.define_singular_writers(mixin, name)
  # RDF Terms are singular, even if they respond to `#each`
  mixin.class_eval "    def \#{name}=(value)\n      if value.respond_to?(:each) && !(value.respond_to?(:term?) && value.term?)\n        raise ArgumentError, \"You attempted to set the property `\#{name}' of \\\#{id} to an enumerable value. However, this property is declared as singular.\"\n      end\n      set_value(:\#{name}, value)\n    end\n  CODE\nend\n", __FILE__, __LINE__ + 1

.define_writers(mixin, name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/active_fedora/attributes/property_builder.rb', line 15

def self.define_writers(mixin, name)
  # RDF Terms are singular, even if they respond to `#each`
  mixin.class_eval "    def \#{name}=(value)\n      unless value.nil? || (value.respond_to?(:each) && !(value.respond_to?(:term?) && value.term?))\n        raise ArgumentError, \"You attempted to set the property `\#{name}' of \\\#{id} to a scalar value. However, this property is declared as being multivalued.\"\n      end\n      set_value(:\#{name}, value)\n    end\n  CODE\nend\n", __FILE__, __LINE__ + 1

Instance Method Details

#build(&block) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/active_fedora/attributes/property_builder.rb', line 58

def build(&block)
  # TODO: remove this block stuff
  # See https://github.com/samvera/active_fedora/issues/1336
  NodeConfig.new(name, options[:predicate], options.except(:predicate)) do |config|
    config.with_index(&block) if block_given?
  end
end