active_nomad 0.1.0 → 0.2.0
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.
- data/CHANGELOG +4 -0
- data/README.markdown +6 -6
- data/lib/active_nomad/version.rb +1 -1
- data/lib/active_nomad.rb +9 -4
- data/spec/unit/active_nomad_spec.rb +8 -8
- metadata +3 -3
data/CHANGELOG
CHANGED
data/README.markdown
CHANGED
@@ -26,7 +26,7 @@ persist to a cookie:
|
|
26
26
|
|
27
27
|
thing = Thing.from_json(cookies[:thing])
|
28
28
|
thing.to_save do |thing|
|
29
|
-
cookies[:thing] = thing.
|
29
|
+
cookies[:thing] = thing.to_ordered_json
|
30
30
|
true
|
31
31
|
end
|
32
32
|
|
@@ -34,9 +34,9 @@ Things to note:
|
|
34
34
|
|
35
35
|
* The proc should return true if persistence was successful, false
|
36
36
|
otherwise. This will be the return value of `save`, etc.
|
37
|
-
* Active Nomad defines `
|
38
|
-
from a JSON string with predictable attribute order (i.e., appropriate
|
39
|
-
cookie).
|
37
|
+
* Active Nomad defines `to_ordered_json` and `from_json` which will serialize
|
38
|
+
to and from a JSON string with predictable attribute order (i.e., appropriate
|
39
|
+
for a cookie).
|
40
40
|
* You may alternatively override `persist` in a subclass if you don't want to
|
41
41
|
register a proc for every instance.
|
42
42
|
|
@@ -44,8 +44,8 @@ Things to note:
|
|
44
44
|
|
45
45
|
Active Nomad provides serialization to and from:
|
46
46
|
|
47
|
-
* JSON (`
|
48
|
-
* Query string (`
|
47
|
+
* JSON (`to_ordered_json` and `from_json`)
|
48
|
+
* Query string (`to_ordered_query_string` and `from_query_string`)
|
49
49
|
|
50
50
|
You can define your own formats easily using `to_serialized_attributes` and
|
51
51
|
`from_serialized_attributes`. The former returns an `ActiveSupport::OrderedHash`
|
data/lib/active_nomad/version.rb
CHANGED
data/lib/active_nomad.rb
CHANGED
@@ -45,7 +45,9 @@ module ActiveNomad
|
|
45
45
|
#
|
46
46
|
# Serialize this record as a query string.
|
47
47
|
#
|
48
|
-
|
48
|
+
# Attributes are sorted by name.
|
49
|
+
#
|
50
|
+
def to_ordered_query_string
|
49
51
|
to_serialized_attributes.map do |name, value|
|
50
52
|
next nil if value.nil?
|
51
53
|
"#{CGI.escape(name)}=#{CGI.escape(value)}"
|
@@ -54,7 +56,7 @@ module ActiveNomad
|
|
54
56
|
|
55
57
|
#
|
56
58
|
# Deserialize this record from a query string returned by
|
57
|
-
# #
|
59
|
+
# #to_ordered_query_string.
|
58
60
|
#
|
59
61
|
def self.from_query_string(string)
|
60
62
|
return new if string.blank?
|
@@ -68,12 +70,15 @@ module ActiveNomad
|
|
68
70
|
#
|
69
71
|
# Serialize this record as a JSON string.
|
70
72
|
#
|
71
|
-
|
73
|
+
# Attributes are sorted by name.
|
74
|
+
#
|
75
|
+
def to_ordered_json
|
72
76
|
to_serialized_attributes.to_json
|
73
77
|
end
|
74
78
|
|
75
79
|
#
|
76
|
-
# Deserialize this record from a JSON string returned by
|
80
|
+
# Deserialize this record from a JSON string returned by
|
81
|
+
# #to_ordered_json.
|
77
82
|
#
|
78
83
|
def self.from_json(string)
|
79
84
|
return new if string.blank?
|
@@ -238,14 +238,14 @@ describe ActiveNomad::Base do
|
|
238
238
|
end
|
239
239
|
end
|
240
240
|
|
241
|
-
describe "#
|
241
|
+
describe "#to_ordered_query_string" do
|
242
242
|
it "should serialize the attributes as a query string" do
|
243
243
|
klass = Class.new(ActiveNomad::Base) do
|
244
244
|
attribute :first_name, :string
|
245
245
|
attribute :last_name, :string
|
246
246
|
end
|
247
247
|
instance = klass.new(:first_name => 'Joe', :last_name => 'Blow')
|
248
|
-
instance.
|
248
|
+
instance.to_ordered_query_string.should == 'first_name=Joe&last_name=Blow'
|
249
249
|
end
|
250
250
|
end
|
251
251
|
|
@@ -348,14 +348,14 @@ describe ActiveNomad::Base do
|
|
348
348
|
|
349
349
|
it_should_roundtrip_through(:to_serialized_attributes, :from_serialized_attributes)
|
350
350
|
|
351
|
-
it_should_roundtrip_through(:
|
351
|
+
it_should_roundtrip_through(:to_ordered_query_string, :from_query_string) do
|
352
352
|
it "should not be tripped up by delimiters in the keys" do
|
353
353
|
klass = Class.new(ActiveNomad::Base) do
|
354
354
|
attribute :'a=x', :string
|
355
355
|
attribute :'b&x', :string
|
356
356
|
end
|
357
357
|
original = klass.new("a=x" => "1", "b&x" => "2")
|
358
|
-
roundtripped = klass.from_query_string(original.
|
358
|
+
roundtripped = klass.from_query_string(original.to_ordered_query_string)
|
359
359
|
roundtripped.send("a=x").should == "1"
|
360
360
|
roundtripped.send("b&x").should == "2"
|
361
361
|
end
|
@@ -366,19 +366,19 @@ describe ActiveNomad::Base do
|
|
366
366
|
attribute :b, :string
|
367
367
|
end
|
368
368
|
original = klass.new(:a => "1=2", :b => "3&4")
|
369
|
-
roundtripped = klass.from_query_string(original.
|
369
|
+
roundtripped = klass.from_query_string(original.to_ordered_query_string)
|
370
370
|
roundtripped.a.should == "1=2"
|
371
371
|
roundtripped.b.should == "3&4"
|
372
372
|
end
|
373
373
|
end
|
374
374
|
|
375
|
-
it_should_roundtrip_through(:
|
375
|
+
it_should_roundtrip_through(:to_ordered_json, :from_json) do
|
376
376
|
it "should not be tripped up by delimiters in the keys" do
|
377
377
|
klass = Class.new(ActiveNomad::Base) do
|
378
378
|
attribute :"'a':b,c", :string
|
379
379
|
end
|
380
380
|
original = klass.new("'a':b,c" => "1")
|
381
|
-
roundtripped = klass.from_json(original.
|
381
|
+
roundtripped = klass.from_json(original.to_ordered_json)
|
382
382
|
roundtripped.send("'a':b,c").should == "1"
|
383
383
|
end
|
384
384
|
|
@@ -387,7 +387,7 @@ describe ActiveNomad::Base do
|
|
387
387
|
attribute :a, :string
|
388
388
|
end
|
389
389
|
original = klass.new(:a => "'a':b,c")
|
390
|
-
roundtripped = klass.from_json(original.
|
390
|
+
roundtripped = klass.from_json(original.to_ordered_json)
|
391
391
|
roundtripped.a.should == "'a':b,c"
|
392
392
|
end
|
393
393
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_nomad
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- George Ogata
|