serialize_attributes 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 866a54a31babd7bd446a0348107c7262b0ea49b71769ca573a75d37107429b85
4
- data.tar.gz: 484d600fbc6fb88e9796ee067957e9b337a20f8c3180fad81f2f0b0792620514
3
+ metadata.gz: 336ab0663fe21a1f3750663a41aec451f209bf1141f233f3043c914aefd0ebf9
4
+ data.tar.gz: c80f05ff696c4292571c5ae613343687377bb10e1235790ea332dc80e6e15230
5
5
  SHA512:
6
- metadata.gz: b90b797aeb295de0516ab9b991c49fefb430b0943fee75e0e44710fa5ba85688d4c7220d64dd93d3e75566bca4d5b4dc8dd2b2c62ca2130c8fe4cd7c741be5d8
7
- data.tar.gz: c0fcee019031311f0a597505669b5a42e50d2b935997ef1ba901e9f474ccde426b28f970e35d3b4ab053fd563b3944536c455c23a97bb606c200ad2f281254c2
6
+ metadata.gz: c3f9f7d020b7002f2b83e816452f4166a26cb4a1f2fdfaf3271977bfb8c80d8f075456ebeeeec042a1c544786f5fa2a5f80a16f7353596eb4817cdc76e768b95
7
+ data.tar.gz: 6c0133edf56b9a396cfe011f4abda01d3ac61b1c95dca5329dcaa1d8a29b2bd366ac28f691b8e1d6aa4c565398781131921a6645f8b69a8051565bfabc9b1676
data/README.md CHANGED
@@ -117,6 +117,26 @@ class MyModel
117
117
  end
118
118
  ```
119
119
 
120
+ ### Array types
121
+
122
+ By default, `ActiveModel::Attribute` does not support Array types, however this library
123
+ does. The syntax is the same as `ActiveRecord::Attribute` when using the Postgres adapter:
124
+
125
+ ```ruby
126
+ class MyModel
127
+ serialize_attributes :settings do
128
+ attribute :emails, :string, array: true
129
+ end
130
+ end
131
+ ```
132
+
133
+ Please note that the default value for an array attribute is always `[]`, unless you
134
+ specify a `default` attribute yourself explicitly:
135
+
136
+ ```ruby
137
+ attribute :emails, :string, array: true, default: ["unknown@example.com"]
138
+ ```
139
+
120
140
  ### Usage with ActiveModel alone
121
141
 
122
142
  It's also possible to use this library without `ActiveRecord`:
@@ -50,10 +50,17 @@ module SerializeAttributes
50
50
 
51
51
  def attribute(name, type, **options)
52
52
  name = name.to_sym
53
+ arr = options.delete(:array) { false }
53
54
  type = ActiveModel::Type.lookup(type, **options.except(:default)) if type.is_a?(Symbol)
55
+ type = ArrayWrapper.new(type) if arr
54
56
 
55
57
  @attributes[name] = type
56
- @defaults[name] = options[:default] if options.key?(:default)
58
+
59
+ if options.key?(:default)
60
+ @defaults[name] = options[:default]
61
+ elsif arr
62
+ @defaults[name] = []
63
+ end
57
64
 
58
65
  @model_class.module_eval <<~RUBY, __FILE__, __LINE__ + 1
59
66
  def #{name} # def user_name
@@ -72,14 +79,35 @@ module SerializeAttributes
72
79
  .serialized_attributes_store(:#{@column_name}) # .serialized_attributes_store(:settings)
73
80
  .cast(:#{name}, value) # .cast(:user_name, value)
74
81
  store = public_send(:#{@column_name}) # store = public_send(:settings)
75
- self.public_send( # self.public_send(
76
- :#{@column_name}=, # :settings=,
77
- store.merge("#{name}" => cast_value) # store.merge("user_name" => cast_value)
78
- ) # )
82
+ #
83
+ if #{arr} && cast_value == ArrayWrapper::EMPTY # if false && cast_value == ArrayWrapper::EMPTY
84
+ store.delete("#{name}") # store.delete("user_name")
85
+ else # else
86
+ store.merge!("#{name}" => cast_value) # store.merge!("user_name" => cast_value)
87
+ end # end
88
+ #
89
+ self.public_send(:#{@column_name}=, store) # self.public_send(:settings=, store)
79
90
  end # end
80
91
  RUBY
81
92
  end
82
93
 
94
+ class ArrayWrapper < SimpleDelegator # :nodoc:
95
+ EMPTY = Object.new
96
+
97
+ def cast(value)
98
+ # We don't want to store the null value, because array types _always_ have a default
99
+ # configured. So we return this special object here, and check it again before
100
+ # updating the underlying store.
101
+ return EMPTY unless value
102
+
103
+ Array(value)
104
+ end
105
+
106
+ def deserialize(value)
107
+ value.map { __getobj__.deserialize(_1) }
108
+ end
109
+ end
110
+
83
111
  class StoreColumnWrapper < SimpleDelegator # :nodoc:
84
112
  def initialize(original, store) # rubocop:disable Lint/MissingSuper
85
113
  __setobj__(original)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SerializeAttributes
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serialize_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zaikio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-16 00:00:00.000000000 Z
11
+ date: 2022-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel