k_doc 0.0.28 → 0.0.36

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,41 +1,151 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KDoc
4
+ # Provide data load events, dependency and import management
5
+ #
4
6
  # A container acts a base data object for any data that requires tagging
5
7
  # such as unique key, type and namespace.
6
- # Rename: BlockProcessor
8
+ #
9
+ # example usage of the container using model as the basis:
10
+ # KDoc.model do
11
+ # init do
12
+ # context.some_data = :xmen
13
+ # end
14
+ # settings do
15
+ # name context.some_data
16
+ # end
17
+ # action
18
+ # puts context.some_data
19
+ # end
20
+ # end
7
21
  module BlockProcessor
8
- attr_accessor :block
9
- attr_accessor :block_state
22
+ include KLog::Logging
10
23
 
11
- # Proc/Handler to be called before evaluating when importing data
12
- attr_reader :on_init
24
+ attr_reader :block
25
+ attr_reader :block_state
13
26
 
14
- def initialize_block(opts, &block)
27
+ attr_reader :init_block
28
+ attr_reader :action_block
29
+ attr_reader :children
30
+
31
+ # TODO: Can dependencies be extracted to their own module?
32
+ attr_reader :depend_on_tags
33
+ attr_reader :dependents
34
+
35
+ def initialize_block_processor(_opts, &block)
15
36
  @block = block if block_given?
16
37
  @block_state = :new
17
- @on_init = opts.delete(:on_init)
38
+
39
+ @init_block = nil
40
+ @action_block = nil
41
+ @children = []
42
+
43
+ @depend_on_tags = []
44
+ @dependents = {}
45
+ @block_executed = false
46
+ end
47
+
48
+ def depend_on(*document_tags)
49
+ document_tags.each do |document_tag|
50
+ @depend_on_tags << document_tag
51
+ end
52
+ end
53
+
54
+ def resolve_dependency(document)
55
+ @dependents[document.tag] = document
56
+ end
57
+
58
+ def import(tag)
59
+ @dependents[tag]
60
+ end
61
+
62
+ def import_data(tag, as: :document)
63
+ doc = import(tag)
64
+
65
+ return nil unless doc&.data
66
+
67
+ # log.error 'about to import'
68
+ doc.debug(include_header: true)
69
+
70
+ return KUtil.data.to_open_struct(doc.data) if %i[open_struct ostruct].include?(as)
71
+
72
+ doc.data
73
+ end
74
+
75
+ def dependencies_met?
76
+ depend_on_tags.all? { |tag| dependents[tag] }
77
+ end
78
+
79
+ def execute_block(run_actions: false)
80
+ block_execute
81
+ fire_action if run_actions
82
+ end
83
+
84
+ def block_execute
85
+ return if @block_executed
86
+
87
+ # Evaluate the main block of code
88
+ fire_eval # aka primary eval
89
+
90
+ return unless dependencies_met?
91
+
92
+ # Call the block of code attached to the init method
93
+ fire_init
94
+
95
+ # Call the each block in the child array of blocks in the order of creation (FIFO)
96
+ fire_children
97
+
98
+ @block_executed = true
18
99
  end
19
100
 
101
+ # The underlying container is created and in the case of k_manager, registered
102
+ def new?
103
+ @block_state == :new
104
+ end
105
+
106
+ # The main block has been evaluated, but child blocks are still to be processed
20
107
  def evaluated?
21
- @block_state == :evaluated || @block_state == :actioned
108
+ @block_state == :evaluated || initialized?
109
+ end
110
+
111
+ # Has the init block been called?
112
+ def initialized?
113
+ @block_state == :initialized || children_evaluated?
22
114
  end
23
115
 
116
+ # The block and the data it represents has been evaluated.
117
+ def children_evaluated?
118
+ @block_state == :children_evaluated || actioned?
119
+ end
120
+
121
+ # The on_action method has been called.
24
122
  def actioned?
25
123
  @block_state == :actioned
26
124
  end
27
125
 
