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


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

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


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

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

.define_singular_readers(mixin, name) ⇒ Object


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

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

.define_singular_writers(mixin, name) ⇒ Object


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

def self.define_singular_writers(mixin, name)
  mixin.class_eval "def \#{name}=(value)\nif value.respond_to?(:each) # singular\nraise ArgumentError, \"You attempted to set the property `\#{name}' to an enumerable value. However, this property is declared as singular.\"\nend\nset_value(:\#{name}, value)\nend\n", __FILE__, __LINE__ + 1
end

.define_writers(mixin, name) ⇒ Object


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

def self.define_writers(mixin, name)
  mixin.class_eval "def \#{name}=(value)\nif value.present? && !value.respond_to?(:each)\nraise ArgumentError, \"You attempted to set the property `\#{name}' to a scalar value. However, this property is declared as being multivalued.\"\nend\nset_value(:\#{name}, value)\nend\n", __FILE__, __LINE__ + 1
end

Instance Method Details

#build(&block) ⇒ Object


56
57
58
59
60
# File 'lib/active_fedora/attributes/property_builder.rb', line 56

def build(&block)
  NodeConfig.new(name, options[:predicate], options.except(:predicate)) do |config|
    config.with_index(&block) if block_given?
  end
end