hash-serializer 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/.rvmrc ADDED
@@ -0,0 +1,2 @@
1
+ rvm_gemset_create_on_use_flag=1
2
+ rvm gemset use hash-serializer
data/README.md CHANGED
@@ -1,16 +1,43 @@
1
1
 
2
2
  # INTRODUCTION
3
3
 
4
- The aim of this gem is to enable an easy way to serialize an object in a Hash object or a JSON data structure using
5
- the attributes name as keys.
4
+ The aim of this gem is to enable an easy way to serialize an object in a Hash object or a JSON data structure using attributes name as keys.
6
5
 
7
- This can be useful if you need to work with XSD schemas while using code generators that convert an schema in
8
- ruby objects but still you haven't a way to convert to JSON or any other format.
6
+ This can be useful if you need to work with XSD schemas while using code generators that convert an schema in ruby objects but still you haven't a way to convert to JSON or any other format.
9
7
 
10
8
  # INSTALLATION
11
9
  gem install hash-serializer
12
10
 
13
- # HOW TO USE
11
+ # HOT TO USE v0.2.0
12
+ require 'hash-serializer'
13
+
14
+ class User
15
+ attr_accessor :nickname
16
+ attr_accessor :email
17
+ attr_accessor :password
18
+
19
+ def initialize(nickname = nil, email = nil, password = nil)
20
+ @nickname = nickname
21
+ @email = email
22
+ @password = password
23
+ end
24
+ end
25
+
26
+ u = User.new("juandebravo", "juan at pollinimini dot net", "*******")
27
+
28
+ HashSerializer.to_hash(u)
29
+ => {"nickname"=>"juandebravo", "email"=>"juan at pollinimini dot net", "password"=>"*******"}
30
+
31
+ HashSerializer.to_hash(u, u.nickname)
32
+ => {"juandebravo"=>{"nickname"=>"juandebravo", "email"=>"juan at pollinimini dot net", "password"=>"*******"}}
33
+
34
+ puts HashSerializer.to_json(u)
35
+ => {"nickname":"juandebravo","email":"juan at pollinimini dot net","password":"*******"}
36
+
37
+ puts HashSerializer.to_json(u, "user")
38
+ => {"user":{"nickname":"juandebravo","email":"juan at pollinimini dot net","password":"*******"}}
39
+
40
+ # HOW TO USE v0.1.0
14
41
 
15
42
  require 'hash-serializer'
16
43
 
@@ -40,3 +67,7 @@ ruby objects but still you haven't a way to convert to JSON or any other format.
40
67
  puts u.serialize_to_json("user")
41
68
  => {"user":{"nickname":"juandebravo","email":"juan at pollinimini dot net","password":"*******"}}
42
69
 
70
+ # CHANGES
71
+
72
+ * v0.2.0 does not monkey patch Object class anymore. Always seems to be a bad idea this, so v0.2.0
73
+ is incompatible backwards.
@@ -1,4 +1,62 @@
1
- require 'hash-serializer/extensions/object'
1
+ # Include json only if it's not loaded before
2
+ Object.respond_to? :to_json or require 'json'
3
+
2
4
  module HashSerializer
3
- # Your code goes here...
5
+
6
+ class << self
7
+ #
8
+ # Instance method to parse an object to an equivalent Hash format
9
+ # Used to serialize body data
10
+ #
11
+ def to_hash(obj, root_name = nil)
12
+ result = Hash.new
13
+ obj.instance_variables.each do |column|
14
+ aux = obj.instance_variable_get(column)
15
+ unless aux.nil?
16
+ if aux.instance_of?(String) or aux.kind_of?(String)
17
+ # It's required to erase any '_' character because
18
+ # soap4r has included it (not idea about the reason)
19
+ result[get_column_value(column)] = aux.to_s
20
+ else
21
+ if aux.is_a?(Array)
22
+ result_aux = Array.new
23
+ aux.each do |elem|
24
+ result_aux << HashSerializer.to_hash(elem)
25
+ end
26
+ else
27
+ result_aux = HashSerializer.to_hash(aux)
28
+ end
29
+ result[get_column_value(column)] = result_aux
30
+ end
31
+ end
32
+ end
33
+ unless root_name.nil?
34
+ temp = result
35
+ result = {}
36
+ result[root_name] = temp
37
+ end
38
+ result
39
+ end
40
+
41
+ #
42
+ # Convert the Hashed object to a String containing the object JSON format
43
+ #
44
+ def to_json(obj, root_name = nil)
45
+ to_hash(obj, root_name).to_json
46
+ end
47
+
48
+ private
49
+
50
+ #
51
+ # Retrieves the attribute name
52
+ #
53
+ def get_column_value(column)
54
+ column = column.to_s.sub(/@/, '')
55
+ unless column.index('_').nil?
56
+ column = column.split('_')[1]
57
+ end
58
+ column
59
+ end
60
+
61
+ end
4
62
  end
@@ -1,4 +1,4 @@
1
1
  module HashSerializer
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
4
4
 
@@ -24,7 +24,7 @@ describe "hash-serializer" do
24
24
 
25
25
  describe "object to json extension" do
26
26
  it "converts properly a basic object with root name" do
27
- json_obj = obj.serialize_to_json("object")
27
+ json_obj = HashSerializer.to_json(obj, "object")
28
28
  json_obj.should be_a_kind_of(String)
