k_doc 0.0.16 → 0.0.22

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64615008b76feb26a84e9dd808654495cf3f26bf42f6941b00eaca9013081a76
4
- data.tar.gz: e69ccfb764c6dd9df4ee1fe78df56f717726f733184839020d3df968c34fde63
3
+ metadata.gz: 2dcd9625fafb133dca240894911cad20bf220718f4d12b9d73240d2079f4294f
4
+ data.tar.gz: 592c2661f4b9a8cd905325b60d6488110029245e89f3dfdd7c1f0eb518205afd
5
5
  SHA512:
6
- metadata.gz: 94d4cd31dddad2f142fa418c24025c866124722e8f93ee3133d1d7c68893640954c7203e5bd5a502eb75f9650c444d7103945af6fa901ece63450a0537b130d3
7
- data.tar.gz: f1a6e045702b3e9c94146ae413f573fc281472eaf5927e9b27a819445cff112b2a688863c7f0bf2d2d4e996e703dd3f4f7721c7766ecd621feb4971b24cfddba
6
+ metadata.gz: 303a6524573902efd1e7488d080ed1affe6aa23abd4eac80371d7aaceac7289c06fc4ab8dea4b9c817e3a896fce6538ded912684929586487fa043ca9f14cae2
7
+ data.tar.gz: 996f72379212f095f6786897287282a6add950ca83c5c554da61434556b919b258e1f5777d88116f57bbe756c247c9c09f19ca9b9536edb77c678786a5d16f50
@@ -0,0 +1,10 @@
1
+ require_relative './build_actions'
2
+ require_relative './director'
3
+ # require_relative './gem_director'
4
+ require_relative './klue_director'
5
+ require_relative './design_pattern_director'
6
+
7
+ def klue
8
+ @klue ||= KlueDirector.new
9
+ end
10
+
@@ -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,5 @@
1
+ class KlueDirector < Director
2
+ def design_patterns
3
+ director(:design_patterns)
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require 'k_log'
2
+ require 'k_util'
3
+
4
+ include KLog::Logging
5
+
6
+ require_relative './configure_builder'
7
+ require_relative './handlebars_helpers'
@@ -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
@@ -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/(.+)\.rb$}) { |m| "spec/unit/#{m[1]}_spec.rb" }
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
@@ -44,4 +44,7 @@ Gem::Specification.new do |spec|
44
44
  spec.add_dependency 'k_type' , '~> 0.0.0'
45
45
  spec.add_dependency 'k_util' , '~> 0.0.0'
46
46
  # spec.add_dependency 'tty-box', '~> 0.5.0'
47
+ spec.metadata = {
48
+ 'rubygems_mfa_required' => 'true'
49
+ }
47
50
  end
@@ -1,63 +1,49 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KDoc
4
- # A data acts a base data object for any data requires tagging such as
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
- # include KLog::Logging
8
-
9
- # Seems like there is way too much data here
10
- # Firstly it should probably be in an interface
11
- # Secondly some of this (namespace, project_key, error) belongs in k_manager
12
- # So container would be better off just being key, type, data
13
- attr_reader :key
14
-
15
- # NOTE: This should not be using an opinion in this project
16
- attr_reader :type
17
-
18
- # Move this up to k_manager
19
- # attr_reader :namespace
20
- # attr_reader :project_key
21
- attr_reader :error
22
-
23
- # Create container for storing data/documents.
24
- #
25
- # Any container can be uniquely identified via it's
26
- # key, type, namespace and project_key attributes
27
- #
28
- # @param [Hash] **opts The options
29
- # @option opts [String|Symbol] name Name of the container
30
- # @option opts [String|Symbol] type Type of the container, defaults to KDoc:: FakeOpinion.new.default_model_type if not set
31
- # @option opts [String|Symbol] namespace Namespace that the container belongs to
32
- # @option opts [String|Symbol] project_key Project that the container belongs to
33
- def initialize(**opts)
34
- @key = opts[:key] || SecureRandom.alphanumeric(4)
35
- @type = opts[:type] || '' # KDoc.opinion.default_model_type
36
- # @namespace = opts[:namespace] || ''
37
- # @project_key = opts[:project_key] || ''
38
-
39
- # Old name is default_data, wonder if I still need that idea?
40
- # Most documents live within a hash, some tabular documents such as CSV will use an []
41
- # @data = slice_option(:default_data) || {}
42
- @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
43
20
  end
44
21
 
45
- # def unique_key
46
- # @unique_key ||= KDoc.util.build_unique_key(key, type, namespace, project_key)
47
- # end
22
+ def initialize(**opts, &block)
23
+ @opts = opts
24
+
25
+ initialize_tag(opts)
26
+ initialize_data(opts)
27
+ initialize_block(opts, &block)
28
+ end
29
+
30
+ def default_container_type
31
+ :container
32
+ end
33
+
34
+ def default_data_type
35
+ Hash
36
+ end
48
37
 
49
- def debug_header
50
- log.kv 'key', key
51
- log.kv 'type', type
52
- # log.kv 'namespace', namespace
53
- # log.kv 'project_key', namespace
54
- # log.kv 'error', error
38
+ def debug
39
+ debug_container
40
+ debug_errors
55
41
  end
56
42
 
57
- attr_writer :data
43
+ private
58
44
 
59
- def data
60
- @data.clone
45
+ def debug_errors
46
+ log.block(errors, title: 'errors') unless valid?
61
47
  end
62
48
  end
63
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
@@ -5,8 +5,12 @@ 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
9
10
  attr_accessor :default_model_type
11
+ attr_accessor :default_csv_type
12
+ attr_accessor :default_json_type
13
+ attr_accessor :default_yaml_type
10
14
  attr_accessor :default_settings_key
11
15
  attr_accessor :default_table_key
12
16
 
@@ -16,6 +20,9 @@ module KDoc
16
20
 
17
21
  def initialize
18
22
  @default_model_type = :entity
23
+ @default_csv_type = :csv
24
+ @default_json_type = :json
25
+ @default_yaml_type = :yaml
19
26
  @default_settings_key = :settings
20
27
  @default_table_key = :table
21
28
 
@@ -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