nxt_support 0.1.2 → 0.1.3
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 +5 -0
- data/Gemfile.lock +1 -1
- data/README.md +28 -0
- data/lib/nxt_support.rb +1 -0
- data/lib/nxt_support/models.rb +1 -0
- data/lib/nxt_support/models/indifferently_accessible_json_attrs.rb +41 -0
- data/lib/nxt_support/serializers.rb +1 -0
- data/lib/nxt_support/serializers/has_time_attributes.rb +21 -0
- data/lib/nxt_support/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f951700c6299147ea96c2065fc17079862d4cbf1c42a18f7b7cd93a4b0b14fb
|
4
|
+
data.tar.gz: 19ccfbd579f45fdd335a3ebe5033fe5c697172df65a758ce825d88f97ea18e27
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '084dbc6eec41a7b755a5b762494f063e0f02676adc34740554e30e3f32b73999521e76fdacd64e6a90ab61f81028bf9c123c29932c898414c8df549ef7e23cb3'
|
7
|
+
data.tar.gz: fd42adb4583b76b9bfa08302b0ebed82489e31a7e17fd2eee4f98bab6fa0f8becc85cd585ac1e38b1fadf4fdf41c8572d4307c666b5c72170f8292e111afdbf6
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -32,6 +32,18 @@ Enjoy support for your models.
|
|
32
32
|
|
33
33
|
This class collects useful tools around working with email addresses. Use `NxtSupport::Email::REGEXP` to match email address strings. See the sources for a list of criteria it validates.
|
34
34
|
|
35
|
+
#### NxtSupport::IndifferentlyAccessibleJsonAttrs
|
36
|
+
|
37
|
+
This mixin provides the `indifferently_accessible_json_attrs` class method which serializes and deserializes JSON database columns with `ActiveSupport::HashWithIndifferentAccess` instead of `Hash`.
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
class MyModel < ApplicationRecord
|
41
|
+
include IndifferentlyAccessibleJsonAttrs
|
42
|
+
|
43
|
+
indifferently_accessible_json_attrs :data
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
35
47
|
#### NxtSupport::SafelyFindOrCreateable
|
36
48
|
|
37
49
|
The `NxtSupport::Models::SafelyFindOrCreateable` concern is aimed at ActiveRecord models with a uniqueness database constraint. If you use `find_or_create_by` from ActiveRecord, it can happen that the `find_by` call returns `nil` (because no record for the given conditions exists), but in the small timeframe between the `find_by` and the `create` call, another thread inserts a record, so that the `create` call raises an error.
|
@@ -46,6 +58,22 @@ end
|
|
46
58
|
Book.safely_find_or_create_by!(market: 'de', title: 'Moche!')
|
47
59
|
```
|
48
60
|
|
61
|
+
### NxtSupport/Serializers
|
62
|
+
|
63
|
+
Enjoy mixins for your serializers.
|
64
|
+
|
65
|
+
#### NxtSupport::HasTimeAttributes
|
66
|
+
|
67
|
+
This mixin provides your serializer classes with a `attribute_as_iso8601` and a `attributes_as_iso8601` method. They behave almost the same as the `attribute` method of [active_model_serializers](https://github.com/rails-api/active_model_serializers) (in fact they call it behind the scenes), but they convert the values of the given attributes to an ISO8601 string. This is useful for `Date`, `Time` or `ActiveSupport::Duration` values.
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
class MySerializer < ActiveModel::Serializer
|
71
|
+
include NxtSupport::HasTimeAttributes
|
72
|
+
|
73
|
+
attributes_as_iso8601 :created_at, :updated_at
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
49
77
|
### NxtSupport/Util
|
50
78
|
|
51
79
|
Enjoy some useful utilities
|
data/lib/nxt_support.rb
CHANGED
data/lib/nxt_support/models.rb
CHANGED
@@ -0,0 +1,41 @@
|
|
1
|
+
module NxtSupport
|
2
|
+
module IndifferentlyAccessibleJsonAttrs
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
class JsonSerializer
|
6
|
+
class << self
|
7
|
+
def load(str)
|
8
|
+
indifferent_accessable(JSON.load(str))
|
9
|
+
end
|
10
|
+
|
11
|
+
def dump(obj)
|
12
|
+
JSON.dump(obj)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def indifferent_accessable(obj)
|
18
|
+
if obj.is_a?(Array)
|
19
|
+
obj.map! { |o| indifferent_accessable(o) }
|
20
|
+
elsif obj.is_a?(Hash)
|
21
|
+
obj.with_indifferent_access
|
22
|
+
elsif obj.is_a?(NilClass)
|
23
|
+
obj
|
24
|
+
else
|
25
|
+
raise ArgumentError, "Cant deserialize '#{obj}'"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class_methods do
|
32
|
+
def indifferently_accessible_json_attrs(*attrs)
|
33
|
+
attrs.each do |attr|
|
34
|
+
serialize attr, JsonSerializer
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
alias_method :indifferently_accessible_json_attr, :indifferently_accessible_json_attrs
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "nxt_support/serializers/has_time_attributes"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module NxtSupport
|
2
|
+
module HasTimeAttributes
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def attributes_as_iso8601(*attr_names)
|
7
|
+
attr_names.each do |attr_name|
|
8
|
+
attribute_as_iso8601(attr_name)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def attribute_as_iso8601(attr_name)
|
13
|
+
define_method(attr_name) do
|
14
|
+
object.send(attr_name)&.iso8601
|
15
|
+
end
|
16
|
+
|
17
|
+
attribute attr_name
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/nxt_support/version.rb
CHANGED
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.3
|
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-02-
|
12
|
+
date: 2020-02-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -121,7 +121,10 @@ files:
|
|
121
121
|
- lib/nxt_support.rb
|
122
122
|
- lib/nxt_support/models.rb
|
123
123
|
- lib/nxt_support/models/email.rb
|
124
|
+
- lib/nxt_support/models/indifferently_accessible_json_attrs.rb
|
124
125
|
- lib/nxt_support/models/safely_find_or_createable.rb
|
126
|
+
- lib/nxt_support/serializers.rb
|
127
|
+
- lib/nxt_support/serializers/has_time_attributes.rb
|
125
128
|
- lib/nxt_support/util.rb
|
126
129
|
- lib/nxt_support/util/enum_hash.rb
|
127
130
|
- lib/nxt_support/version.rb
|