Class: KDoc::Model

Inherits:
Container show all
Includes:
KLog::Logging
Defined in:
lib/k_doc/model.rb

Overview

Model is a DSL for modeling general purpose data objects

A model can have

- 0 or more named setting groups each with their key/value pairs
- 0 or more named table groups each with their own columns and rows

A settings group without a name will default to name: :settings A table group without a name will default to name: :table

Instance Attribute Summary

Attributes inherited from Container

#context, #opts, #owner

Attributes included from BlockProcessor

#action_block, #block, #block_state, #children, #depend_on_tags, #dependents, #init_block

Attributes included from Datum

#data

Attributes included from Taggable

#tag_options

Instance Method Summary collapse

Methods inherited from Container

#default_data_type, #os, #owned?

Methods included from BlockProcessor

#action, #actioned?, #add_child, #block_execute, #children_evaluated?, #debug_block_processor, #depend_on, #dependencies_met?, #evaluated?, #execute_block, #fire_action, #fire_children, #fire_eval, #fire_init, #import, #import_data, #init, #initialize_block_processor, #initialized?, #new?, #resolve_dependency

Methods included from Datum

#clear_data, #default_data_type, #initialize_data, #set_data

Methods included from Guarded

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

Methods included from Taggable

#initialize_tag, #key, #namespace, #project, #tag, #type

Constructor Details

#initialize(key = nil, **opts, &block) ⇒ Model

include KType::Error include KType::ManagedState include KType::NamedFolder include KType::LayeredFolder



20
21
22
# File 'lib/k_doc/model.rb', line 20

def initialize(key = nil, **opts, &block)
  super(**{ key: key }.merge(opts), &block)
end

Instance Method Details

#data_structObject



68
69
70
# File 'lib/k_doc/model.rb', line 68

def data_struct
  KUtil.data.to_open_struct(data)
end

#debug(include_header: false) ⇒ Object

Move this out to the logger function when it has been refactor



122
123
124
125
126
127
128
# File 'lib/k_doc/model.rb', line 122

def debug(include_header: false)
  debug_header if include_header

  # tp dsls.values, :k_key, :k_type, :state, :save_at, :last_at, :data, :last_data, :source, { :file => { :width => 150 } }
  # puts JSON.pretty_generate(data)
  log.o(raw_data_struct)
end

#debug_headerObject



130
131
132
133
134
135
136
137
# File 'lib/k_doc/model.rb', line 130

def debug_header
  log.heading self.class.name
  debug_taggable
  debug_block_processor
  debug_header_keys

  log.line
end

#debug_header_keysObject



139
140
141
142
143
# File 'lib/k_doc/model.rb', line 139

def debug_header_keys
  opts&.keys&.reject { |k| k == :namespace }&.each do |key|
    log.kv key, opts[key]
  end
end

#default_container_typeObject



77
78
79
# File 'lib/k_doc/model.rb', line 77

def default_container_type
  KDoc.opinion.default_model_type
end

#get_node_type(node_name) ⇒ Object

Raises:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/k_doc/model.rb', line 81

def get_node_type(node_name)
  node_name = KUtil.data.clean_symbol(node_name)
  node_data = data[node_name]

  raise KDoc::Error, "Node not found: #{node_name}" if node_data.nil?

  if node_data.is_a?(Array)
    puts 'why is this?'
    return nil
  end

  if node_data.keys.length == 2 && (node_data.key?('fields') && node_data.key?('rows'))
    :table
  else
    :settings
  end
end

#odataObject

Need to move this down to container Need to use some sort of cache invalidation to know if the internal data has been altered



60
61
62
# File 'lib/k_doc/model.rb', line 60

def odata
  @odata ||= data_struct
end

#orawObject



64
65
66
# File 'lib/k_doc/model.rb', line 64

def oraw
  @oraw ||= raw_data_struct
end

#raw_dataObject

Removes any meta data eg. “fields” from a table and just returns the raw data REFACTOR: IT MAY BE BEST TO MOVE raw_data into each of the node_types



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/k_doc/model.rb', line 101

def raw_data
  # REFACT, what if this is CSV, meaning it is just an array?
  #         add specs
  result = data

  result.each_key do |key|
    # ANTI: get_node_type uses @data while we are using @data.clone here
    result[key] = if get_node_type(key) == :table
                    # Old format was to keep the rows and delete the fields
                    # Now the format is to pull the row_value up to the key and remove rows and fields
                    # result[key].delete('fields')
                    result[key]['rows']
                  else
                    result[key]
                  end
  end

  result
end

#raw_data_structObject

alias d data_struct



73
74
75
# File 'lib/k_doc/model.rb', line 73

def raw_data_struct
  KUtil.data.to_open_struct(raw_data)
end

#settings(key = nil, **setting_opts, &block) ⇒ Object

Need to look at Director as an alternative to this technique



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/k_doc/model.rb', line 25

def settings(key = nil, **setting_opts, &block)
  if block.nil?
    log.warn 'You cannot call settings without a block. Did you mean to call data[:settings] or odata.settings?'
    return
  end

  setting_opts = {}.merge(opts)         # Container options
                   .merge(setting_opts) # Settings setting_opts
                   .merge(parent: self)

  child = KDoc::Settings.new(self, data, key, **setting_opts, &block)

  add_child(child)
end

#table(key = :table, **opts, &block) ⇒ Object Also known as: rows



40
41
42
43
44
45
46
47
48
49
# File 'lib/k_doc/model.rb', line 40

def table(key = :table, **opts, &block)
  if block.nil?
    log.warn 'You cannot call table without a block. Did you mean to call data[:table] or odata.table?'
    return
  end

  child = KDoc::Table.new(self, data, key, **opts, &block)

  add_child(child)
end