class ActiveFedora::RegisteredAttributes::Attribute

Constants

VALID_ATTRIBUTE_OPTIONS

Attributes

context_class[R]
datastream[R]
name[R]

Public Class Methods

new(context_class, name, options = {}) click to toggle source

Parameters:

@param context_class [ActiveFedora::Base, human_attribute_name]

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

@param name [String, Symbol]

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

@param [Hash] options Configuration options @option options [Symbol, call] :default @option options [Boolean] :displayable (true) @option options [Boolean] :editable (true)

By marking this attribute :editable

@option options [Boolean] :skip_accessor (false)

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

@option options [Hash] :form

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

@option options [Symbol, String, Nil, Hash] :datastream

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}

@option options [Hash] :validates

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

@option options [Boolean] :multiple (false)

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

@option options [Symbol, call] :writer

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

@option options [Symbol, call] :reader

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

@option options [#to_s] :label (internationalization)

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

@option options [#to_s] :hint

A supplement to the Attribute's :label
# 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

Public Instance Methods

default(context) click to toggle source
# 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?() click to toggle source
# File lib/active_fedora/registered_attributes/attribute.rb, line 60
def displayable?
  @options.fetch(:displayable, true)
end
editable?() click to toggle source
# File lib/active_fedora/registered_attributes/attribute.rb, line 64
def editable?
  @options.fetch(:editable, true)
end
label() click to toggle source
# 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?() click to toggle source
# File lib/active_fedora/registered_attributes/attribute.rb, line 56
def multiple?
  options[:multiple]
end
options_for_input(overrides = {}) click to toggle source
# 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, {}| ... } click to toggle source
# 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| ... } click to toggle source
# 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, options| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

options() click to toggle source
# File lib/active_fedora/registered_attributes/attribute.rb, line 175
def options
  @options
end
options_for_delegation() click to toggle source
# File lib/active_fedora/registered_attributes/attribute.rb, line 139
def options_for_delegation
  if datastream.is_a?(Hash)
    datastream.merge(multiple: multiple?)
  else
    {
      datastream: datastream,
      multiple: multiple?
    }
  end
end
with_accession?() click to toggle source
# File lib/active_fedora/registered_attributes/attribute.rb, line 88
def with_accession?
  return false if options[:skip_accessor]
  !datastream
end
with_delegation?() click to toggle source
# File lib/active_fedora/registered_attributes/attribute.rb, line 76
def with_delegation?; datastream; end
with_reader_method_wrapper() { |method_name, proc| ... } click to toggle source
# File lib/active_fedora/registered_attributes/attribute.rb, line 165
def with_reader_method_wrapper
  if reader = options[:reader]
    method_name = "#{name}".to_sym
    proc = reader.respond_to?(:call) ?
      reader :
      lambda { |value| send(reader, value) }
    yield(method_name, proc)
  end
end
with_validation?() click to toggle source
# File lib/active_fedora/registered_attributes/attribute.rb, line 82
def with_validation?; options[:validates]; end
with_writer_method_wrap() { |method_name, proc| ... } click to toggle source
# File lib/active_fedora/registered_attributes/attribute.rb, line 150
def with_writer_method_wrap
  method_name = "#{name}=".to_sym
  if writer = options[:writer]
    proc = writer.respond_to?(:call) ?
      writer :
      lambda { |value| send(writer, value) }
    yield(method_name, proc)
  elsif multiple?
    proc = lambda {|*values|
      Array(values).flatten.select {|value| value.present? }
    }
    yield(method_name, proc)
  end
end