qoobaa-to_hash 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.rdoc +55 -4
  2. data/test/to_hash_test.rb +31 -1
  3. metadata +1 -1
data/README.rdoc CHANGED
@@ -1,12 +1,63 @@
1
1
  = to_hash
2
2
 
3
- Easy and powerful object to hash serialization tool. It may be used together with to_json/to_xml/to_yaml standard methods.
3
+ Easy and powerful object to hash serialization tool. It may be used together with to_json/to_xml/to_yaml methods.
4
4
 
5
5
  == Usage
6
6
 
7
- hash = employee.to_hash(:firstName => :first_name, :lastName => :last_name, :projects => [:projects, :title])
8
- # => { :firstName => "John", :lastName => "Doe", :projects => [{ :title => "Project 1" }, { :title => "Project 2" }] }
9
- { :employee => hash }.to_json
7
+ Take a look at the example below:
8
+
9
+ class Category < ActiveRecord::Base
10
+ def name
11
+ #...
12
+ end
13
+ end
14
+
15
+ class Comment < ActiveRecord::Base
16
+ def author_name
17
+ #...
18
+ end
19
+ end
20
+
21
+ class Post < ActiveRecord::Base
22
+ has_one :category
23
+ has_many :comments
24
+
25
+ include ToHash
26
+
27
+ def title
28
+ #...
29
+ end
30
+
31
+ def body
32
+ #...
33
+ end
34
+ end
35
+
36
+ # simple serialization
37
+ Post.first.to_hash(:title, :body)
38
+ #=> { :title => "Post Title", :body => "Post body" }
39
+
40
+ # serialization with key changing
41
+ Post.first.to_hash(:title => :title, :content => :body)
42
+ #=> { :title => "Post Title", :content => "Post body" }
43
+
44
+ # simple serialization of nested objects
45
+ Post.first.to_hash(:title, [:category, :name])
46
+ #=> { :title => "Post Title", :category => { :name => "Category Name" } }
47
+
48
+ # serialization of nested objects with key changing
49
+ Post.first.to_hash(:title, [:category, { :categoryName => :name }])
50
+ #=> { :title => "Post Title", :category => { :categoryName => "Category Name" } }
51
+
52
+ # serialization of nested arrays
53
+ Post.first.to_hash(:title, [:comments, { :author => :author_name }])
54
+ #=> { :title => "Post Title", :comments => [{ :author => "John Doe" }, { :author => "Jim Smith" }] }
55
+
56
+ # creating JSON
57
+ Post.first.to_hash(:t => :title, :b => :body).to_json
58
+ #=> "{\"t\": \"Post Title\", \"b\": \"Post body\"}"
59
+
60
+ * ToHash module doesn't require the class to inherit from ActiveRecord::Base - it can be used with any other classes as well.
10
61
 
11
62
  = License
12
63
 
data/test/to_hash_test.rb CHANGED
@@ -45,7 +45,37 @@ class ToHashTest < Test::Unit::TestCase
45
45
  assert_equal hash, { :myTitle => "Project 1", :myDesc => "Project 1 description" }
46
46
  end
47
47
 
48
- # TODO: more test (no time right now)
48
+ should "allow to include other objects in hash" do
49
+ hash = @project1.to_hash([:authors, :first_name])
50
+ assert_equal hash, { :authors => [{ :first_name => "John" }, { :first_name => "Michael" }, { :first_name => "Jimmy" }] }
51
+ end
52
+
53
+ should "allow to include other objects with multiple attributes in hash" do
54
+ hash = @project1.to_hash([:authors, :first_name, :last_name])
55
+ assert_equal hash, { :authors => [{ :first_name => "John", :last_name => "Doe" },
56
+ { :first_name => "Michael", :last_name => "Smith" },
57
+ { :first_name => "Jimmy", :last_name => "Parker" }] }
58
+ end
59
+
60
+ should "allow to include other objects with changed key" do
61
+ hash = @project1.to_hash(:people => [:authors, :first_name])
62
+ assert_equal hash, { :people => [{ :first_name => "John" }, { :first_name => "Michael" }, { :first_name => "Jimmy" }] }
63
+ end
64
+
65
+ should "allow to include other objects with inner keys changed" do
66
+ hash = @project1.to_hash(:people => [:authors, { :firstName => :first_name }])
67
+ assert_equal hash, { :people => [{ :firstName => "John" }, { :firstName => "Michael" }, { :firstName => "Jimmy" }] }
68
+ end
69
+
70
+ should "allow using different serialization methods described above" do
71
+ hash = @project1.to_hash(:title, [:authors, { :firstName => :first_name }])
72
+ assert_equal hash, { :title => "Project 1", :authors => [{ :firstName => "John" }, { :firstName => "Michael" }, { :firstName => "Jimmy" }]}
73
+ end
74
+
75
+ should "serialize singular nested object" do
76
+ hash = @project1.to_hash(:title, [:company, :name])
77
+ assert_equal hash, { :title => "Project 1", :company => { :name => "HAL" } }
78
+ end
49
79
  end
50
80
  end
51
81
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qoobaa-to_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jakub Ku\xC5\xBAma"