k_doc 0.0.18 → 0.0.24
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.builders/directors/_.rb +10 -0
- data/.builders/directors/build_actions.rb +39 -0
- data/.builders/directors/design_pattern_director.rb +39 -0
- data/.builders/directors/director.rb +185 -0
- data/.builders/directors/klue_director.rb +5 -0
- data/.builders/initializers/_.rb +7 -0
- data/.builders/initializers/configure_builder.rb +38 -0
- data/.builders/initializers/handlebars_helpers.rb +27 -0
- data/.builders/setup.rb +18 -0
- data/Guardfile +1 -1
- data/k_doc.gemspec +3 -0
- data/lib/k_doc/action.rb +28 -0
- data/lib/k_doc/container.rb +38 -39
- data/lib/k_doc/csv_doc.rb +64 -0
- data/lib/k_doc/fake_opinion.rb +11 -1
- data/lib/k_doc/json_doc.rb +58 -0
- data/lib/k_doc/mixins/block_processor.rb +45 -0
- data/lib/k_doc/mixins/composable_components.rb +38 -0
- data/lib/k_doc/mixins/datum.rb +45 -0
- data/lib/k_doc/mixins/guarded.rb +50 -0
- data/lib/k_doc/mixins/taggable.rb +118 -0
- data/lib/k_doc/model.rb +35 -80
- data/lib/k_doc/model_backup.rb +191 -0
- data/lib/k_doc/table.rb +1 -4
- data/lib/k_doc/version.rb +1 -1
- data/lib/k_doc/yaml_doc.rb +63 -0
- data/lib/k_doc.rb +47 -2
- metadata +23 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62acce8bfac2c242082dd6cf050db2d7e6c535bc6af229f50b4185aec7573261
|
4
|
+
data.tar.gz: 23b23c0dffa5da8de05f88c0f470aa63dbdfe3f734582872debf0a74edd1c8fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3762d2ab76f5d4128a2330ea7ad84db2234bea4a56bec313ade497691a46df0f0e20aec4445268647b49d2d68279c3c5730d0ebdecd0b2dba0e48d15b7d77ded
|
7
|
+
data.tar.gz: 8e9054976e5e0fbe40e9a27cb3e5244cbd451b5d67c30246d874348b75fd17877774ac0dd819da017db8a3c02439e7fe70aa1fee9887c292d4d0c8997187fad6
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module BuildActions
|
2
|
+
def cd(folder_key)
|
3
|
+
@builder.cd(folder_key)
|
4
|
+
|
5
|
+
self
|
6
|
+
end
|
7
|
+
|
8
|
+
def add_file(file, **opts)
|
9
|
+
@builder.add_file(file, **opts)
|
10
|
+
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
def delete_file(file, **opts)
|
15
|
+
@builder.delete_file(file, **opts)
|
16
|
+
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_clipboard(**opts)
|
21
|
+
@builder.add_clipboard(**opts)
|
22
|
+
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def open
|
27
|
+
builder.open
|
28
|
+
|
29
|
+
self
|
30
|
+
end
|
31
|
+
alias o open
|
32
|
+
|
33
|
+
def open_template
|
34
|
+
builder.open_template
|
35
|
+
|
36
|
+
self
|
37
|
+
end
|
38
|
+
alias ot open_template
|
39
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class DesignPatternDirector < Director
|
2
|
+
include BuildActions
|
3
|
+
|
4
|
+
def del(file, *_params, **opts)
|
5
|
+
delete_file(file, **opts)
|
6
|
+
|
7
|
+
self
|
8
|
+
end
|
9
|
+
alias dadd del
|
10
|
+
|
11
|
+
def add(output_file, template_file = nil, **opts)
|
12
|
+
template_file = output_file if template_file.nil?
|
13
|
+
|
14
|
+
opts = {
|
15
|
+
template_file: template_file,
|
16
|
+
on_exist: :write
|
17
|
+
}.merge(opts)
|
18
|
+
|
19
|
+
add_file(output_file, **opts)
|
20
|
+
puts output_file
|
21
|
+
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
# This pattern would be great as a configurable generic class
|
26
|
+
#
|
27
|
+
# children could be renamed as:
|
28
|
+
# - directors
|
29
|
+
# - components
|
30
|
+
# - elements
|
31
|
+
def composite(name:, relative_path:, **opts)
|
32
|
+
opts = { name: name }.merge(opts)
|
33
|
+
cd(:lib)
|
34
|
+
add(File.join(relative_path, "#{name}.rb"), "composite/composite.rb", **opts)
|
35
|
+
cd(:spec)
|
36
|
+
add(File.join(relative_path, "#{name}_spec.rb"), "composite/composite_spec.rb", **opts)
|
37
|
+
self
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,185 @@
|
|
1
|
+
class Director
|
2
|
+
include KType::Composite
|
3
|
+
# Should this be on the base class, or setup as needed on child directors?
|
4
|
+
|
5
|
+
attr_reader :builder
|
6
|
+
|
7
|
+
# Maybe Deprecate
|
8
|
+
attr_reader :director_types
|
9
|
+
|
10
|
+
def initialize(parent = nil, **opts)
|
11
|
+
attach_parent(parent)
|
12
|
+
|
13
|
+
reset_settings
|
14
|
+
process_options(opts)
|
15
|
+
end
|
16
|
+
|
17
|
+
def init(opts)
|
18
|
+
reset_settings
|
19
|
+
process_options(opts)
|
20
|
+
|
21
|
+
self
|
22
|
+
rescue => ex
|
23
|
+
log.exception(ex)
|
24
|
+
end
|
25
|
+
|
26
|
+
def reset
|
27
|
+
reset_settings
|
28
|
+
|
29
|
+
self
|
30
|
+
rescue => ex
|
31
|
+
log.exception(ex)
|
32
|
+
end
|
33
|
+
|
34
|
+
def register(name, klass)
|
35
|
+
register_director(name, klass)
|
36
|
+
|
37
|
+
self
|
38
|
+
rescue => ex
|
39
|
+
log.exception(ex)
|
40
|
+
end
|
41
|
+
|
42
|
+
def back
|
43
|
+
navigate_parent
|
44
|
+
end
|
45
|
+
|
46
|
+
def with(director, &block)
|
47
|
+
change_director = director(director)
|
48
|
+
change_director.instance_eval(&block)
|
49
|
+
navigate_parent
|
50
|
+
end
|
51
|
+
|
52
|
+
def help(*topics) #**opts)
|
53
|
+
if topics.blank?
|
54
|
+
help_actions
|
55
|
+
return
|
56
|
+
end
|
57
|
+
|
58
|
+
help_actions if topics.include?(:all)
|
59
|
+
help_channels if topics.include?(:channels)
|
60
|
+
rescue => ex
|
61
|
+
log.exception(ex)
|
62
|
+
end
|
63
|
+
|
64
|
+
def director(name)
|
65
|
+
@director ||= Hash.new do |add_to_hash, key|
|
66
|
+
raise "Director not registered: #{key}" unless has_child?(key)
|
67
|
+
director = get_child(key)
|
68
|
+
add_to_hash[key] = director.klass.new(self).init(builder_config_name: key)
|
69
|
+
end
|
70
|
+
@director[name]
|
71
|
+
rescue => ex
|
72
|
+
log.exception(ex)
|
73
|
+
end
|
74
|
+
|
75
|
+
def guard(message)
|
76
|
+
log.error(message)
|
77
|
+
|
78
|
+
self
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
# def director(type, name)
|
83
|
+
# Setup.config(type).debug
|
84
|
+
# end
|
85
|
+
|
86
|
+
def debug
|
87
|
+
# puts 'debug'
|
88
|
+
# puts children
|
89
|
+
# puts director_types
|
90
|
+
if builder
|
91
|
+
builder.debug
|
92
|
+
else
|
93
|
+
log.error "No builder"
|
94
|
+
end
|
95
|
+
|
96
|
+
director_types.keys.each do |key|
|
97
|
+
log.section_heading(key)
|
98
|
+
log.structure director_types[key]
|
99
|
+
end
|
100
|
+
|
101
|
+
self
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
# Composite Pattern Custom Implementation - BEGIN
|
107
|
+
|
108
|
+
def has_child?(key)
|
109
|
+
@children.key?(key)
|
110
|
+
end
|
111
|
+
|
112
|
+
def get_child(key)
|
113
|
+
@children[key]
|
114
|
+
end
|
115
|
+
|
116
|
+
def add_child(key, child)
|
117
|
+
@children[key] = child
|
118
|
+
end
|
119
|
+
|
120
|
+
def reset_children
|
121
|
+
@children = {}
|
122
|
+
end
|
123
|
+
|
124
|
+
# Composite Pattern Custom Implementation - END
|
125
|
+
|
126
|
+
def register_director(name, klass)
|
127
|
+
if has_child?(name)
|
128
|
+
log.error "Director has already been registered: #{name}"
|
129
|
+
return
|
130
|
+
end
|
131
|
+
|
132
|
+
add_director(name, klass)
|
133
|
+
rescue => ex
|
134
|
+
log.exception(ex)
|
135
|
+
end
|
136
|
+
|
137
|
+
def add_director(name, klass)
|
138
|
+
director = OpenStruct.new(name: name, klass: klass)
|
139
|
+
|
140
|
+
# Adding a director should create a new method for accessing that director
|
141
|
+
add_child(name, director)
|
142
|
+
rescue => ex
|
143
|
+
log.exception(ex)
|
144
|
+
end
|
145
|
+
|
146
|
+
# HELP
|
147
|
+
|
148
|
+
def help_actions
|
149
|
+
log.warn 'Help about actions available from this class'
|
150
|
+
end
|
151
|
+
|
152
|
+
def help_channels
|
153
|
+
log.warn 'Help about channels'
|
154
|
+
debug
|
155
|
+
end
|
156
|
+
|
157
|
+
def reset_settings
|
158
|
+
@builder = nil
|
159
|
+
reset_children
|
160
|
+
@director_types = {}
|
161
|
+
end
|
162
|
+
|
163
|
+
def process_options(opts)
|
164
|
+
attach_builder(opts)
|
165
|
+
rescue => ex
|
166
|
+
log.exception(ex)
|
167
|
+
end
|
168
|
+
|
169
|
+
# If builder is provided then use it
|
170
|
+
# If builder configuration is provided
|
171
|
+
# If builder config_name is provided
|
172
|
+
|
173
|
+
def attach_builder(opts)
|
174
|
+
return if @builder
|
175
|
+
|
176
|
+
log.warning("options :builder and :builder_config_name are mutually exclusive, will use opts[:builder]") if opts[:builder] && opts[:builder_config_name]
|
177
|
+
|
178
|
+
# Use the provided builder
|
179
|
+
@builder = opts[:builder] if opts[:builder]
|
180
|
+
|
181
|
+
return if @builder
|
182
|
+
|
183
|
+
@builder = KBuilder::BaseBuilder.init(KBuilder.configuration(opts[:builder_config_name]))
|
184
|
+
end
|
185
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
KBuilder.reset(:gem)
|
2
|
+
KBuilder.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
|
+
KBuilder.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
|
+
# KBuilder.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
|
@@ -0,0 +1,27 @@
|
|
1
|
+
Handlebars::Helpers.configure do |config|
|
2
|
+
config_file = File.join(Gem.loaded_specs['handlebars-helpers'].full_gem_path, '.handlebars_helpers.json')
|
3
|
+
config.helper_config_file = config_file
|
4
|
+
|
5
|
+
string_config_file = File.join(Gem.loaded_specs['handlebars-helpers'].full_gem_path, '.handlebars_string_formatters.json')
|
6
|
+
config.string_formatter_config_file = string_config_file
|
7
|
+
end
|
8
|
+
|
9
|
+
def camel
|
10
|
+
require 'handlebars/helpers/string_formatting/camel'
|
11
|
+
Handlebars::Helpers::StringFormatting::Camel.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def titleize
|
15
|
+
require 'handlebars/helpers/string_formatting/titleize'
|
16
|
+
Handlebars::Helpers::StringFormatting::Titleize.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def pluralize
|
20
|
+
require 'handlebars/helpers/inflection/pluralize'
|
21
|
+
Handlebars::Helpers::Inflection::Pluralize.new
|
22
|
+
end
|
23
|
+
|
24
|
+
def singularize
|
25
|
+
require 'handlebars/helpers/inflection/singularize'
|
26
|
+
Handlebars::Helpers::Inflection::Singularize.new
|
27
|
+
end
|
data/.builders/setup.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'initializers/_'
|
2
|
+
require 'directors/_'
|
3
|
+
|
4
|
+
klue
|
5
|
+
.reset
|
6
|
+
.register(:design_patterns , DesignPatternDirector)
|
7
|
+
|
8
|
+
klue
|
9
|
+
.with(:design_patterns) do
|
10
|
+
composite(
|
11
|
+
relative_path: 'mixins',
|
12
|
+
name: :composite_spec,
|
13
|
+
namespace: 'KDoc',
|
14
|
+
children_name: :child,
|
15
|
+
children_name_plural: :children)
|
16
|
+
end
|
17
|
+
|
18
|
+
puts '...'
|
data/Guardfile
CHANGED
@@ -19,7 +19,7 @@ group :green_pass_then_cop, halt_on_fail: true do
|
|
19
19
|
# Ruby files
|
20
20
|
ruby = dsl.ruby
|
21
21
|
dsl.watch_spec_files_for(ruby.lib_files)
|
22
|
-
watch(%r{^lib/k_doc
|
22
|
+
watch(%r{^lib/k_doc/**/(.+)\.rb$}) { |m| "spec/unit/#{m[1]}_spec.rb" }
|
23
23
|
watch(%r{^lib/k_doc/commands/(.+)\.rb$}) { |m| "spec/unit/commands/#{m[1]}_spec.rb" }
|
24
24
|
end
|
25
25
|
|
data/k_doc.gemspec
CHANGED
data/lib/k_doc/action.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KDoc
|
4
|
+
# Action is a DSL for modeling JSON data objects
|
5
|
+
class Action < KDoc::Container
|
6
|
+
attr_reader :file
|
7
|
+
|
8
|
+
# Simple Ruby Action
|
9
|
+
#
|
10
|
+
# @param [String|Symbol] name Name of the document
|
11
|
+
# @param args[0] Type of the document, defaults to KDoc:: FakeOpinion.new.default_action_type if not set
|
12
|
+
# @param default: Default value (using named params), as above
|
13
|
+
# @param [Proc] block The block is stored and accessed different types in the document loading workflow.
|
14
|
+
def initialize(key = nil, **opts, &_block)
|
15
|
+
super(**{ key: key }.merge(opts))
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def default_data_type
|
21
|
+
String
|
22
|
+
end
|
23
|
+
|
24
|
+
def default_container_type
|
25
|
+
:action
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/k_doc/container.rb
CHANGED
@@ -1,50 +1,49 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module KDoc
|
4
|
-
# A
|
5
|
-
# unique key, type and namespace.
|
4
|
+
# A container acts a base data object for any data that requires tagging
|
5
|
+
# such as unique key, type and namespace.
|
6
6
|
class Container
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
#
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
#
|
29
|
-
# Any container can be uniquely identified via it's
|
30
|
-
# key, type, namespace and project_key attributes
|
31
|
-
#
|
32
|
-
# @param [Hash] **opts The options
|
33
|
-
# @option opts [String|Symbol] name Name of the container
|
34
|
-
# @option opts [String|Symbol] type Type of the container, defaults to KDoc:: FakeOpinion.new.default_model_type if not set
|
35
|
-
def initialize(**opts)
|
36
|
-
@key = opts[:key] || SecureRandom.alphanumeric(4)
|
37
|
-
@type = opts[:type] || ''
|
38
|
-
@data = opts[:data] || {}
|
7
|
+
include KLog::Logging
|
8
|
+
include KDoc::Guarded
|
9
|
+
include KDoc::Taggable
|
10
|
+
include KDoc::Datum
|
11
|
+
include KDoc::BlockProcessor
|
12
|
+
|
13
|
+
attr_reader :opts
|
14
|
+
|
15
|
+
# TODO: Owner/Owned need to be in a module and tested
|
16
|
+
attr_accessor :owner
|
17
|
+
|
18
|
+
def owned?
|
19
|
+
@owner != nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(**opts, &block)
|
23
|
+
@opts = opts
|
24
|
+
|
25
|
+
initialize_tag(opts)
|
26
|
+
initialize_data(opts)
|
27
|
+
initialize_block(opts, &block)
|
39
28
|
end
|
40
29
|
|
41
|
-
def
|
42
|
-
|
43
|
-
log.kv 'type' , type , 15
|
30
|
+
def default_container_type
|
31
|
+
:container
|
44
32
|
end
|
45
33
|
|
46
|
-
def
|
47
|
-
@
|
34
|
+
def default_data_type
|
35
|
+
@default_data_type ||= Hash
|
36
|
+
end
|
37
|
+
|
38
|
+
def debug
|
39
|
+
debug_container
|
40
|
+
debug_errors
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def debug_errors
|
46
|
+
log.block(errors, title: 'errors') unless valid?
|
48
47
|
end
|
49
48
|
end
|
50
49
|
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'csv'
|
4
|
+
|
5
|
+
module KDoc
|
6
|
+
# CsvDoc is a DSL for modeling CSV data objects
|
7
|
+
class CsvDoc < KDoc::Container
|
8
|
+
attr_reader :file
|
9
|
+
|
10
|
+
# Create CSV document
|
11
|
+
#
|
12
|
+
# @param [String|Symbol] name Name of the document
|
13
|
+
# @param args[0] Type of the document, defaults to KDoc:: FakeOpinion.new.default_csv_type if not set
|
14
|
+
# @param default: Default value (using named params), as above
|
15
|
+
def initialize(key = nil, **opts, &block)
|
16
|
+
super(**{ key: key }.merge(opts))
|
17
|
+
|
18
|
+
initialize_file
|
19
|
+
|
20
|
+
@block = block if block_given?
|
21
|
+
end
|
22
|
+
|
23
|
+
# Load data from file
|
24
|
+
#
|
25
|
+
# @param [Symbol] load_action The load_action to take if data has already been loaded
|
26
|
+
# @param [:once] load_action :once will load the data from content source on first try only
|
27
|
+
# @param [:reload] load_action :reload will reload from content source each time
|
28
|
+
# @param [Symbol] data_action The data_action to take when setting data, defaults to :replace
|
29
|
+
# @param [:replace] data_action :replace will replace the existing data instance with the incoming data value
|
30
|
+
# @param [:append] data_action :append will keep existing data and then new value data over the top
|
31
|
+
def load(load_action: :once, data_action: :replace)
|
32
|
+
return if load_action == :once && loaded?
|
33
|
+
|
34
|
+
rows = []
|
35
|
+
|
36
|
+
CSV.foreach(file, headers: true, header_converters: :symbol) do |row|
|
37
|
+
rows << row.to_h
|
38
|
+
end
|
39
|
+
|
40
|
+
set_data(rows, data_action: data_action)
|
41
|
+
|
42
|
+
@loaded = true
|
43
|
+
end
|
44
|
+
|
45
|
+
def loaded?
|
46
|
+
@loaded
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def initialize_file
|
52
|
+
@file ||= opts.delete(:file) || ''
|
53
|
+
@loaded = false
|
54
|
+
end
|
55
|
+
|
56
|
+
def default_data_type
|
57
|
+
Array
|
58
|
+
end
|
59
|
+
|
60
|
+
def default_container_type
|
61
|
+
:csv
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/lib/k_doc/fake_opinion.rb
CHANGED
@@ -5,8 +5,13 @@ require 'json'
|
|
5
5
|
module KDoc
|
6
6
|
# This is called fake opinion because I have not figured out
|
7
7
|
# how I want to implement this
|
8
|
+
# Need to look at the configuration patterns, this is really a configuration
|
8
9
|
class FakeOpinion
|
10
|
+
attr_accessor :default_action_type
|
9
11
|
attr_accessor :default_model_type
|
12
|
+
attr_accessor :default_csv_type
|
13
|
+
attr_accessor :default_json_type
|
14
|
+
attr_accessor :default_yaml_type
|
10
15
|
attr_accessor :default_settings_key
|
11
16
|
attr_accessor :default_table_key
|
12
17
|
|
@@ -15,7 +20,12 @@ module KDoc
|
|
15
20
|
attr_accessor :table_class
|
16
21
|
|
17
22
|
def initialize
|
18
|
-
@default_model_type = :entity
|
23
|
+
# @default_model_type = :entity
|
24
|
+
@default_action_type = :action
|
25
|
+
@default_model_type = :kdoc
|
26
|
+
@default_csv_type = :csv
|
27
|
+
@default_json_type = :json
|
28
|
+
@default_yaml_type = :yaml
|
19
29
|
@default_settings_key = :settings
|
20
30
|
@default_table_key = :table
|
21
31
|
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KDoc
|
4
|
+
# JsonDoc is a DSL for modeling JSON data objects
|
5
|
+
class JsonDoc < KDoc::Container
|
6
|
+
attr_reader :file
|
7
|
+
|
8
|
+
# Create JSON document
|
9
|
+
#
|
10
|
+
# @param [String|Symbol] name Name of the document
|
11
|
+
# @param args[0] Type of the document, defaults to KDoc:: FakeOpinion.new.default_csv_type if not set
|
12
|
+
# @param default: Default value (using named params), as above
|
13
|
+
# @param [Proc] block The block is stored and accessed different types in the document loading workflow.
|
14
|
+
def initialize(key = nil, **opts, &_block)
|
15
|
+
super(**{ key: key }.merge(opts))
|
16
|
+
|
17
|
+
initialize_file
|
18
|
+
end
|
19
|
+
|
20
|
+
# Load data from file
|
21
|
+
#
|
22
|
+
# @param [Symbol] load_action The load_action to take if data has already been loaded
|
23
|
+
# @param [:once] load_action :once will load the data from content source on first try only
|
24
|
+
# @param [:reload] load_action :reload will reload from content source each time
|
25
|
+
# @param [Symbol] data_action The data_action to take when setting data, defaults to :replace
|
26
|
+
# @param [:replace] data_action :replace will replace the existing data instance with the incoming data value
|
27
|
+
# @param [:append] data_action :append will keep existing data and then new value data over the top
|
28
|
+
def load(load_action: :once, data_action: :replace)
|
29
|
+
return if load_action == :once && loaded?
|
30
|
+
|
31
|
+
content = File.read(file)
|
32
|
+
hash = JSON.parse(content)
|
33
|
+
|
34
|
+
set_data(hash, data_action: data_action)
|
35
|
+
|
36
|
+
@loaded = true
|
37
|
+
end
|
38
|
+
|
39
|
+
def loaded?
|
40
|
+
@loaded
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def initialize_file
|
46
|
+
@file ||= opts.delete(:file) || ''
|
47
|
+
@loaded = false
|
48
|
+
end
|
49
|
+
|
50
|
+
def default_data_type
|
51
|
+
Hash
|
52
|
+
end
|
53
|
+
|
54
|
+
def default_container_type
|
55
|
+
:json
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|