serialize_attributes 0.4.0 → 0.4.1

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: c505eb52395e2c8fe3786c3ef97dd04e5a56f978072d839ba517c0069f9dce93
4
- data.tar.gz: 5ea660579c45ebd3cca56da5c3ea7e07fe9af5fb38a05fd62d3133f118f3d8df
3
+ metadata.gz: 809e8a18bc0152b6f9961215ee10b10769b90c01e7973e094ea238e09e4bacc5
4
+ data.tar.gz: fa5e51462f9e9f7a57048dec29c6d09ec041dd1a748242674f6560956c4c91a7
5
5
  SHA512:
6
- metadata.gz: 311846e88d0c92cf41bf5adbd32c1515fbd1d131df0d733cfd444963bedc8594620d05cb12cfe701b40fba851195931f6f53673dcbe25593bf257e2b28c183dc
7
- data.tar.gz: c5e2e60ba000e0afe423cee6c37e20fae5b590a39bcadab76d65712db712359e1d4ae3c0532e44565d673cb1c8f55cd4d535b9fdc057f50d11d3e3744f0803e9
6
+ metadata.gz: 5ab61e0943e013c453cec02210764f1dab818e640185c416564b19d4f3e62c85993068e7cb68b1cb268205f42086f8fc65de318aa07c3bdf620dfdf4ea4e6c56
7
+ data.tar.gz: 60ac9d6688461a364c81bef28a79b4e03707c00ae20e05b3d5dbbc40a63d93a64e646f73c44a8315ff3aa87b151c710754ed7fede08162b58438e2981257027c
@@ -47,7 +47,10 @@ module SerializeAttributes
47
47
  # Model.serialized_attributes_store(:settings).cast(:user_name, 42)
48
48
  # => "42"
49
49
  def cast(name, value)
50
- @attributes[name.to_sym].cast(value)
50
+ # @attributes.fetch(name.to_sym) returns the Type as defined in ActiveModel::Type or
51
+ # raise an error if the type is unknown.
52
+ # Type::Integer.new.cast("42") => 42
53
+ @attributes.fetch(name.to_sym).cast(value)
51
54
  end
52
55
 
53
56
  # Deserialize a stored attribute using the value from the database (or elsewhere)
@@ -55,7 +58,12 @@ module SerializeAttributes
55
58
  # Model.serialized_attributes_store(:settings).deserialize(:subscribed, "0")
56
59
  # => false
57
60
  def deserialize(name, value)
58
- @attributes[name.to_sym].deserialize(value)
61
+ attribute = @attributes[name.to_sym]
62
+ if attribute.nil?
63
+ raise "The attribute #{name} is not define in serialize_attribute method in the #{@model_class} class."
64
+ end
65
+
66
+ attribute.deserialize(value)
59
67
  end
60
68
 
61
69
  # Retrieve the default value for a given block. If the default is a Proc, it can be
@@ -96,7 +104,16 @@ module SerializeAttributes
96
104
 
97
105
  @model_class.module_eval <<~RUBY, __FILE__, __LINE__ + 1
98
106
  def #{name} # def user_name
99
- store = public_send(:#{@column_name}) # store = public_send(:settings)
107
+ if @_bad_typcasting # if @_bad_typcasting
108
+ store = # store =
109
+ read_attribute_before_type_cast( # read_attribute_before_type_cast(
110
+ :#{@column_name} # :settings
111
+ ) # )
112
+ @_bad_typcasting = false # @_bad_typcasting = false
113
+ else # else
114
+ store = public_send(:#{@column_name}) # store = public_send(:settings)
115
+ end # end
116
+ #
100
117
  if store.key?("#{name}") # if store.key?("user_name")
101
118
  store["#{name}"] # store["user_name"]
102
119
  else # else
@@ -105,20 +122,25 @@ module SerializeAttributes
105
122
  .default(:#{name}, self) # .default(:user_name, self)
106
123
  end # end
107
124
  end # end
108
-
125
+ #
109
126
  def #{name}=(value) # def user_name=(value)
110
127
  cast_value = self.class # cast_value = self.class
111
128
  .serialized_attributes_store(:#{@column_name}) # .serialized_attributes_store(:settings)
112
129
  .cast(:#{name}, value) # .cast(:user_name, value)
113
130
  store = public_send(:#{@column_name}) # store = public_send(:settings)
114
131
  #
115
- if #{arr} && cast_value == ArrayWrapper::EMPTY # if false && cast_value == ArrayWrapper::EMPTY
132
+ if #{arr} && cast_value == ArrayWrapper::EMPTY # if arr && cast_value == ArrayWrapper::EMPTY
116
133
  store.delete("#{name}") # store.delete("user_name")
117
134
  else # else
118
135
  store.merge!("#{name}" => cast_value) # store.merge!("user_name" => cast_value)
119
136
  end # end
137
+ public_send(:#{@column_name}=, store) # public_send(:settings=, store)
120
138
  #
121
- self.public_send(:#{@column_name}=, store) # self.public_send(:settings=, store)
139
+ values_before_typecast = store.values # values_before_typecast = store.values
140
+ values_after_typecast = # values_after_typecast =
141
+ public_send(:#{@column_name}).values # public_send(:settings).values
142
+ @_bad_typcasting = # @_bad_typcasting =
143
+ values_before_typecast != values_after_typecast # values_before_typecast != values_after_typecast
122
144
  end # end
123
145
  RUBY
124
146
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SerializeAttributes
4
- VERSION = "0.4.0"
4
+ VERSION = "0.4.1"
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.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zaikio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-07 00:00:00.000000000 Z
11
+ date: 2022-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel