super_serialize 0.0.2 → 0.0.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/lib/super_serialize/version.rb +1 -1
- data/lib/super_serialize.rb +52 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9582fd7206a15c2a01a88c444079c4141f7b8f44
|
4
|
+
data.tar.gz: e67b84f14b3dc54068e6c1ed9fa9ad07090ca65b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5bc58f79564ab70d5a6c97833a26067ebf3f4bf292b175ba1fe9d5cd50eefd9ae525ce5095b538eadda53421e0fb89e4cc2ca035940b062fc22852ff657abbef
|
7
|
+
data.tar.gz: e945146f60d42d1a3d00f87a260194a284dd772bc1cdd2a60e77cafdc1b0bf18d6edd1ea5f21aa810a177f1414cdd64d1a55bf66271cde29f6237e508c57fc17
|
data/lib/super_serialize.rb
CHANGED
@@ -83,6 +83,16 @@ module SuperSerialize
|
|
83
83
|
|
84
84
|
def #{attr_name}=(value)
|
85
85
|
current_changed_attributes = changed_attributes.dup
|
86
|
+
|
87
|
+
# Sanitizing the input
|
88
|
+
if value.is_a?(String)
|
89
|
+
value.strip!
|
90
|
+
|
91
|
+
if trying_to_serialize_a_hash?(value)
|
92
|
+
value = attempt_to_sanitize_hash_syntax(value)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
86
96
|
return_value = super(value)
|
87
97
|
|
88
98
|
if #{attr_name} == #{attr_name}_was
|
@@ -115,6 +125,31 @@ module SuperSerialize
|
|
115
125
|
const_set('SUPER_SERIALIZED_ATTRIBUTES', attr_names)
|
116
126
|
end
|
117
127
|
|
128
|
+
unless const_defined?('HASH_ROCKET_REGEX_MATCH')
|
129
|
+
const_set('HASH_ROCKET_REGEX_MATCH',
|
130
|
+
%r{
|
131
|
+
# Do not match two colons next to each other.
|
132
|
+
# This is the 1st group match
|
133
|
+
([^:])
|
134
|
+
# This is technically the 2nd group match
|
135
|
+
(
|
136
|
+
# Match the old syntax symbol hash rocket key
|
137
|
+
# 3rd group match
|
138
|
+
:([a-zA-Z_][a-zA-Z_0-9]*)
|
139
|
+
|
|
140
|
+
# Match the single quote wrapped hash rocket key
|
141
|
+
# 4th group match
|
142
|
+
'([a-zA-Z_][a-zA-Z_0-9]*)'
|
143
|
+
|
|
144
|
+
# Match the double quote wrapped hash rocket key
|
145
|
+
# 5th group match
|
146
|
+
"([a-zA-Z_][a-zA-Z_0-9]*)"
|
147
|
+
)
|
148
|
+
\s*\=>\s*
|
149
|
+
}x
|
150
|
+
)
|
151
|
+
end
|
152
|
+
|
118
153
|
private
|
119
154
|
|
120
155
|
def is_valid_yaml?(value)
|
@@ -125,6 +160,17 @@ module SuperSerialize
|
|
125
160
|
end
|
126
161
|
end
|
127
162
|
|
163
|
+
def trying_to_serialize_a_hash?(value)
|
164
|
+
return false unless value.is_a?(String)
|
165
|
+
!!(value =~ /^{.+}$/)
|
166
|
+
end
|
167
|
+
|
168
|
+
def attempt_to_sanitize_hash_syntax(value)
|
169
|
+
value.gsub(self.class::HASH_ROCKET_REGEX_MATCH) do |string|
|
170
|
+
"#{$1}#{($3 || $4 || $5)}: "
|
171
|
+
end.gsub(/(:)([^\s])/, '\1 \2')
|
172
|
+
end
|
173
|
+
|
128
174
|
def yamlize_super_serialized_attributes
|
129
175
|
self.class.const_get('SUPER_SERIALIZED_ATTRIBUTES').each do |attr_name|
|
130
176
|
next unless send(attr_name)
|
@@ -143,8 +189,13 @@ module SuperSerialize
|
|
143
189
|
def check_if_super_serialized_attributes_are_valid_yaml
|
144
190
|
self.class.const_get('SUPER_SERIALIZED_ATTRIBUTES').each do |attr_name|
|
145
191
|
next unless send(attr_name)
|
192
|
+
|
146
193
|
unless is_valid_yaml?(super_serialized_attr_as_string_or_yaml(attr_name))
|
147
|
-
|
194
|
+
if trying_to_serialize_a_hash?(read_attribute(attr_name))
|
195
|
+
errors.add(attr_name, 'syntax is incorrect if you are trying to save a hash. Example of a valid hash would be {some_key: "some value"}.')
|
196
|
+
else
|
197
|
+
errors.add(attr_name, 'could not be properly serialized. Typical serializations are numbers, strings, arrays, or hashes.')
|
198
|
+
end
|
148
199
|
end
|
149
200
|
end
|
150
201
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: super_serialize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ricardo Quiñones
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|