28
- def execute_block(run_actions: nil)
29
- run_on_init
126
+ def fire_eval
127
+ return unless new?
30
128
 
31
- # return unless dependencies_met?
129
+ instance_eval(&block) if block
32
130
 
33
- eval_block
34
- run_on_action if run_actions
131
+ @block_state = :evaluated
132
+ rescue StandardError => e
133
+ log.error('Standard error in document')
134
+ # puts "key #{unique_key}"
135
+ # puts "file #{KUtil.data.console_file_hyperlink(resource.file, resource.file)}"
136
+ log.error(e.message)
137
+ @error = e
138
+ raise
139
+ end
140
+
141
+ def init(&block)
142
+ @init_block = block
35
143
  end
36
144
 
37
- def run_on_init
38
- instance_eval(&on_init) if on_init
145
+ def fire_init
146
+ return unless evaluated?
147
+
148
+ instance_eval(&init_block) if init_block
39
149
 
40
150
  @block_state = :initialized
41
151
  rescue StandardError => e
@@ -47,24 +157,50 @@ module KDoc
47
157
  raise
48
158
  end
49
159
 
50
- def eval_block
51
- instance_eval(&block) if block
160
+ def add_child(block)
161
+ @children << block
162
+ end
52
163
 
53
- @block_state = :evaluated
164
+ # Run blocks associated with the children
165
+ #
166
+ # A child can follow one of three patterns:
167
+ # 1. A block that is evaluated immediately against the parent class
168
+ # 2. A class that has its own custom block evaluation
169
+ # 3. A class that has a block which will be evaluated immediately against the child class
170
+ # rubocop:disable Metrics/AbcSize
171
+ def fire_children
172
+ return unless initialized?
173
+
174
+ children.each do |child|
175
+ if child.is_a?(Proc)
176
+ instance_eval(&child)
177
+ elsif child.respond_to?(:fire_eval)
178
+ child.fire_eval
179
+ elsif child.respond_to?(:block)
180
+ child.instance_eval(&child.block)
181
+ end
182
+ end
183
+
184
+ @block_state = :children_evaluated
54
185
  rescue StandardError => e
55
- log.error('Standard error in document')
186
+ log.error('Standard error in document with one of the child blocks')
56
187
  # puts "key #{unique_key}"
57
188
  # puts "file #{KUtil.data.console_file_hyperlink(resource.file, resource.file)}"
58
189
  log.error(e.message)
59
190
  @error = e
60
191
  raise
61
192
  end
193
+ # rubocop:enable Metrics/AbcSize
194
+
195
+ def action(&block)
196
+ @action_block = block
197
+ end
62
198
 
63
- def run_on_action
64
- return unless block
199
+ def fire_action
200
+ return unless children_evaluated?
65
201
 
66
- if respond_to?(:on_action)
67
- on_action
202
+ if action_block
203
+ instance_eval(&action_block)
68
204
  @block_state = :actioned
69
205
  end
70
206
  rescue StandardError => e
@@ -76,10 +212,14 @@ module KDoc
76
212
  raise
77
213
  end
78
214
 
215
+ # rubocop:disable Metrics/AbcSize
79
216
  def debug_block_processor
80
- log.kv 'block_state', block_state , debug_pad_size if respond_to?(:block_state)
81
- log.kv 'evaluated' , evaluated? , debug_pad_size if respond_to?(:evaluated?)
82
- log.kv 'actioned' , actioned? , debug_pad_size if respond_to?(:actioned?)
217
+ log.kv 'block_state' , block_state , debug_pad_size
218
+ log.kv 'new' , new? , debug_pad_size
219
+ log.kv 'evaluated' , evaluated? , debug_pad_size
220
+ log.kv 'children_evaluated' , children_evaluated? , debug_pad_size
221
+ log.kv 'actioned' , actioned? , debug_pad_size
83
222
  end
223
+ # rubocop:enable Metrics/AbcSize
84
224
  end
