schema-normalize 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +51 -0
- data/.rubocop.yml +11 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +59 -0
- data/README.md +14 -0
- data/gemfiles/ruby_1.9.3.gemfile +1 -0
- data/gemfiles/ruby_2.1.9.gemfile +1 -0
- data/gemfiles/ruby_2.6.3.gemfile +1 -0
- data/gemfiles/travis.gemfile +1 -0
- data/lib/schema-normalize.rb +7 -0
- data/lib/schema/attribute_normalizer.rb +64 -0
- data/lib/schema/model_normalizer.rb +22 -0
- data/lib/schema/normalize.rb +37 -0
- data/schema-normalize.gemspec +15 -0
- data/script/console +7 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 88a1219e8e98d03219ffdf7cc80fca3a04432001d93be6aa40da025d26812fc7
|
4
|
+
data.tar.gz: 65f144251b614287fb12f2bee4f995fef0d8c1e9aaea10cc3f75da8b99e5ba0f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: edd3f094a8e0d8efa4698c3ec4a173256a14b8740aaf0409420554e98799f11942a2432511ca0824b2db09e4ef8ede7ffc68fe794d5a8245319a9a2bacc9b879
|
7
|
+
data.tar.gz: 41994b10302759b7e1d61c315f16a3dfaa33f5c3a812f6fd0bdb5094618a66fea25f838181db47dff17612c8653c8bb54563b217b19e49eb7dafd38cd482a6b5
|
data/.gitignore
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# rcov generated
|
2
|
+
coverage
|
3
|
+
coverage.data
|
4
|
+
|
5
|
+
# rdoc generated
|
6
|
+
rdoc
|
7
|
+
|
8
|
+
# yard generated
|
9
|
+
doc
|
10
|
+
.yardoc
|
11
|
+
|
12
|
+
# bundler
|
13
|
+
.bundle
|
14
|
+
|
15
|
+
# jeweler generated
|
16
|
+
pkg
|
17
|
+
|
18
|
+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
|
19
|
+
#
|
20
|
+
# * Create a file at ~/.gitignore
|
21
|
+
# * Include files you want ignored
|
22
|
+
# * Run: git config --global core.excludesfile ~/.gitignore
|
23
|
+
#
|
24
|
+
# After doing this, these files will be ignored in all your git projects,
|
25
|
+
# saving you from having to 'pollute' every project you touch with them
|
26
|
+
#
|
27
|
+
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
|
28
|
+
#
|
29
|
+
# For MacOS:
|
30
|
+
#
|
31
|
+
.DS_Store
|
32
|
+
|
33
|
+
# For TextMate
|
34
|
+
#*.tmproj
|
35
|
+
#tmtags
|
36
|
+
|
37
|
+
# For emacs:
|
38
|
+
*~
|
39
|
+
\#*
|
40
|
+
.\#*
|
41
|
+
|
42
|
+
# For vim:
|
43
|
+
*.swp
|
44
|
+
|
45
|
+
# For redcar:
|
46
|
+
#.redcar
|
47
|
+
|
48
|
+
# For rubinius:
|
49
|
+
#*.rbc
|
50
|
+
|
51
|
+
*.gem
|
data/.rubocop.yml
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
schema-normalize
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.0.0
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
ast (2.4.2)
|
5
|
+
diff-lcs (1.4.4)
|
6
|
+
docile (1.3.5)
|
7
|
+
inheritance-helper (0.1.5)
|
8
|
+
parallel (1.20.1)
|
9
|
+
parser (3.0.0.0)
|
10
|
+
ast (~> 2.4.1)
|
11
|
+
rainbow (3.0.0)
|
12
|
+
rake (13.0.3)
|
13
|
+
regexp_parser (2.1.1)
|
14
|
+
rexml (3.2.4)
|
15
|
+
rspec (3.10.0)
|
16
|
+
rspec-core (~> 3.10.0)
|
17
|
+
rspec-expectations (~> 3.10.0)
|
18
|
+
rspec-mocks (~> 3.10.0)
|
19
|
+
rspec-core (3.10.1)
|
20
|
+
rspec-support (~> 3.10.0)
|
21
|
+
rspec-expectations (3.10.1)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.10.0)
|
24
|
+
rspec-mocks (3.10.2)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.10.0)
|
27
|
+
rspec-support (3.10.2)
|
28
|
+
rubocop (1.11.0)
|
29
|
+
parallel (~> 1.10)
|
30
|
+
parser (>= 3.0.0.0)
|
31
|
+
rainbow (>= 2.2.2, < 4.0)
|
32
|
+
regexp_parser (>= 1.8, < 3.0)
|
33
|
+
rexml
|
34
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
35
|
+
ruby-progressbar (~> 1.7)
|
36
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
37
|
+
rubocop-ast (1.4.1)
|
38
|
+
parser (>= 2.7.1.5)
|
39
|
+
ruby-progressbar (1.11.0)
|
40
|
+
simplecov (0.21.2)
|
41
|
+
docile (~> 1.1)
|
42
|
+
simplecov-html (~> 0.11)
|
43
|
+
simplecov_json_formatter (~> 0.1)
|
44
|
+
simplecov-html (0.12.3)
|
45
|
+
simplecov_json_formatter (0.1.2)
|
46
|
+
unicode-display_width (2.0.0)
|
47
|
+
|
48
|
+
PLATFORMS
|
49
|
+
x86_64-darwin-20
|
50
|
+
|
51
|
+
DEPENDENCIES
|
52
|
+
inheritance-helper
|
53
|
+
rake
|
54
|
+
rspec
|
55
|
+
rubocop
|
56
|
+
simplecov
|
57
|
+
|
58
|
+
BUNDLED WITH
|
59
|
+
2.2.3
|
data/README.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# schema-normalize
|
2
|
+
|
3
|
+
Basic concept is to apply filtering/normalization methods to attributes. Primary interface is to call normalize on the model. This way you can control when the methods are applied.
|
4
|
+
|
5
|
+
```
|
6
|
+
class Person < ActiveRecord::Base
|
7
|
+
include Schema::Normalize
|
8
|
+
|
9
|
+
normalize :name, :strip, if: :name_changed?
|
10
|
+
normalize :value, :round, args: [2], if: :value_changed?
|
11
|
+
|
12
|
+
before_validation :normalize
|
13
|
+
end
|
14
|
+
```
|
@@ -0,0 +1 @@
|
|
1
|
+
../Gemfile
|
@@ -0,0 +1 @@
|
|
1
|
+
../Gemfile
|
@@ -0,0 +1 @@
|
|
1
|
+
../Gemfile
|
@@ -0,0 +1 @@
|
|
1
|
+
../Gemfile
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Schema
|
4
|
+
class AttributeNormalizer
|
5
|
+
def initialize
|
6
|
+
@normalizations = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def add(method, options = {})
|
10
|
+
@normalizations << options.merge(method: method)
|
11
|
+
end
|
12
|
+
|
13
|
+
def normalize_model_attribute(model, attribute)
|
14
|
+
value = normalize(model, model.public_send(attribute))
|
15
|
+
model.public_send("#{attribute}=", value)
|
16
|
+
end
|
17
|
+
|
18
|
+
def normalize(model, value)
|
19
|
+
return nil if value.nil?
|
20
|
+
|
21
|
+
@normalizations.each do |normalization|
|
22
|
+
value = apply_normalization(model, value, normalization)
|
23
|
+
end
|
24
|
+
value
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def apply_normalization(model, value, normalization)
|
30
|
+
return value if skip_normalization?(model, normalization)
|
31
|
+
|
32
|
+
if normalization[:with]
|
33
|
+
if normalization[:class_name] || normalization[:class]
|
34
|
+
kls = normalization[:class] || Object.const_get(normalization[:class_name])
|
35
|
+
arguments = [value]
|
36
|
+
arguments += normalization[:args] if normalization[:args]
|
37
|
+
call_method(kls, normalization[:with], arguments)
|
38
|
+
else
|
39
|
+
call_method(model, normalization[:with], [value])
|
40
|
+
end
|
41
|
+
else
|
42
|
+
call_method(value, normalization[:method], normalization[:args])
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def skip_normalization?(model, options)
|
47
|
+
return true if options[:if] && !call_method(model, options[:if])
|
48
|
+
return true if options[:unless] && call_method(model, options[:unless])
|
49
|
+
|
50
|
+
false
|
51
|
+
end
|
52
|
+
|
53
|
+
def call_method(object, method, method_arguments = nil)
|
54
|
+
case method
|
55
|
+
when Symbol
|
56
|
+
object.public_send(method, *method_arguments)
|
57
|
+
when Proc
|
58
|
+
object.instance_eval(&method)
|
59
|
+
else
|
60
|
+
raise("wrong type, failed to call method #{method}")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Schema
|
4
|
+
# Normalize all attributes of a model
|
5
|
+
class ModelNormalizer
|
6
|
+
def initialize
|
7
|
+
@attribute_normalizers = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def add(attribute, method, options = {})
|
11
|
+
@attribute_normalizers[attribute] ||= AttributeNormalizer.new
|
12
|
+
@attribute_normalizers[attribute].add(method, options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def normalize(model)
|
16
|
+
@attribute_normalizers.each do |attribute, attribute_normalizer|
|
17
|
+
attribute_normalizer.normalize_model_attribute(model, attribute)
|
18
|
+
end
|
19
|
+
model
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Schema
|
4
|
+
module Normalize
|
5
|
+
def self.included(base)
|
6
|
+
base.extend InheritanceHelper::Methods
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def schema_normalizations
|
12
|
+
[].freeze
|
13
|
+
end
|
14
|
+
|
15
|
+
def create_schema_model_normalizer
|
16
|
+
normalizer = ::Schema::ModelNormalizer.new
|
17
|
+
schema_normalizations.each do |(attribute_name, method_name, options)|
|
18
|
+
normalizer.add(attribute_name, method_name, options)
|
19
|
+
end
|
20
|
+
normalizer
|
21
|
+
end
|
22
|
+
|
23
|
+
def schema_model_normalizer
|
24
|
+
@schema_model_normalizer ||= create_schema_model_normalizer
|
25
|
+
end
|
26
|
+
|
27
|
+
def normalize(attribute_name, method_name, options = {})
|
28
|
+
new_value = schema_normalizations.dup << [attribute_name, method_name, options]
|
29
|
+
redefine_class_method(:schema_normalizations, new_value)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def normalize
|
34
|
+
self.class.schema_model_normalizer.normalize(self)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'schema-normalize'
|
5
|
+
s.version = '0.1.0'
|
6
|
+
s.licenses = ['MIT']
|
7
|
+
s.summary = 'Schema Normalize'
|
8
|
+
s.description = 'Easy way to normalize attributes'
|
9
|
+
s.authors = ['Doug Youch']
|
10
|
+
s.email = 'dougyouch@gmail.com'
|
11
|
+
s.homepage = 'https://github.com/dougyouch/schema-normalize'
|
12
|
+
s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
13
|
+
|
14
|
+
s.add_runtime_dependency 'inheritance-helper'
|
15
|
+
end
|
data/script/console
ADDED
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: schema-normalize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Doug Youch
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-03-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: inheritance-helper
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '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'
|
27
|
+
description: Easy way to normalize attributes
|
28
|
+
email: dougyouch@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- ".gitignore"
|
34
|
+
- ".rubocop.yml"
|
35
|
+
- ".ruby-gemset"
|
36
|
+
- ".ruby-version"
|
37
|
+
- Gemfile
|
38
|
+
- Gemfile.lock
|
39
|
+
- README.md
|
40
|
+
- gemfiles/ruby_1.9.3.gemfile
|
41
|
+
- gemfiles/ruby_2.1.9.gemfile
|
42
|
+
- gemfiles/ruby_2.6.3.gemfile
|
43
|
+
- gemfiles/travis.gemfile
|
44
|
+
- lib/schema-normalize.rb
|
45
|
+
- lib/schema/attribute_normalizer.rb
|
46
|
+
- lib/schema/model_normalizer.rb
|
47
|
+
- lib/schema/normalize.rb
|
48
|
+
- schema-normalize.gemspec
|
49
|
+
- script/console
|
50
|
+
homepage: https://github.com/dougyouch/schema-normalize
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata: {}
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubygems_version: 3.2.3
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: Schema Normalize
|
73
|
+
test_files: []
|