hash-serializer 0.1.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/.gitignore +6 -0
- data/Gemfile +11 -0
- data/README.md +42 -0
- data/Rakefile +2 -0
- data/hash-serializer.gemspec +28 -0
- data/lib/hash-serializer.rb +4 -0
- data/lib/hash-serializer/extensions/object.rb +63 -0
- data/lib/hash-serializer/version.rb +4 -0
- data/spec/hash-serializer-spec.rb +161 -0
- metadata +87 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
|
|
2
|
+
# INTRODUCTION
|
|
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.
|
|
6
|
+
|
|
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.
|
|
9
|
+
|
|
10
|
+
# INSTALLATION
|
|
11
|
+
gem install hash-serializer
|
|
12
|
+
|
|
13
|
+
# HOW TO USE
|
|
14
|
+
|
|
15
|
+
require 'hash-serializer'
|
|
16
|
+
|
|
17
|
+
class User
|
|
18
|
+
attr_accessor :nickname
|
|
19
|
+
attr_accessor :email
|
|
20
|
+
attr_accessor :password
|
|
21
|
+
|
|
22
|
+
def initialize(nickname = nil, email = nil, password = nil)
|
|
23
|
+
@nickname = nickname
|
|
24
|
+
@email = email
|
|
25
|
+
@password = password
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
u = User.new("juandebravo", "juan at pollinimini dot net", "*******")
|
|
30
|
+
|
|
31
|
+
u.serialize_to_hash
|
|
32
|
+
=> {"nickname"=>"juandebravo", "email"=>"juan at pollinimini dot net", "password"=>"*******"}
|
|
33
|
+
|
|
34
|
+
u.serialize_to_hash(u.nickname)
|
|
35
|
+
=> {"juandebravo"=>{"nickname"=>"juandebravo", "email"=>"juan at pollinimini dot net", "password"=>"*******"}}
|
|
36
|
+
|
|
37
|
+
puts u.serialize_to_json
|
|
38
|
+
=> {"nickname":"juandebravo","email":"juan at pollinimini dot net","password":"*******"}
|
|
39
|
+
|
|
40
|
+
puts u.serialize_to_json("user")
|
|
41
|
+
=> {"user":{"nickname":"juandebravo","email":"juan at pollinimini dot net","password":"*******"}}
|
|
42
|
+
|
data/Rakefile
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
|
3
|
+
|
|
4
|
+
require "hash-serializer/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |s|
|
|
7
|
+
s.name = "hash-serializer"
|
|
8
|
+
s.version = HashSerializer::VERSION
|
|
9
|
+
s.platform = Gem::Platform::RUBY
|
|
10
|
+
s.authors = ["Juan de Bravo"]
|
|
11
|
+
s.email = ["juandebravo@gmail.com"]
|
|
12
|
+
s.homepage = ""
|
|
13
|
+
s.summary = %q{This gem provides an easy way to wrap a class attributes in a key-value representation (Hash object in ruby)}
|
|
14
|
+
s.description = <<END
|
|
15
|
+
Some months ago I worked in a project that defined the data serialization using a XSD schema. I used soap4r gem to create
|
|
16
|
+
ruby objects from the XSD schema, but later I needed to serialize the object value in JSON format. So I defined this
|
|
17
|
+
this extension that enables an easy way to encode an object attributes in Hash and in JSON format.
|
|
18
|
+
END
|
|
19
|
+
|
|
20
|
+
s.rubyforge_project = "hash-serializer"
|
|
21
|
+
|
|
22
|
+
s.files = `git ls-files`.split("\n")
|
|
23
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
|
24
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
25
|
+
s.require_paths = ["lib"]
|
|
26
|
+
|
|
27
|
+
s.add_dependency("json_pure", ">= 1.4.3")
|
|
28
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
class Object
|
|
4
|
+
|
|
5
|
+
#
|
|
6
|
+
# Instance method to parse an object to an equivalent Hash format
|
|
7
|
+
# Used to serialize body data
|
|
8
|
+
#
|
|
9
|
+
def serialize_to_hash(root_name = nil)
|
|
10
|
+
result = Hash.new
|
|
11
|
+
self.instance_variables.each do |column|
|
|
12
|
+
aux = self.instance_variable_get(column)
|
|
13
|
+
unless aux.nil?
|
|
14
|
+
if aux.instance_of?(String) or aux.kind_of?(String)
|
|
15
|
+
# It's required to erase any '_' character because
|
|
16
|
+
# soap4r has included it (not idea about the reason)
|
|
17
|
+
result[self.class.get_column_value(column)] = aux.to_s
|
|
18
|
+
else
|
|
19
|
+
if aux.is_a?(Array)
|
|
20
|
+
result_aux = Array.new
|
|
21
|
+
aux.each do |elem|
|
|
22
|
+
result_aux << elem.serialize_to_hash
|
|
23
|
+
end
|
|
24
|
+
else
|
|
25
|
+
result_aux = aux.serialize_to_hash
|
|
26
|
+
end
|
|
27
|
+
result[self.class.get_column_value(column)] = result_aux
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
unless root_name.nil?
|
|
32
|
+
temp = result
|
|
33
|
+
result = {}
|
|
34
|
+
result[root_name] = temp
|
|
35
|
+
end
|
|
36
|
+
result
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
#
|
|
40
|
+
# Convert the Hashed object to a String containing the object JSON format
|
|
41
|
+
#
|
|
42
|
+
def serialize_to_json(root_name = nil)
|
|
43
|
+
serialize_to_hash(root_name).to_json
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
class << self
|
|
49
|
+
#
|
|
50
|
+
# Retrieves the attribute name
|
|
51
|
+
#
|
|
52
|
+
def get_column_value(column)
|
|
53
|
+
column = column.to_s.sub(/@/, '')
|
|
54
|
+
unless column.index('_').nil?
|
|
55
|
+
column = column.split('_')[1]
|
|
56
|
+
end
|
|
57
|
+
column
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
end
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
require 'hash-serializer'
|
|
2
|
+
|
|
3
|
+
describe "hash-serializer" do
|
|
4
|
+
|
|
5
|
+
# Generates a method whose return value is memoized after the first call.
|
|
6
|
+
let(:obj) {
|
|
7
|
+
obj = Object.new
|
|
8
|
+
obj.instance_variable_set("@foo".to_sym, "bar")
|
|
9
|
+
obj.instance_variable_set("@bar".to_sym, "bazz")
|
|
10
|
+
obj
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
# Generates a method whose return value is memoized after the first call.
|
|
14
|
+
let(:complex_obj) {
|
|
15
|
+
complex_obj = Object.new
|
|
16
|
+
complex_obj.instance_variable_set("@foo".to_sym, "bar")
|
|
17
|
+
complex_obj.instance_variable_set("@bar".to_sym, "bazz")
|
|
18
|
+
complex_obj.instance_variable_set("@bazz".to_sym, obj)
|
|
19
|
+
complex_obj.instance_variable_set("@array".to_sym, [obj, obj])
|
|
20
|
+
complex_obj
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe "basic object" do
|
|
24
|
+
|
|
25
|
+
describe "object to json extension" do
|
|
26
|
+
it "converts properly a basic object with root name" do
|
|
27
|
+
json_obj = obj.serialize_to_json("object")
|
|
28
|
+
json_obj.should be_a_kind_of(String)
|
|
29
|
+
json_obj = JSON.parse(json_obj)
|
|
30
|
+
json_obj.should be_a_kind_of(Hash)
|
|
31
|
+
|
|
32
|
+
json_obj.should have_key("object")
|
|
33
|
+
json_obj = json_obj["object"]
|
|
34
|
+
json_obj.should have_key("foo")
|
|
35
|
+
json_obj.should have_key("bar")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
it "converts properly a basic object without root name" do
|
|
40
|
+
json_obj = obj.serialize_to_json
|
|
41
|
+
json_obj.should be_a_kind_of(String)
|
|
42
|
+
json_obj = JSON.parse(json_obj)
|
|
43
|
+
json_obj.should be_a_kind_of(Hash)
|
|
44
|
+
|
|
45
|
+
json_obj.should have_key("foo")
|
|
46
|
+
json_obj.should have_key("bar")
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
describe "object to hash extension" do
|
|
52
|
+
it "converts properly a basic object without root name" do
|
|
53
|
+
json_obj = obj.serialize_to_hash
|
|
54
|
+
|
|
55
|
+
json_obj.should be_a_kind_of(Hash)
|
|
56
|
+
json_obj.should have_key("foo")
|
|
57
|
+
json_obj.should have_key("bar")
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it "converts properly a basic object with root name" do
|
|
61
|
+
json_obj = obj.serialize_to_hash("foo")
|
|
62
|
+
json_obj.should be_a_kind_of(Hash)
|
|
63
|
+
json_obj.should have_key("foo")
|
|
64
|
+
|
|
65
|
+
json_obj = json_obj["foo"]
|
|
66
|
+
json_obj.should have_key("foo")
|
|
67
|
+
json_obj.should have_key("bar")
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it "converts properly a basic object with a symbol as root name" do
|
|
71
|
+
json_obj = obj.serialize_to_hash(:foo)
|
|
72
|
+
json_obj.should be_a_kind_of(Hash)
|
|
73
|
+
json_obj.should have_key(:foo)
|
|
74
|
+
|
|
75
|
+
json_obj = json_obj[:foo]
|
|
76
|
+
json_obj.should have_key("foo")
|
|
77
|
+
json_obj.should have_key("bar")
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
describe "object with another object as attribute" do
|
|
83
|
+
|
|
84
|
+
describe "object to hash extension" do
|
|
85
|
+
it "converts properly a complex object without root name" do
|
|
86
|
+
|
|
87
|
+
json_obj = complex_obj.serialize_to_hash
|
|
88
|
+
|
|
89
|
+
["foo", "bar", "bazz"].each { |key|
|
|
90
|
+
json_obj.should have_key(key)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
bazz = json_obj["bazz"]
|
|
94
|
+
["foo", "bar"].each { |key|
|
|
95
|
+
bazz.should have_key(key)
|
|
96
|
+
}
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
it "converts properly a complex object wit root name" do
|
|
100
|
+
|
|
101
|
+
json_obj = complex_obj.serialize_to_hash(:object)
|
|
102
|
+
|
|
103
|
+
json_obj.should be_a_kind_of(Hash)
|
|
104
|
+
json_obj.should have_key(:object)
|
|
105
|
+
json_obj = json_obj[:object]
|
|
106
|
+
|
|
107
|
+
["foo", "bar", "bazz"].each { |key|
|
|
108
|
+
json_obj.should have_key(key)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
bazz = json_obj["bazz"]
|
|
112
|
+
["foo", "bar"].each { |key|
|
|
113
|
+
bazz.should have_key(key)
|
|
114
|
+
}
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
describe "object with an array as attribute" do
|
|
122
|
+
describe "object to hash extension" do
|
|
123
|
+
it "converts properly a complex object without root name" do
|
|
124
|
+
|
|
125
|
+
json_obj = complex_obj.serialize_to_hash
|
|
126
|
+
|
|
127
|
+
["foo", "bar", "bazz", "array"].each { |key|
|
|
128
|
+
json_obj.should have_key(key)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
array = json_obj["array"]
|
|
132
|
+
array.length.should be 2
|
|
133
|
+
elem = array[0]
|
|
134
|
+
["foo", "bar"].each { |key|
|
|
135
|
+
elem.should have_key(key)
|
|
136
|
+
}
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
describe "object to json extension" do
|
|
141
|
+
|
|
142
|
+
it "converts properly a basic object with root name" do
|
|
143
|
+
json_obj = complex_obj.serialize_to_json
|
|
144
|
+
json_obj.should be_a_kind_of(String)
|
|
145
|
+
json_obj = JSON.parse(json_obj)
|
|
146
|
+
json_obj.should be_a_kind_of(Hash)
|
|
147
|
+
["foo", "bar", "bazz", "array"].each { |key|
|
|
148
|
+
json_obj.should have_key(key)
|
|
149
|
+
}
|
|
150
|
+
array = json_obj["array"]
|
|
151
|
+
array.length.should be 2
|
|
152
|
+
elem = array[0]
|
|
153
|
+
["foo", "bar"].each { |key|
|
|
154
|
+
elem.should have_key(key)
|
|
155
|
+
}
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: hash-serializer
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
prerelease: false
|
|
5
|
+
segments:
|
|
6
|
+
- 0
|
|
7
|
+
- 1
|
|
8
|
+
- 0
|
|
9
|
+
version: 0.1.0
|
|
10
|
+
platform: ruby
|
|
11
|
+
authors:
|
|
12
|
+
- Juan de Bravo
|
|
13
|
+
autorequire:
|
|
14
|
+
bindir: bin
|
|
15
|
+
cert_chain: []
|
|
16
|
+
|
|
17
|
+
date: 2011-04-05 00:00:00 +02:00
|
|
18
|
+
default_executable:
|
|
19
|
+
dependencies:
|
|
20
|
+
- !ruby/object:Gem::Dependency
|
|
21
|
+
name: json_pure
|
|
22
|
+
prerelease: false
|
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
24
|
+
none: false
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
segments:
|
|
29
|
+
- 1
|
|
30
|
+
- 4
|
|
31
|
+
- 3
|
|
32
|
+
version: 1.4.3
|
|
33
|
+
type: :runtime
|
|
34
|
+
version_requirements: *id001
|
|
35
|
+
description: " Some months ago I worked in a project that defined the data serialization using a XSD schema. I used soap4r gem to create\n ruby objects from the XSD schema, but later I needed to serialize the object value in JSON format. So I defined this\n this extension that enables an easy way to encode an object attributes in Hash and in JSON format.\n"
|
|
36
|
+
email:
|
|
37
|
+
- juandebravo@gmail.com
|
|
38
|
+
executables: []
|
|
39
|
+
|
|
40
|
+
extensions: []
|
|
41
|
+
|
|
42
|
+
extra_rdoc_files: []
|
|
43
|
+
|
|
44
|
+
files:
|
|
45
|
+
- .gitignore
|
|
46
|
+
- Gemfile
|
|
47
|
+
- README.md
|
|
48
|
+
- Rakefile
|
|
49
|
+
- hash-serializer.gemspec
|
|
50
|
+
- lib/hash-serializer.rb
|
|
51
|
+
- lib/hash-serializer/extensions/object.rb
|
|
52
|
+
- lib/hash-serializer/version.rb
|
|
53
|
+
- spec/hash-serializer-spec.rb
|
|
54
|
+
has_rdoc: true
|
|
55
|
+
homepage: ""
|
|
56
|
+
licenses: []
|
|
57
|
+
|
|
58
|
+
post_install_message:
|
|
59
|
+
rdoc_options: []
|
|
60
|
+
|
|
61
|
+
require_paths:
|
|
62
|
+
- lib
|
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
64
|
+
none: false
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
segments:
|
|
69
|
+
- 0
|
|
70
|
+
version: "0"
|
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
|
+
none: false
|
|
73
|
+
requirements:
|
|
74
|
+
- - ">="
|
|
75
|
+
- !ruby/object:Gem::Version
|
|
76
|
+
segments:
|
|
77
|
+
- 0
|
|
78
|
+
version: "0"
|
|
79
|
+
requirements: []
|
|
80
|
+
|
|
81
|
+
rubyforge_project: hash-serializer
|
|
82
|
+
rubygems_version: 1.3.7
|
|
83
|
+
signing_key:
|
|
84
|
+
specification_version: 3
|
|
85
|
+
summary: This gem provides an easy way to wrap a class attributes in a key-value representation (Hash object in ruby)
|
|
86
|
+
test_files:
|
|
87
|
+
- spec/hash-serializer-spec.rb
|