85
225
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KDoc
4
- # Guarded provides parameter waring and guarding
4
+ # Guarded provides parameter warning and guarding
5
5
  #
6
6
  # TODO: this could be moved into KType or KGuard
7
7
  module Guarded
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KDoc
4
+ # Provide tagging functionality to the underlying model
5
+ #
4
6
  # A container acts a base data object for any data that requires tagging
5
7
  # such as unique key, type and namespace.
6
8
  module Taggable
data/lib/k_doc/model.rb CHANGED
@@ -23,20 +23,18 @@ module KDoc
23
23
 
24
24
  # Need to look at Director as an alternative to this technique
25
25
  def settings(key = nil, **setting_opts, &block)
26
- # TODO: add test
27
26
  if block.nil?
28
27
  log.warn 'You cannot call settings without a block. Did you mean to call data[:settings] or odata.settings?'
29
28
  return
30
29
  end
31
30
 
32
- setting_opts ||= {}
33
-
34
- setting_opts = {}.merge(opts) # Container options
31
+ setting_opts = {}.merge(opts) # Container options
35
32
  .merge(setting_opts) # Settings setting_opts
36
33
  .merge(parent: self)
37
34
 
38
- settings_instance(data, key, **setting_opts, &block)
39
- # settings.run_decorators(opts)
35
+ child = KDoc::Settings.new(self, data, key, **setting_opts, &block)
36
+
37
+ add_child(child)
40
38
  end
41
39
 
42
40
  def table(key = :table, **opts, &block)
@@ -45,9 +43,9 @@ module KDoc
45
43
  return
46
44
  end
47
45
 
48
- # NEED to add support for run_decorators I think
49
- opts.merge(parent: self)
50
- table_instance(data, key, **opts, &block)
46
+ child = KDoc::Table.new(self, data, key, **opts, &block)
47
+
48
+ add_child(child)
51
49
  end
52
50
  alias rows table
53
51
 
@@ -86,6 +84,11 @@ module KDoc
86
84
 
87
85
  raise KDoc::Error, "Node not found: #{node_name}" if node_data.nil?
88
86
 
87
+ if node_data.is_a?(Array)
88
+ puts 'why is this?'
89
+ return nil
90
+ end
91
+
89
92
  if node_data.keys.length == 2 && (node_data.key?('fields') && node_data.key?('rows'))
90
93
  :table
91
94
  else
@@ -138,15 +141,5 @@ module KDoc
138
141
  log.kv key, opts[key]
139
142
  end
140
143
  end
141
-
142
- private
143
-
144
- def settings_instance(data, key, **opts, &block)
145
- KDoc.opinion.settings_class.new(data, key, **opts, &block)
146
- end
147
-
148
- def table_instance(data, key, **opts, &block)
149
- KDoc.opinion.table_class.new(data, key, **opts, &block)
150
- end
151
144
  end
152
145
  end
@@ -1,40 +1,41 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
4
-
5
3
  module KDoc
6
4
  # Builds up key/value settings from the block
7
- # and applies them to a key coded node on the hash
5
+ #
8
6
  class Settings
9
7
  include KLog::Logging
10
- # include KDoc::Decorators
11
8
 
12
9
  attr_reader :parent
13
10
  attr_reader :key
14
11
  attr_reader :decorators
12
+ attr_reader :block
15
13
 
16
- alias kp parent
17
-
18
- def initialize(data, key = nil, **options, &block)
19
- initialize_attributes(data, key, **options)
14
+ def initialize(parent, data, key = nil, **opts, &block)
15
+ @parent = parent
16
+ @data = data
17
+ @key = (key || FakeOpinion.new.default_settings_key).to_s
18
+ @data[@key] = {}
19
+ @decorators = build_decorators(opts) # TODO: add tests for decorators
20
+ @block = block
21
+ end
20
22
 
21
- # Need a way to find out the line number for errors and report it correctly
22
- begin
23
- instance_eval(&block) if block_given?
23
+ def fire_eval
24
+ return unless block
24
25
 
