k_decor 0.0.1 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/CODE_OF_CONDUCT.md +6 -6
- data/Guardfile +31 -0
- data/k_decor.gemspec +3 -1
- data/lib/k_decor.rb +32 -1
- data/lib/k_decor/base_decorator.rb +44 -0
- data/lib/k_decor/configuration.rb +20 -0
- data/lib/k_decor/decorator_hash.rb +54 -0
- data/lib/k_decor/helper.rb +54 -0
- data/lib/k_decor/resolve_instance.rb +21 -0
- data/lib/k_decor/version.rb +1 -1
- metadata +37 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4895a5e41e46f0010305f1a7702b5b733595eec102fabd4655458687f918473e
|
4
|
+
data.tar.gz: 3a2af2a33aca215f12237a712452bb11679bb298ee2093d5c4e8bb4bfd2f630f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc546d4fb93297ccc148d243dc9f8b6e7bdb3e2015bb39bce0a9920ec7499a36bfdc6dd23b4dfabebeac0758cd6b13eceaa4fee595fe581418d06078a51bbae2
|
7
|
+
data.tar.gz: 92929c85592e1581dc8563c4a547c29fcf43c70d793ecb514546f321d5bac8c220c2a2366bee86a7b18d9273bcc5b2c64605b447a569c5ac65e9001a5d3620d3
|
data/.rubocop.yml
CHANGED
data/CODE_OF_CONDUCT.md
CHANGED
@@ -11,7 +11,7 @@ orientation.
|
|
11
11
|
|
12
12
|
## Our Standards
|
13
13
|
|
14
|
-
Examples of
|
14
|
+
Examples of behaviour that contributes to creating a positive environment
|
15
15
|
include:
|
16
16
|
|
17
17
|
- Using welcoming and inclusive language
|
@@ -20,7 +20,7 @@ include:
|
|
20
20
|
- Focusing on what is best for the community
|
21
21
|
- Showing empathy towards other community members
|
22
22
|
|
23
|
-
Examples of unacceptable
|
23
|
+
Examples of unacceptable behaviour by participants include:
|
24
24
|
|
25
25
|
- The use of sexualized language or imagery and unwelcome sexual attention or
|
26
26
|
advances
|
@@ -34,13 +34,13 @@ Examples of unacceptable behavior by participants include:
|
|
34
34
|
## Our Responsibilities
|
35
35
|
|
36
36
|
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
-
|
38
|
-
response to any instances of unacceptable
|
37
|
+
behaviour and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behaviour.
|
39
39
|
|
40
40
|
Project maintainers have the right and responsibility to remove, edit, or
|
41
41
|
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
42
|
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
-
permanently any contributor for other
|
43
|
+
permanently any contributor for other behaviours that they deem inappropriate,
|
44
44
|
threatening, offensive, or harmful.
|
45
45
|
|
46
46
|
## Scope
|
@@ -54,7 +54,7 @@ further defined and clarified by project maintainers.
|
|
54
54
|
|
55
55
|
## Enforcement
|
56
56
|
|
57
|
-
Instances of abusive, harassing, or otherwise unacceptable
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behaviour may be
|
58
58
|
reported by contacting the project team at david.cruwys@bugcrowd.com. All
|
59
59
|
complaints will be reviewed and investigated and will result in a response that
|
60
60
|
is deemed necessary and appropriate to the circumstances. The project team is
|
data/Guardfile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
guard :bundler, cmd: 'bundle install' do
|
4
|
+
watch('Gemfile')
|
5
|
+
watch('k_decor.gemspec')
|
6
|
+
end
|
7
|
+
|
8
|
+
group :green_pass_then_cop, halt_on_fail: true do
|
9
|
+
guard :rspec, cmd: 'bundle exec rspec -f doc' do
|
10
|
+
require 'guard/rspec/dsl'
|
11
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
12
|
+
|
13
|
+
# RSpec files
|
14
|
+
rspec = dsl.rspec
|
15
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
16
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
17
|
+
watch(rspec.spec_files)
|
18
|
+
|
19
|
+
# Ruby files
|
20
|
+
ruby = dsl.ruby
|
21
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
22
|
+
watch(%r{^lib/k_decor/(.+)\.rb$}) { |m| "spec/unit/#{m[1]}_spec.rb" }
|
23
|
+
watch(%r{^lib/k_decor/commands/(.+)\.rb$}) { |m| "spec/unit/commands/#{m[1]}_spec.rb" }
|
24
|
+
watch(%r{^spec/mocks/(.+)\.rb$}) { |_m| 'spec/k_decor/base_decorator_spec.rb' }
|
25
|
+
end
|
26
|
+
|
27
|
+
guard :rubocop, all_on_start: false, cli: ['--format', 'clang'] do
|
28
|
+
watch(/{.+\.rb$/)
|
29
|
+
watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
|
30
|
+
end
|
31
|
+
end
|
data/k_decor.gemspec
CHANGED
@@ -38,5 +38,7 @@ Gem::Specification.new do |spec|
|
|
38
38
|
spec.require_paths = ['lib']
|
39
39
|
# spec.extensions = ['ext/k_decor/extconf.rb']
|
40
40
|
|
41
|
-
|
41
|
+
spec.add_dependency 'k_log' , '~> 0.0.0'
|
42
|
+
spec.add_dependency 'k_type' , '~> 0.0.0'
|
43
|
+
# spec.add_dependency 'k_util' , '~> 0.0.0'
|
42
44
|
end
|
data/lib/k_decor.rb
CHANGED
@@ -1,10 +1,41 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'k_log'
|
4
|
+
require 'k_type'
|
5
|
+
|
3
6
|
require 'k_decor/version'
|
7
|
+
require 'k_decor/base_decorator'
|
8
|
+
require 'k_decor/resolve_instance'
|
9
|
+
require 'k_decor/configuration'
|
10
|
+
require 'k_decor/decorator_hash'
|
11
|
+
require 'k_decor/helper'
|
4
12
|
|
5
13
|
module KDecor
|
6
14
|
# raise KDecor::Error, 'Sample message'
|
7
15
|
class Error < StandardError; end
|
8
16
|
|
9
|
-
|
17
|
+
class << self
|
18
|
+
attr_accessor :decorate
|
19
|
+
|
20
|
+
def configuration
|
21
|
+
@configuration ||= KDecor::Configuration.new
|
22
|
+
end
|
23
|
+
|
24
|
+
def reset
|
25
|
+
@configuration = KDecor::Configuration.new
|
26
|
+
end
|
27
|
+
|
28
|
+
def configure
|
29
|
+
yield(configuration)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
KDecor.decorate = KDecor::Helper.new
|
34
|
+
end
|
35
|
+
|
36
|
+
if ENV['KLUE_DEBUG']&.to_s&.downcase == 'true'
|
37
|
+
namespace = 'KDecor::Version'
|
38
|
+
file_path = $LOADED_FEATURES.find { |f| f.include?('k_decor/version') }
|
39
|
+
version = KDecor::VERSION.ljust(9)
|
40
|
+
puts "#{namespace.ljust(35)} : #{version.ljust(9)} : #{file_path}"
|
10
41
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KDecor
|
4
|
+
# Base decorator
|
5
|
+
class BaseDecorator
|
6
|
+
include KLog::Logging
|
7
|
+
|
8
|
+
# Compatible type will store the Object Type that this decorator
|
9
|
+
# is suitable for processing
|
10
|
+
attr_accessor :compatible_type
|
11
|
+
|
12
|
+
# What are the specific behaviours available on this decorator
|
13
|
+
#
|
14
|
+
# If you wish to use a decorator to run against a compatible data
|
15
|
+
# type you do not need individual behaviours then set
|
16
|
+
# implemented_behaviours to [:default]
|
17
|
+
#
|
18
|
+
# But if this decorator type only targets certain behaviours then give it a
|
19
|
+
# list of specific :behaviour to perform. e.g. [:update_fields, :update_rows]
|
20
|
+
attr_accessor :implemented_behaviours
|
21
|
+
|
22
|
+
def initialize(compatible_type)
|
23
|
+
@compatible_type = compatible_type
|
24
|
+
@implemented_behaviours = []
|
25
|
+
end
|
26
|
+
|
27
|
+
# Does the decorator implement the behaviour
|
28
|
+
# , any = false)
|
29
|
+
def behaviour?(behaviour)
|
30
|
+
behaviour == :all || implemented_behaviours.include?(behaviour)
|
31
|
+
end
|
32
|
+
|
33
|
+
def compatible?(target)
|
34
|
+
target.is_a?(compatible_type)
|
35
|
+
end
|
36
|
+
|
37
|
+
def decorate(target, **opts)
|
38
|
+
raise KType::Error, "#{self.class} is incompatible with data object" unless compatible?(target)
|
39
|
+
raise KType::Error, "#{self.class} has not implemented an update method" unless respond_to?(:update)
|
40
|
+
|
41
|
+
update(target, **opts)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KDecor
|
4
|
+
# Configuration class for KDecor
|
5
|
+
class Configuration
|
6
|
+
attr_accessor :decorators
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@decorators = KDecor::DecoratorHash.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def register_decorator(key, decorator)
|
13
|
+
decorators.add(key, decorator)
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_decorator(key)
|
17
|
+
decorators[key]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KDecor
|
4
|
+
# Decorator set will hold a set of instantiated decorators with lookup keys
|
5
|
+
class DecoratorHash < Hash
|
6
|
+
include KDecor::ResolveInstance
|
7
|
+
include KLog::Logging
|
8
|
+
|
9
|
+
# Add a keyed decorator to the store.
|
10
|
+
#
|
11
|
+
# @param [Symbol] key lookup key that can be used when retrieving the decorator
|
12
|
+
def add(key, decorator)
|
13
|
+
if key?(key)
|
14
|
+
log.warn("Decorator with this key '#{key}' already in the stored")
|
15
|
+
return
|
16
|
+
end
|
17
|
+
|
18
|
+
self[key] = resolve_decorator_instance(decorator)
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
|
22
|
+
# def decorators(decorators)
|
23
|
+
# return [] if decorators.nil?
|
24
|
+
|
25
|
+
# decorators.map do |decorator|
|
26
|
+
# if decorator.is_a?(Class)
|
27
|
+
# handle_class(decorator)
|
28
|
+
# elsif decorator.is_a?(Symbol)
|
29
|
+
# handle_symbol(decorator)
|
30
|
+
# else
|
31
|
+
# decorator
|
32
|
+
# end
|
33
|
+
# end.compact
|
34
|
+
# end
|
35
|
+
|
36
|
+
# def decorator_instance(decorator)
|
37
|
+
# if decorator.is_a?(Class)
|
38
|
+
# return decorator.new if decorator.ancestors.include?(KDecor::BaseDecorator)
|
39
|
+
|
40
|
+
# raise KType::Error, 'Class type is not a KDecor::BaseDecorator'
|
41
|
+
# end
|
42
|
+
|
43
|
+
# raise KType::Error, 'Class instance is not a KDecor::BaseDecorator' unless decorator.is_a?(KDecor::BaseDecorator)
|
44
|
+
|
45
|
+
# decorator
|
46
|
+
# end
|
47
|
+
|
48
|
+
# def handle_symbol(decorator)
|
49
|
+
# result = KDsl.config.get_decorator(decorator)&.new
|
50
|
+
# result = nil unless result.respond_to?(:update)
|
51
|
+
# result
|
52
|
+
# end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KDecor
|
4
|
+
# KDoc.settings :x, decorators: [Class, Class.instance, :class] do
|
5
|
+
# #
|
6
|
+
# end
|
7
|
+
|
8
|
+
class Helper
|
9
|
+
include KDecor::ResolveInstance
|
10
|
+
|
11
|
+
# Resolve decorator instances by evaluating a list of decorators in the format
|
12
|
+
# :symbol, class or :class_instance
|
13
|
+
#
|
14
|
+
# :symbol will be looked up from pre-configured decorators
|
15
|
+
# :class will be instantiated
|
16
|
+
# :class_instance will be returned as is
|
17
|
+
#
|
18
|
+
# @example Sample of how decorators can be passed
|
19
|
+
#
|
20
|
+
# decorators(:uppercase, PeopleModelDecorator, PluralizeModelDecorator.new('Person' => 'People') })
|
21
|
+
#
|
22
|
+
# @param [Array<Symbol,Class<BaseDecorator>|Instance<BaseDecorator>>] decorators provide a list of
|
23
|
+
# decorators as either Class or Instances or pre-configured symbols
|
24
|
+
def resolve_decorators(decorators)
|
25
|
+
decorators.map do |decorator|
|
26
|
+
if decorator.is_a?(Symbol)
|
27
|
+
KDecor.configuration.get_decorator(decorator)
|
28
|
+
else
|
29
|
+
resolve_decorator_instance(decorator)
|
30
|
+
end
|
31
|
+
end.compact
|
32
|
+
end
|
33
|
+
|
34
|
+
# Run a list of decorators against the data
|
35
|
+
def decorate(data, *decorators, behaviour: :default, behaviours: [])
|
36
|
+
if behaviours.length.zero?
|
37
|
+
decorators.map do |decorator|
|
38
|
+
decorator.decorate(data, behaviour: behaviour)
|
39
|
+
end
|
40
|
+
else
|
41
|
+
behaviours.each do |behave|
|
42
|
+
decorators.map do |decorator|
|
43
|
+
decorator.decorate(data, behaviour: behave)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
data
|
48
|
+
end
|
49
|
+
|
50
|
+
# decorate(data, [](:plural, PluralizeModelDecorator)
|
51
|
+
# config.register_decorator(:plural_configured, PluralizeModelDecorator.new('Person' => 'People'))
|
52
|
+
# config.register_decorator(:people, PeopleModelDecorator)
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KDecor
|
4
|
+
# Decorator set will hold a set of instantiated decorators with lookup keys
|
5
|
+
module ResolveInstance
|
6
|
+
# Decorator instance will accept a decorator in the form of Class or Instance
|
7
|
+
#
|
8
|
+
# @param [Class<BaseDecorator>|Instance<BaseDecorator>] decorator as either a class type or an instance that extends from BaseDecorator
|
9
|
+
def resolve_decorator_instance(decorator)
|
10
|
+
if decorator.is_a?(Class)
|
11
|
+
return decorator.new if decorator.ancestors.include?(KDecor::BaseDecorator)
|
12
|
+
|
13
|
+
raise KType::Error, 'Class type is not a KDecor::BaseDecorator'
|
14
|
+
end
|
15
|
+
|
16
|
+
raise KType::Error, 'Class instance is not a KDecor::BaseDecorator' unless decorator.is_a?(KDecor::BaseDecorator)
|
17
|
+
|
18
|
+
decorator
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/k_decor/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: k_decor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Cruwys
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-04-
|
12
|
-
dependencies:
|
11
|
+
date: 2021-04-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: k_log
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: k_type
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.0.0
|
13
41
|
description: " KDecor provides decorators and management for applying data decoration
|
14
42
|
to data objects\n"
|
15
43
|
email:
|
@@ -24,6 +52,7 @@ files:
|
|
24
52
|
- ".rubocop.yml"
|
25
53
|
- CODE_OF_CONDUCT.md
|
26
54
|
- Gemfile
|
55
|
+
- Guardfile
|
27
56
|
- LICENSE.txt
|
28
57
|
- README.md
|
29
58
|
- Rakefile
|
@@ -36,6 +65,11 @@ files:
|
|
36
65
|
- hooks/update-version
|
37
66
|
- k_decor.gemspec
|
38
67
|
- lib/k_decor.rb
|
68
|
+
- lib/k_decor/base_decorator.rb
|
69
|
+
- lib/k_decor/configuration.rb
|
70
|
+
- lib/k_decor/decorator_hash.rb
|
71
|
+
- lib/k_decor/helper.rb
|
72
|
+
- lib/k_decor/resolve_instance.rb
|
39
73
|
- lib/k_decor/version.rb
|
40
74
|
homepage: http://appydave.com/gems/k-decor
|
41
75
|
licenses:
|