nxt_support 0.1.5 → 0.1.6
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 +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +4 -1
- data/README.md +40 -0
- data/lib/nxt_support.rb +3 -0
- data/lib/nxt_support/models.rb +1 -0
- data/lib/nxt_support/models/preprocess_attributes.rb +35 -0
- data/lib/nxt_support/preprocessor.rb +76 -0
- data/lib/nxt_support/preprocessors/downcase_preprocessor.rb +22 -0
- data/lib/nxt_support/preprocessors/strip_preprocessor.rb +22 -0
- data/lib/nxt_support/version.rb +1 -1
- data/nxt_support.gemspec +1 -0
- metadata +21 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 683364c398008994e7e9efb8007e03bf7056dd816dec3e396a4482ede9e546d9
|
4
|
+
data.tar.gz: 4e8cee47fa7c145b01ae07b5e8c8ded7da56211ba1272794823e2f8b3a8be5ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de2c12071f41de18b97e49d1516faa54d01b343ab68ff0949004713c8e2345b443cdd60f2f051394e41e966480aca79451b9327d189a60915261814b297620a5
|
7
|
+
data.tar.gz: 8b3a6e21cedd45c22ca925099c02a2e62fe1c7f3f25dae05770e09952c754cf433c0737746f699da42fb6caddb966673daafe20795f1618bcdb5411a1db9ac31
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
nxt_support (0.1.
|
4
|
+
nxt_support (0.1.6)
|
5
5
|
nxt_init
|
6
|
+
nxt_registry
|
6
7
|
rails
|
7
8
|
|
8
9
|
GEM
|
@@ -90,6 +91,8 @@ GEM
|
|
90
91
|
mini_portile2 (~> 2.4.0)
|
91
92
|
nxt_init (0.1.5)
|
92
93
|
activesupport
|
94
|
+
nxt_registry (0.1.5)
|
95
|
+
activesupport
|
93
96
|
pry (0.13.1)
|
94
97
|
coderay (~> 1.1)
|
95
98
|
method_source (~> 1.0)
|
data/README.md
CHANGED
@@ -96,6 +96,46 @@ book.genre #=> crime
|
|
96
96
|
```
|
97
97
|
If the default value is not in the list of assignable values, then validation will fail.
|
98
98
|
|
99
|
+
#### NxtSupport::PreprocessAttributes
|
100
|
+
|
101
|
+
This mixin provides the `preprocess_attributes` class method which can preprocess columns before saving by either stripping whitespace, downcasing, or both.
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
class Book < ApplicationRecord
|
105
|
+
include NxtSupport::PreprocessAttributes
|
106
|
+
|
107
|
+
preprocess_attributes :title, :author, preprocessors: %i[strip downcase]
|
108
|
+
end
|
109
|
+
|
110
|
+
book = Book.new(title: ' Moche!')
|
111
|
+
book.save
|
112
|
+
book.title #=> "moche!"
|
113
|
+
```
|
114
|
+
|
115
|
+
Register a custom preprocesser:
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
NxtSupport::Preprocessor.register(:compress, CompressPreprocessor)
|
119
|
+
```
|
120
|
+
|
121
|
+
Also works with non-string columns
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
NxtSupport::Preprocessor.register(:add_one, AddOnePreprocessor, :integer)
|
125
|
+
```
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
class Book < ApplicationRecord
|
129
|
+
include NxtSupport::PreprocessAttributes
|
130
|
+
|
131
|
+
preprocess_attributes :views, preprocessors: %i[add_one], column_type: :integer
|
132
|
+
end
|
133
|
+
|
134
|
+
book = Book.new(views: 1000)
|
135
|
+
book.save
|
136
|
+
book.views #=> "1001"
|
137
|
+
```
|
138
|
+
|
99
139
|
### NxtSupport/Serializers
|
100
140
|
|
101
141
|
Enjoy mixins for your serializers.
|
data/lib/nxt_support.rb
CHANGED
data/lib/nxt_support/models.rb
CHANGED
@@ -0,0 +1,35 @@
|
|
1
|
+
module NxtSupport
|
2
|
+
module PreprocessAttributes
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
class_methods do
|
6
|
+
attr_reader :nxt_preprocessors
|
7
|
+
|
8
|
+
def preprocess_attributes(*attributes, **options)
|
9
|
+
attributes_to_process =
|
10
|
+
if attributes.any?
|
11
|
+
attributes.map(&:to_sym)
|
12
|
+
else
|
13
|
+
column_names.map(&:to_sym)
|
14
|
+
end
|
15
|
+
|
16
|
+
@nxt_preprocessors ||= []
|
17
|
+
@nxt_preprocessors << NxtSupport::Preprocessor.new(attributes_to_process, options)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
included do
|
22
|
+
before_validation do |record|
|
23
|
+
nxt_preprocessors.each do |preprocessor|
|
24
|
+
preprocessor.process(record)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def nxt_preprocessors
|
32
|
+
self.class.nxt_preprocessors
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require "nxt_support/preprocessors/strip_preprocessor"
|
2
|
+
require "nxt_support/preprocessors/downcase_preprocessor"
|
3
|
+
|
4
|
+
module NxtSupport
|
5
|
+
class Preprocessor
|
6
|
+
extend NxtRegistry
|
7
|
+
|
8
|
+
PREPROCESSORS = registry :type, callable: false do
|
9
|
+
nested :preprocessors
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_accessor :attributes, :preprocessors, :options
|
13
|
+
attr_reader :type
|
14
|
+
|
15
|
+
def initialize(attributes, options)
|
16
|
+
@type = options.fetch(:column_type) { :string }
|
17
|
+
@options = options
|
18
|
+
|
19
|
+
attributes_to_preprocess(attributes, options)
|
20
|
+
extract_preprocessors
|
21
|
+
register_default_preprocessors
|
22
|
+
end
|
23
|
+
|
24
|
+
def process(record)
|
25
|
+
attributes.each do |attr|
|
26
|
+
value_to_process = record[attr]
|
27
|
+
processed_value = process_value(value_to_process)
|
28
|
+
record[attr] = processed_value
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def attributes_to_preprocess(attributes, options = {})
|
35
|
+
if options[:only]
|
36
|
+
attributes.select! { |attr| attr.in?(options[:only]) }
|
37
|
+
elsif options[:except]
|
38
|
+
attributes.reject! { |attr| attr.in?(options[:except]) }
|
39
|
+
end
|
40
|
+
|
41
|
+
@attributes = attributes
|
42
|
+
end
|
43
|
+
|
44
|
+
def extract_preprocessors
|
45
|
+
@preprocessors = options[:preprocessors]
|
46
|
+
raise ArgumentError, 'No preprocessors provided' unless preprocessors.present?
|
47
|
+
end
|
48
|
+
|
49
|
+
def process_value(value)
|
50
|
+
preprocessors.each do |key|
|
51
|
+
value = resolve(key, value)
|
52
|
+
end
|
53
|
+
|
54
|
+
value
|
55
|
+
end
|
56
|
+
|
57
|
+
def resolve(key, value)
|
58
|
+
if key.respond_to?(:lambda?)
|
59
|
+
key.call(value)
|
60
|
+
else
|
61
|
+
PREPROCESSORS.resolve(type).resolve(key).new(value).call
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def register_default_preprocessors
|
66
|
+
PREPROCESSORS.type(:string).preprocessors!(:strip, NxtSupport::Preprocessors::StripPreprocessor)
|
67
|
+
PREPROCESSORS.type(:string).preprocessors!(:downcase, NxtSupport::Preprocessors::DowncasePreprocessor)
|
68
|
+
end
|
69
|
+
|
70
|
+
class << self
|
71
|
+
def register(name, preprocessor, type = :string)
|
72
|
+
PREPROCESSORS.type(type).preprocessors!(name, preprocessor)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module NxtSupport
|
2
|
+
module Preprocessors
|
3
|
+
class WrongTypeError < StandardError; end
|
4
|
+
|
5
|
+
class DowncasePreprocessor
|
6
|
+
attr_accessor :value
|
7
|
+
|
8
|
+
def initialize(value)
|
9
|
+
@value = value
|
10
|
+
end
|
11
|
+
|
12
|
+
def call
|
13
|
+
return if value.nil?
|
14
|
+
|
15
|
+
value.downcase!
|
16
|
+
value
|
17
|
+
rescue NoMethodError => e
|
18
|
+
raise WrongTypeError, "Tried to call #{e.name} on #{value}, but #{value} does not respond to #{e.name}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module NxtSupport
|
2
|
+
module Preprocessors
|
3
|
+
class WrongTypeError < StandardError; end
|
4
|
+
|
5
|
+
class StripPreprocessor
|
6
|
+
attr_accessor :value
|
7
|
+
|
8
|
+
def initialize(value)
|
9
|
+
@value = value
|
10
|
+
end
|
11
|
+
|
12
|
+
def call
|
13
|
+
return if value.nil?
|
14
|
+
|
15
|
+
value.strip!
|
16
|
+
value
|
17
|
+
rescue NoMethodError => e
|
18
|
+
raise WrongTypeError, "Tried to call #{e.name} on #{value}, but #{value} does not respond to #{e.name}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/nxt_support/version.rb
CHANGED
data/nxt_support.gemspec
CHANGED
@@ -29,6 +29,7 @@ Gem::Specification.new do |spec|
|
|
29
29
|
|
30
30
|
spec.add_dependency "rails"
|
31
31
|
spec.add_dependency "nxt_init"
|
32
|
+
spec.add_dependency "nxt_registry"
|
32
33
|
spec.add_development_dependency "bundler"
|
33
34
|
spec.add_development_dependency "rake"
|
34
35
|
spec.add_development_dependency "rspec"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nxt_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nils Sommer
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2020-
|
12
|
+
date: 2020-07-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -39,6 +39,20 @@ dependencies:
|
|
39
39
|
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: nxt_registry
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
42
56
|
- !ruby/object:Gem::Dependency
|
43
57
|
name: bundler
|
44
58
|
requirement: !ruby/object:Gem::Requirement
|
@@ -137,7 +151,11 @@ files:
|
|
137
151
|
- lib/nxt_support/models/assignable_values.rb
|
138
152
|
- lib/nxt_support/models/email.rb
|
139
153
|
- lib/nxt_support/models/indifferently_accessible_json_attrs.rb
|
154
|
+
- lib/nxt_support/models/preprocess_attributes.rb
|
140
155
|
- lib/nxt_support/models/safely_find_or_createable.rb
|
156
|
+
- lib/nxt_support/preprocessor.rb
|
157
|
+
- lib/nxt_support/preprocessors/downcase_preprocessor.rb
|
158
|
+
- lib/nxt_support/preprocessors/strip_preprocessor.rb
|
141
159
|
- lib/nxt_support/serializers.rb
|
142
160
|
- lib/nxt_support/serializers/has_time_attributes.rb
|
143
161
|
- lib/nxt_support/util.rb
|
@@ -168,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
168
186
|
- !ruby/object:Gem::Version
|
169
187
|
version: '0'
|
170
188
|
requirements: []
|
171
|
-
rubygems_version: 3.0.
|
189
|
+
rubygems_version: 3.0.8
|
172
190
|
signing_key:
|
173
191
|
specification_version: 4
|
174
192
|
summary: Support through reusable Mixins and Helpers for Ruby on Rails Applications
|