25
- run_decorators
26
+ instance_eval(&block)
27
+ run_decorators
26
28
 
27
- # rubocop:disable Style/RescueStandardError
28
- rescue => e
29
- # rubocop:enable Style/RescueStandardError
30
- puts "Invalid code block in settings_dsl: #{@key}"
31
- puts e.message
32
- raise
33
- end
29
+ # rubocop:disable Style/RescueStandardError
30
+ rescue => e
31
+ # rubocop:enable Style/RescueStandardError
32
+ log.error("Standard error while running settings for key: #{@key}")
33
+ log.error(e.message)
34
+ raise
34
35
  end
35
36
 
36
- def imports
37
- parent.imports
37
+ def context
38
+ parent.context
38
39
  end
39
40
 
40
41
  # Return these settings which are attached to a data container using :key
@@ -58,7 +59,7 @@ module KDoc
58
59
  # puts "args.length : #{args.length}"
59
60
 
60
61
  if name != :type && !@parent.nil? && @parent.respond_to?(name)
61
- puts "NAME: #{name}"
62
+ puts "Settings.method_missing - NAME: #{name}"
62
63
  return @parent.public_send(name, *args, &block)
63
64
  end
64
65
  raise KDoc::Error, 'Multiple setting values is not supported' if args.length > 1
@@ -127,21 +128,11 @@ module KDoc
127
128
  decorators.each { |decorator| decorator.decorate(self, :settings) }
128
129
  end
129
130
 
130
- def initialize_attributes(data, key = nil, **options)
131
- @data = data
132
- @key = (key || FakeOpinion.new.default_settings_key).to_s
133
-
134
- @parent = options[:parent] if options.key?(:parent)
131
+ def build_decorators(opts)
132
+ decorator_list = opts[:decorators].nil? ? [] : opts[:decorators]
135
133
 
136
- decorator_list = options[:decorators].nil? ? [] : options[:decorators]
137
-
138
- # This code needs to work differently, it needs to support the 3 different types
139
- # Move the query into helpers
140
- @decorators = decorator_list
141
- .map(&:new)
134
+ decorator_list.map(&:new)
142
135
  .select { |decorator| decorator.compatible?(self) }
143
-
144
- @data[@key] = {}
145
136
  end
146
137
  end
147
138
  end
data/lib/k_doc/table.rb CHANGED
@@ -6,22 +6,38 @@ module KDoc
6
6
  include KLog::Logging
7
7
 
8
8
  attr_reader :parent
9
- attr_reader :name
9
+ attr_reader :key # used to be called name
10
10
  attr_reader :decorators
11
+ attr_reader :block
11
12
 
12
- def initialize(data, name = nil, **options, &block)
13
- initialize_attributes(data, name, **options)
13
+ def initialize(parent, data, key = nil, **opts, &block)
14
+ @parent = parent
15
+ @data = data
16
+ @key = (key || FakeOpinion.new.default_table_key).to_s
17
+ @data[@key] = { 'fields' => [], 'rows' => [] }
18
+ @decorators = build_decorators(opts)
19
+ @block = block
20
+ end
21
+
22
+ def fire_eval
23
+ return unless block
24
+
25
+ instance_eval(&block)
14
26
 
15
27
  @has_executed_field_decorators = false
16
28
  @has_executed_row_decorators = false
17
29
 
18
- instance_eval(&block) if block_given?
19
-
20
30
  run_decorators(:update_rows)
31
+
32
+ # rubocop:disable Style/RescueStandardError
33
+ rescue => e
34
+ # rubocop:enable Style/RescueStandardError
35
+ log.error("Table error for key: #{@key} - #{e.message}")
36
+ raise
21
37
  end
22
38
 
23
- def imports
24
- parent.imports
39
+ def context
40
+ parent.context
25
41
  end
26
42
 
27
43
  # Pass fields in using the following format
@@ -31,7 +47,7 @@ module KDoc
31
47
  def fields(*field_definitions)
