Class: ActiveFedora::RegisteredAttributes::Attribute
- Inherits:
-
Object
- Object
- ActiveFedora::RegisteredAttributes::Attribute
- Defined in:
- lib/active_fedora/registered_attributes/attribute.rb
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:
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 = .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
#datastream ⇒ Object (readonly)
Returns the value of attribute datastream.
5 6 7 |
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 5 def datastream @datastream end |
#name ⇒ Object (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 = [: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
55 56 57 |
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 55 def displayable? @options.fetch(:displayable, true) end |
#editable? ⇒ Boolean
59 60 61 |
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 59 def editable? @options.fetch(:editable, true) end |
#label ⇒ Object
63 64 65 66 |
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 63 def label default = [:label] || name.to_s.humanize context_class.human_attribute_name(name, default: default) end |
#multiple? ⇒ Boolean
51 52 53 |
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 51 def multiple? [: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 (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
80 81 82 |
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 80 def yield(name, {}) if with_accession? end |
#with_delegation_options {|name, options_for_delegation| ... } ⇒ Object
68 69 70 |
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 68 def yield(name, ) if with_delegation? end |
#with_validation_options {|name, | ... } ⇒ Object
74 75 76 |
# File 'lib/active_fedora/registered_attributes/attribute.rb', line 74 def yield(name, [: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 |