Class: ActiveFedora::RegisteredAttributes::Attribute

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

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



41
42
43
44
45
46
47
48
49
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 41

def initialize(context_class, name, options = {})
  @options = options.symbolize_keys
  @options.assert_valid_keys(:default, :displayable, :editable, :form, :datastream, :validates, :multiple, :writer, :reader, :label, :hint, :skip_accessor)
  @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.



5
6
7
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 5

def datastream
  @datastream
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 5

def name
  @name
end

Instance Method Details

#default(context) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 101

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)


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

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

#editable?Boolean

Returns:

  • (Boolean)


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

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

#labelObject



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

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

#multiple?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 51

def multiple?
  options[:multiple]
end

#options_for_input(overrides = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 89

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:



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

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

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

Yields:

  • (name, options_for_delegation)


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

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

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

Yields:



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

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

#wrap_reader_method(context) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 121

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



110
111
112
113
114
115
116
117
118
119
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 110

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