32
48
  field_definitions = *field_definitions[0] if field_definitions.length == 1 && field_definitions[0].is_a?(Array)
33
49
 
34
- fields = @data[@name]['fields']
50
+ fields = @data[@key]['fields']
35
51
 
36
52
  field_definitions.each do |fd|
37
53
  fields << if fd.is_a?(String) || fd.is_a?(Symbol)
@@ -46,7 +62,7 @@ module KDoc
46
62
 
47
63
  # rubocop:disable Metrics/AbcSize
48
64
  def row(*args, **named_args)
49
- fields = @data[@name]['fields']
65
+ fields = @data[@key]['fields']
50
66
 
51
67
  raise KType::Error, "To many values for row, argument #{args.length}" if args.length > fields.length
52
68
 
@@ -63,31 +79,31 @@ module KDoc
63
79
  end
64
80
 
65
81
  # Override with named args
66
- named_args.each_key do |key|
67
- row[key.to_s] = named_args[key] # KUtil.data.clean_symbol(named_args[key])
82
+ named_args.each_key do |arg_name|
83
+ row[arg_name.to_s] = named_args[arg_name] # KUtil.data.clean_symbol(named_args[key])
68
84
  end
69
85
 
70
- @data[@name]['rows'] << row
86
+ @data[@key]['rows'] << row
71
87
  row
72
88
  end
73
89
  # rubocop:enable Metrics/AbcSize
74
90
 
75
91
  # rubocop:disable Naming/AccessorMethodName
76
92
  def get_fields
77
- @data[@name]['fields']
93
+ @data[@key]['fields']
78
94
  end
79
95
 
80
96
  def get_rows
81
- @data[@name]['rows']
97
+ @data[@key]['rows']
82
98
  end
83
99
  # rubocop:enable Naming/AccessorMethodName
84
100
 
85
101
  def internal_data
86
- @data[@name]
102
+ @data[@key]
87
103
  end
88
104
 
89
105
  def find_row(key, value)
90
- @data[@name]['rows'].find { |r| r[key] == value }
106
+ @data[@key]['rows'].find { |r| r[key] == value }
91
107
  end
92
108
 
93
109
  # Field definition
@@ -138,24 +154,12 @@ module KDoc
138
154
  end
139
155
  # rubocop:enable Metrics/CyclomaticComplexity
140
156
 
141
- # rubocop:disable Metrics/AbcSize
142
- def initialize_attributes(data, name = nil, **options)
143
- @data = data
144
- @name = (name || FakeOpinion.new.default_table_key.to_s).to_s
145
-
146
- @parent = options[:parent] if !options.nil? && options.key?(:parent)
157
+ def build_decorators(opts)
158
+ decorator_list = opts[:decorators].nil? ? [] : opts[:decorators]
147
159
 
148
- # This code needs to work differently, it needs to support the 3 different types
149
- # Move the query into helpers
150
- decorator_list = options[:decorators].nil? ? [] : options[:decorators]
151
-
152
- @decorators = decorator_list
153
- .map(&:new)
160
+ decorator_list.map(&:new)
154
161
  .select { |decorator| decorator.compatible?(self) }
155
-
156
- @data[@name] = { 'fields' => [], 'rows' => [] }
157
162
  end
158
- # rubocop:enable Metrics/AbcSize
159
163
 
160
164
  def respond_to_missing?(name, *_args, &_block)
161
165
  (!@parent.nil? && @parent.respond_to?(name, true)) || super
data/lib/k_doc/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KDoc
4
- VERSION = '0.0.28'
4
+ VERSION = '0.0.36'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: k_doc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.28
4
+ version: 0.0.36
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Cruwys
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-14 00:00:00.000000000 Z
11
+ date: 2022-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -88,14 +88,15 @@ executables: []
88
88
  extensions: []
89
89
  extra_rdoc_files: []
90
90
  files:
91
+ - ".builders/_.rb"
92
+ - ".builders/boot.rb"
91
93
  - ".builders/directors/_.rb"
