Module: KDoc::Datum

Includes:
Guarded
Included in:
Container
Defined in:
lib/k_doc/mixins/datum.rb

Overview

Data acts as a base data object containers

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Guarded

#clear_errors, #error_hash, #error_messages, #errors, #guard, #log_any_messages, #valid?, #warn

Instance Attribute Details

#dataObject (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_dataObject



41
42
43
# File 'lib/k_doc/mixins/datum.rb', line 41

def clear_data
  @data.clear
end

#default_data_typeObject



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

Parameters:

  • value (Object)

    A compatible data object to be stored against .data property

  • data_action (Symbol) (defaults to: :replace)

    The data_action to take when setting data, defaults to :replace

  • data_action (:replace) (defaults to: :replace)

    :replace will replace the existing data instance with the incoming data value

  • data_action (:append) (defaults to: :replace)

    :append will keep existing data and then new value data over the top



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