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 <<-CODE, __FILE__, __LINE__ + 1
    def #{name}_id(*args)
      get_values(:#{name}, :cast => false)
    end
  CODE
end

.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 <<-CODE, __FILE__, __LINE__ + 1
    def #{name}(*args)
      vals = get_values(:#{name})
      return nil unless vals
      raise ActiveFedora::ConstraintError, "Expected \\"#{name}\\" of \#{id} to have 0-1 statements, but there are \#{vals.size}" if vals.size > 1
      vals.first
    end
  CODE
end

.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 <<-CODE, __FILE__, __LINE__ + 1
    def #{name}=(value)
      if value.respond_to?(:each) && !(value.respond_to?(:term?) && value.term?)
        raise ArgumentError, "You attempted to set the property `#{name}' of \#{id} to an enumerable value. However, this property is declared as singular."
      end
      set_value(:#{name}, value)
    end
  CODE
end

.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 <<-CODE, __FILE__, __LINE__ + 1
    def #{name}=(value)
      unless value.nil? || (value.respond_to?(:each) && !(value.respond_to?(:term?) && value.term?))
        raise ArgumentError, "You attempted to set the property `#{name}' of \#{id} to a scalar value. However, this property is declared as being multivalued."
      end
      set_value(:#{name}, value)
    end
  CODE
end

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