Class: KDoc::Model
- 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
Attributes included from BlockProcessor
Attributes included from Datum
Attributes included from Taggable
Instance Method Summary collapse
- #data_struct ⇒ Object
-
#debug(include_header: false) ⇒ Object
Move this out to the logger function when it has been refactor.
- #debug_header ⇒ Object
- #debug_header_keys ⇒ Object
- #default_container_type ⇒ Object
- #get_node_type(node_name) ⇒ Object
-
#initialize(key = nil, **opts, &block) ⇒ Model
constructor
include KType::Error include KType::ManagedState include KType::NamedFolder include KType::LayeredFolder.
-
#odata ⇒ Object
Need to move this down to container Need to use some sort of cache invalidation to know if the internal data has been altered.
- #oraw ⇒ Object
-
#raw_data ⇒ Object
Removes any meta data eg.
-
#raw_data_struct ⇒ Object
alias d data_struct.
-
#settings(key = nil, **setting_opts, &block) ⇒ Object
Need to look at Director as an alternative to this technique.
- #table(key = :table, **opts, &block) ⇒ Object (also: #rows)
Methods inherited from Container
Methods included from BlockProcessor
#eval_block, #execute_block, #initialize_block, #run_on_action
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_struct ⇒ Object
59 60 61 |
# File 'lib/k_doc/model.rb', line 59 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
108 109 110 111 112 113 114 |
# File 'lib/k_doc/model.rb', line 108 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_header ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/k_doc/model.rb', line 116 def debug_header log.heading self.class.name debug_container debug_header_keys log.line end |
#debug_header_keys ⇒ Object
124 125 126 127 128 |
# File 'lib/k_doc/model.rb', line 124 def debug_header_keys opts&.keys&.reject { |k| k == :namespace }&.each do |key| log.kv key, opts[key] end end |
#default_container_type ⇒ Object
68 69 70 |
# File 'lib/k_doc/model.rb', line 68 def default_container_type KDoc.opinion.default_model_type end |
#get_node_type(node_name) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/k_doc/model.rb', line 72 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.keys.length == 2 && (node_data.key?('fields') && node_data.key?('rows')) :table else :settings end end |
#odata ⇒ Object
Need to move this down to container Need to use some sort of cache invalidation to know if the internal data has been altered
51 52 53 |
# File 'lib/k_doc/model.rb', line 51 def odata @odata ||= data_struct end |
#oraw ⇒ Object
55 56 57 |
# File 'lib/k_doc/model.rb', line 55 def oraw @oraw ||= raw_data_struct end |
#raw_data ⇒ Object
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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/k_doc/model.rb', line 87 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_struct ⇒ Object
alias d data_struct
64 65 66 |
# File 'lib/k_doc/model.rb', line 64 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 |
# File 'lib/k_doc/model.rb', line 25 def settings(key = nil, **setting_opts, &block) setting_opts ||= {} setting_opts = {}.merge(opts) # Container options .merge(setting_opts) # Settings setting_opts .merge(parent: self) settings_instance(data, key, **setting_opts, &block) # settings.run_decorators(opts) end |
#table(key = :table, **opts, &block) ⇒ Object Also known as: rows
36 37 38 39 40 |
# File 'lib/k_doc/model.rb', line 36 def table(key = :table, **opts, &block) # NEED to add support for run_decorators I think opts.merge(parent: self) table_instance(data, key, **opts, &block) end |