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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 469fc9a13f4a9363e12d67a2640d9b28bdde711acf66fecbb2b42da9ab2a3824
4
- data.tar.gz: 46a8fbd8d133be5e79e6d5652dc4ecfcc23d7550f47a06396149216accf85848
3
+ metadata.gz: 3f951700c6299147ea96c2065fc17079862d4cbf1c42a18f7b7cd93a4b0b14fb
4
+ data.tar.gz: 19ccfbd579f45fdd335a3ebe5033fe5c697172df65a758ce825d88f97ea18e27
5
5
  SHA512:
6
- metadata.gz: 1c34b8ea6c57ddfac5861d6c5aac8baf42c5d51f93441ae1605384f2ab4ecfbe0dcebc3e749e4db396e8b529047962d0bf2b49d346e5e54515824db90a158ca8
7
- data.tar.gz: 7a24934f8045dcbc45dfa6ee9bc48ebc023502ee66208e55bb4d0ebbc273d31271857b04e1d699567549e08c82bd49c41e417c72a353c3451e931da48312d09e
6
+ metadata.gz: '084dbc6eec41a7b755a5b762494f063e0f02676adc34740554e30e3f32b73999521e76fdacd64e6a90ab61f81028bf9c123c29932c898414c8df549ef7e23cb3'
7
+ data.tar.gz: fd42adb4583b76b9bfa08302b0ebed82489e31a7e17fd2eee4f98bab6fa0f8becc85cd585ac1e38b1fadf4fdf41c8572d4307c666b5c72170f8292e111afdbf6
@@ -1,3 +1,8 @@
1
+ # v0.1.3 2020-02-19
2
+
3
+ - Added `NxtSupport::IndifferentlyAccessibleJsonAttrs`
4
+ - Added `NxtSupport::HasTimeAttributes`.
5
+
1
6
  # v0.1.2 2020-02-12
2
7
 
3
8
  - Added `NxtSupport::Email`.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nxt_support (0.1.2)
4
+ nxt_support (0.1.3)
5
5
  rails
6
6
 
7
7
  GEM
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
@@ -1,6 +1,7 @@
1
1
  require 'active_support/all'
2
2
  require "nxt_support/version"
3
3
  require "nxt_support/models"
4
+ require "nxt_support/serializers"
4
5
  require "nxt_support/util"
5
6
 
6
7
  module NxtSupport
@@ -1,2 +1,3 @@
1
1
  require "nxt_support/models/email"
2
2
  require "nxt_support/models/safely_find_or_createable"
3
+ require "nxt_support/models/indifferently_accessible_json_attrs"
@@ -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
@@ -1,3 +1,3 @@
1
1
  module NxtSupport
2
- VERSION = "0.1.2".freeze
2
+ VERSION = "0.1.3".freeze
3
3
  end
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.2
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 00:00:00.000000000 Z
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