92
94
  - ".builders/directors/build_actions.rb"
93
95
  - ".builders/directors/design_pattern_director.rb"
94
96
  - ".builders/directors/director.rb"
95
97
  - ".builders/directors/klue_director.rb"
96
- - ".builders/initializers/_.rb"
97
- - ".builders/initializers/configure_builder.rb"
98
- - ".builders/initializers/handlebars_helpers.rb"
98
+ - ".builders/generators/domain_diagram.rb"
99
+ - ".builders/generators/project_plan.rb"
99
100
  - ".builders/setup.rb"
100
101
  - ".github/workflows/main.yml"
101
102
  - ".gitignore"
@@ -115,6 +116,16 @@ files:
115
116
  - bin/kgitsync
116
117
  - bin/khotfix
117
118
  - bin/setup
119
+ - docs/class-diagram-dsl.png
120
+ - docs/class-diagram.png
121
+ - docs/domain-custom.drawio
122
+ - docs/domain-custom.svg
123
+ - docs/domain-modal.md
124
+ - docs/domain.drawio
125
+ - docs/domain_model.svg
126
+ - docs/project-plan.drawio
127
+ - docs/project-plan.md
128
+ - docs/project_in_progress.svg
118
129
  - hooks/pre-commit
119
130
  - hooks/update-version
120
131
  - k_doc.gemspec
@@ -132,7 +143,6 @@ files:
132
143
  - lib/k_doc/mixins/guarded.rb
133
144
  - lib/k_doc/mixins/taggable.rb
134
145
  - lib/k_doc/model.rb
135
- - lib/k_doc/model_backup.rb
136
146
  - lib/k_doc/settings.rb
137
147
  - lib/k_doc/table.rb
138
148
  - lib/k_doc/version.rb
@@ -1,7 +0,0 @@
1
- require 'k_log'
2
- require 'k_util'
3
-
4
- include KLog::Logging
5
-
6
- require_relative './configure_builder'
7
- require_relative './handlebars_helpers'
@@ -1,38 +0,0 @@
1
- KBuilder.reset(:gem)
2
- KConfig.configure(:gem) do |config|
3
- # config.template_folders.add(:global , '~/dev/kgems/k_templates/definitions/genesis')
4
- config.target_folders.add(:root , File.expand_path('..', Dir.getwd))
5
- config.target_folders.add(:lib , :root, 'lib', 'k_doc')
6
- config.target_folders.add(:spec , :root, 'spec', 'k_doc')
7
- end
8
-
9
- KBuilder.reset(:design_patterns)
10
- KConfig.configure(:design_patterns) do |config|
11
- config.template_folders.add(:global , '~/dev/kgems/k_templates/templates/ruby_design_patterns')
12
- config.target_folders.add(:root , File.expand_path('..', Dir.getwd))
13
- config.target_folders.add(:lib , :root, 'lib', 'k_doc')
14
- config.target_folders.add(:spec , :root, 'spec', 'k_doc')
15
- end
16
-
17
- # KConfig.configuration(:gem).debug
18
-
19
- # def builder
20
- # @builder ||= KBuilder::BaseBuilder.init
21
- # end
22
-
23
- puts ':)'
24
-
25
- # Setup.configure(:microapp) do |config|
26
- # config.template_folders.add(:microapp , '~/dev/kgems/k_templates/definitions/microapp')
27
- # config.target_folders.add(:root , Dir.getwd)
28
- # end
29
-
30
- # Setup.configure(:data) do |config|
31
- # # config.template_folders.add(:microapp , '~/dev/kgems/k_templates/definitions/microapp')
32
- # config.target_folders.add(:root , File.expand_path('..', Dir.getwd), '.data')
33
- # end
34
-
35
- # Setup.configure(:actual_app) do |config|
36
- # # config.template_folders.add(:microapp , '~/dev/kgems/k_templates/definitions/microapp')
37
- # config.target_folders.add(:root , File.expand_path('..', Dir.getwd))
38
- # end