Class: ActiveFedora::RegisteredAttributes::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/active_fedora/registered_attributes/attribute.rb

Constant Summary collapse

VALID_ATTRIBUTE_OPTIONS =
[
  :default, :displayable, :editable, :form, :datastream, :validates,
  :multiple, :writer, :reader, :label, :hint, :skip_accessor
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context_class, name, options = {}) ⇒ Attribute

Parameters:

Parameters:

  • context_class (ActiveFedora::Base, #human_attribute_name)

    A descendant of ActiveFedora::Base. Though generally speaking it may work with other ActiveModel descendants

  • name (String, Symbol)

    The name of the attribute (i.e. “title”, “subject”, “description”).

  • options (Hash) (defaults to: {})

    Configuration options

Options Hash (options):

  • :default (Symbol, #call)
  • :displayable (Boolean) — default: true
  • :editable (Boolean) — default: true

    By marking this attribute :editable

  • :skip_accessor (Boolean) — default: false

    Don’t attempt to create the setter/getter if there is no :datastream option

  • :form (Hash)

    Additional options for a form builder (i.e. class, id, data-attribute)

  • :datastream (Symbol, String, Nil, Hash)

    Where will the attribute persist; This can be nil. If nil, this would be a virtual attribute (i.e. attr_accessor name). If it is not nil, then see #options_for_delegation

  • :validates (Hash)

    A hash that can be used as the args for ActiveModel::Validations::ClassMethods.validates

  • :multiple (Boolean) — default: false

    Can there be multiple values for this attribute? Used to derive an option for ActiveFedora::Base.delegate

  • :writer (Symbol, #call)

    Before we persist the attribute, pass the value through the :writer

  • :reader (Symbol, #call)

    After we retrieve the value from its persistence, transform the value via the :reader

  • :label (#to_s) — default: internationalization

    If we were to build a form from this, what would we use as the label

  • :hint (#to_s)

    A supplement to the Attribute’s :label



46
47
48
49
50
51
52
53
54
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 46

def initialize(context_class, name, options = {})
  @options = options.symbolize_keys
  @options.assert_valid_keys(VALID_ATTRIBUTE_OPTIONS)
  @context_class = context_class
  @datastream = @options.fetch(:datastream, false)
  @name = name
  @options[:multiple] = false unless @options.key?(:multiple)
  @options[:form] ||= {}
end

Instance Attribute Details

#datastreamObject (readonly)

Returns the value of attribute datastream.



10
11
12
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 10

def datastream
  @datastream
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 10

def name
  @name
end

Instance Method Details

#default(context) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 106

def default(context)
  this_default = options[:default]
  case
  when this_default.respond_to?(:call) then context.instance_exec(&this_default)
  when this_default.duplicable? then this_default.dup
  else this_default
  end
end

#displayable?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 60

def displayable?
  @options.fetch(:displayable, true)
end

#editable?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 64

def editable?
  @options.fetch(:editable, true)
end

#labelObject



68
69
70
71
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 68

def label
  default = options[:label] || name.to_s.humanize
  context_class.human_attribute_name(name, default: default)
end

#multiple?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 56

def multiple?
  options[:multiple]
end

#options_for_input(overrides = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 94

def options_for_input(overrides = {})
  options[:form].tap {|hash|
    hash[:hint] ||= options[:hint] if options[:hint]
    hash[:label] ||= options[:label] if options[:label]
    if multiple?
      hash[:as] = 'multi_value'
      hash[:input_html] ||= {}
      hash[:input_html][:multiple] = 'multiple'
    end
  }.deep_merge(overrides)
end

#with_accession_options {|name, {}| ... } ⇒ Object

Yields:



85
86
87
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 85

def with_accession_options
  yield(name, {}) if with_accession?
end

#with_delegation_options {|name, options_for_delegation| ... } ⇒ Object

Yields:

  • (name, options_for_delegation)


73
74
75
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 73

def with_delegation_options
  yield(name, options_for_delegation) if with_delegation?
end

#with_validation_options {|name, | ... } ⇒ Object

Yields:



79
80
81
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 79

def with_validation_options
  yield(name, options[:validates]) if with_validation?
end

#wrap_reader_method(context) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 126

def wrap_reader_method(context)
  with_reader_method_wrapper do |method_name, block|
    context.instance_exec do
      original_method = instance_method(method_name)
      define_method(method_name) do |*args|
        instance_exec(original_method.bind(self).call(*args), &block)
      end
    end
  end
end

#wrap_writer_method(context) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 115

def wrap_writer_method(context)
  with_writer_method_wrap do |method_name, block|
    context.instance_exec do
      original_method = instance_method(method_name)
      define_method(method_name) do |*args|
        original_method.bind(self).call(instance_exec(*args, &block))
      end
    end
  end
end