29
29
  json_obj = JSON.parse(json_obj)
30
30
  json_obj.should be_a_kind_of(Hash)
@@ -37,7 +37,7 @@ describe "hash-serializer" do
37
37
 
38
38
 
39
39
  it "converts properly a basic object without root name" do
40
- json_obj = obj.serialize_to_json
40
+ json_obj = HashSerializer.to_json(obj)
41
41
  json_obj.should be_a_kind_of(String)
42
42
  json_obj = JSON.parse(json_obj)
43
43
  json_obj.should be_a_kind_of(Hash)
@@ -50,7 +50,7 @@ describe "hash-serializer" do
50
50
 
51
51
  describe "object to hash extension" do
52
52
  it "converts properly a basic object without root name" do
53
- json_obj = obj.serialize_to_hash
53
+ json_obj = HashSerializer.to_hash(obj)
54
54
 
55
55
  json_obj.should be_a_kind_of(Hash)
56
56
  json_obj.should have_key("foo")
@@ -58,7 +58,7 @@ describe "hash-serializer" do
58
58
  end
59
59
 
60
60
  it "converts properly a basic object with root name" do
61
- json_obj = obj.serialize_to_hash("foo")
61
+ json_obj = HashSerializer.to_hash(obj, "foo")
62
62
  json_obj.should be_a_kind_of(Hash)
63
63
  json_obj.should have_key("foo")
64
64
 
@@ -68,7 +68,7 @@ describe "hash-serializer" do
68
68
  end
69
69
 
70
70
  it "converts properly a basic object with a symbol as root name" do
71
- json_obj = obj.serialize_to_hash(:foo)
71
+ json_obj = HashSerializer.to_hash(obj, :foo)
72
72
  json_obj.should be_a_kind_of(Hash)
73
73
  json_obj.should have_key(:foo)
74
74
 
@@ -84,7 +84,7 @@ describe "hash-serializer" do
84
84
  describe "object to hash extension" do
85
85
  it "converts properly a complex object without root name" do
86
86
 
87
- json_obj = complex_obj.serialize_to_hash
87
+ json_obj = HashSerializer.to_hash(complex_obj)
88
88
 
89
89
  ["foo", "bar", "bazz"].each { |key|
90
90
  json_obj.should have_key(key)
@@ -98,7 +98,7 @@ describe "hash-serializer" do
98
98
 
99
99
  it "converts properly a complex object wit root name" do
100
100
 
101
- json_obj = complex_obj.serialize_to_hash(:object)
101
+ json_obj = HashSerializer.to_hash(complex_obj, :object)
102
102
 
103
103
  json_obj.should be_a_kind_of(Hash)
104
104
  json_obj.should have_key(:object)
@@ -122,7 +122,7 @@ describe "hash-serializer" do
122
122
  describe "object to hash extension" do
123
123
  it "converts properly a complex object without root name" do
124
124
 
125
- json_obj = complex_obj.serialize_to_hash
125
+ json_obj = HashSerializer.to_hash(complex_obj)
126
126
 
127
127
  ["foo", "bar", "bazz", "array"].each { |key|
128
128
  json_obj.should have_key(key)
@@ -140,7 +140,7 @@ describe "hash-serializer" do
140
140
  describe "object to json extension" do
141
141
 
142
142
  it "converts properly a basic object with root name" do
143
- json_obj = complex_obj.serialize_to_json
143
+ json_obj = HashSerializer.to_json(complex_obj)
144
144
  json_obj.should be_a_kind_of(String)
145
145
  json_obj = JSON.parse(json_obj)
146
146
  json_obj.should be_a_kind_of(Hash)
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash-serializer
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 0
9
- version: 0.1.0
4
+ prerelease:
5
+ version: 0.2.0
10
6
  platform: ruby
11
7
  authors:
12
8
  - Juan de Bravo
@@ -14,7 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2011-04-05 00:00:00 +02:00
13
+ date: 2011-06-22 00:00:00 +03:00
18
14
  default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
@@ -25,10 +21,6 @@ dependencies:
25
21
  requirements:
26
22
  - - ">="
27
23
  - !ruby/object:Gem::Version
28
- segments:
29
- - 1
30
- - 4
31
- - 3
32
24
  version: 1.4.3
33
25
  type: :runtime
34
26
  version_requirements: *id001
@@ -43,6 +35,7 @@ extra_rdoc_files: []
43
35
 
44
36
  files:
45
37
  - .gitignore
38
+ - .rvmrc
46
39
  - Gemfile
47
40
  - README.md
48
41
  - Rakefile
@@ -65,21 +58,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
65
58
  requirements:
66
59
  - - ">="
67
60
  - !ruby/object:Gem::Version
68
- segments:
69
- - 0
70
61
  version: "0"
71
62
  required_rubygems_version: !ruby/object:Gem::Requirement
72
63
  none: false
73
64
  requirements:
74
65
  - - ">="
75
66
  - !ruby/object:Gem::Version
76
- segments:
77
- - 0
78
67
  version: "0"
79
68
  requirements: []
80
69
 
81
70
  rubyforge_project: hash-serializer
82
- rubygems_version: 1.3.7
71
+ rubygems_version: 1.6.2
83
72
  signing_key:
84
73
  specification_version: 3
85
74
  summary: This gem provides an easy way to wrap a class attributes in a key-value representation (Hash object in ruby)