Module: KDoc::Datum
Overview
Data acts as a base data object containers
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
- #clear_data ⇒ Object
- #default_data_type ⇒ Object
- #initialize_data(opts) ⇒ Object
-
#set_data(value, data_action: :replace) ⇒ Object
Write data object.
Methods included from Guarded
#clear_errors, #error_hash, #error_messages, #errors, #guard, #log_any_messages, #valid?, #warn
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
8 9 10 |
# File 'lib/k_doc/mixins/datum.rb', line 8 def data @data end |
Instance Method Details
#clear_data ⇒ Object
41 42 43 |
# File 'lib/k_doc/mixins/datum.rb', line 41 def clear_data @data.clear end |
#default_data_type ⇒ Object
17 18 19 |
# File 'lib/k_doc/mixins/datum.rb', line 17 def default_data_type raise 'Implement default_data_type in container' unless @default_data_type end |
#initialize_data(opts) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/k_doc/mixins/datum.rb', line 10 def initialize_data(opts) @default_data_type = opts.delete(:default_data_type) if opts.key?(:default_data_type) @data = opts.delete(:data) || opts.delete(:default_data) || default_data_type.new warn("Incompatible data type - #{default_data_type} is incompatible with #{data.class} in constructor") unless data.is_a?(default_data_type) end |
#set_data(value, data_action: :replace) ⇒ Object
Write data object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/k_doc/mixins/datum.rb', line 27 def set_data(value, data_action: :replace) warn("Incompatible data type - #{default_data_type} is incompatible with #{value.class} in set data") unless value.is_a?(default_data_type) case data_action when :replace @data = value when :append @data.merge!(value) if @data.is_a?(Hash) @data += value if @data.is_a?(Array) else warn("Unknown data_action: #{data_action}") end end |