Class: ActiveFedora::RegisteredAttributes::Attribute
- Inherits:
-
Object
- Object
- ActiveFedora::RegisteredAttributes::Attribute
- 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
-
#datastream ⇒ Object
readonly
Returns the value of attribute datastream.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #default(context) ⇒ Object
- #displayable? ⇒ Boolean
- #editable? ⇒ Boolean
-
#initialize(context_class, name, options = {}) ⇒ Attribute
constructor
Parameters:.
- #label ⇒ Object
- #multiple? ⇒ Boolean
- #options_for_input(overrides = {}) ⇒ Object
- #with_accession_options {|name, {}| ... } ⇒ Object
- #with_delegation_options {|name, options_for_delegation| ... } ⇒ Object
- #with_validation_options {|name, | ... } ⇒ Object
- #wrap_reader_method(context) ⇒ Object
- #wrap_writer_method(context) ⇒ Object
Constructor Details
#initialize(context_class, name, options = {}) ⇒ Attribute
Parameters:
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 = .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
#datastream ⇒ Object (readonly)
Returns the value of attribute datastream.
10 11 12 |
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 10 def datastream @datastream end |
#name ⇒ Object (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 = [: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
60 61 62 |
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 60 def displayable? @options.fetch(:displayable, true) end |
#editable? ⇒ Boolean
64 65 66 |
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 64 def editable? @options.fetch(:editable, true) end |
#label ⇒ Object
68 69 70 71 |
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 68 def label default = [:label] || name.to_s.humanize context_class.human_attribute_name(name, default: default) end |
#multiple? ⇒ Boolean
56 57 58 |
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 56 def multiple? [: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 (overrides = {}) [:form].tap {|hash| hash[:hint] ||= [:hint] if [:hint] hash[:label] ||= [:label] if [: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
85 86 87 |
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 85 def yield(name, {}) if with_accession? end |
#with_delegation_options {|name, options_for_delegation| ... } ⇒ Object
73 74 75 |
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 73 def yield(name, ) if with_delegation? end |
#with_validation_options {|name, | ... } ⇒ Object
79 80 81 |
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 79 def yield(name, [: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 |