qoobaa-to_hash 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,13 @@
1
+ = to_hash
2
+
3
+ Easy and powerful object to hash serialization tool. It may be used together with to_json/to_xml/to_yaml standard methods.
4
+
5
+ == Usage
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
10
+
11
+ = License
12
+
13
+ Copyright (c) 2009 Jakub Kuźma, released under the MIT license
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 0
data/lib/to_hash.rb ADDED
@@ -0,0 +1,37 @@
1
+ module ToHash
2
+ def self.eval_object(object, attributes)
3
+ if object.kind_of?(Array)
4
+ object.map { |o| eval_attributes(o, attributes) }
5
+ else
6
+ eval_attributes(object, attributes)
7
+ end
8
+ end
9
+
10
+ def self.eval_attributes(object, attributes)
11
+ result = {}
12
+ attributes = attributes.first if attributes.first.kind_of?(Hash)
13
+ case attributes
14
+ when Array
15
+ attributes.each do |attribute|
16
+ if attribute.kind_of?(Array)
17
+ result[attribute.first] = eval_object(object.send(attribute.first), attribute[1..-1])
18
+ else
19
+ result[attribute] = object.send(attribute)
20
+ end
21
+ end
22
+ when Hash
23
+ attributes.each do |key, attribute|
24
+ if attribute.kind_of?(Array)
25
+ result[key] = eval_object(object.send(attribute.first), attribute[1..-1])
26
+ else
27
+ result[key] = object.send(attribute)
28
+ end
29
+ end
30
+ end
31
+ result
32
+ end
33
+
34
+ def to_hash(*attributes)
35
+ ToHash.eval_attributes(self, attributes)
36
+ end
37
+ end
@@ -0,0 +1,51 @@
1
+ require "test/unit"
2
+ require "shoulda"
3
+ require "ostruct"
4
+
5
+ require File.join(File.dirname(__FILE__), "..", "lib", "to_hash")
6
+
7
+ class ToHashTest < Test::Unit::TestCase
8
+ context "project structures serialization" do
9
+ setup do
10
+ @project1 = OpenStruct.new(:title => "Project 1", :description => "Project 1 description")
11
+ @project2 = OpenStruct.new(:title => "Project 2")
12
+ @project3 = OpenStruct.new(:title => "Project 3")
13
+
14
+ @project1.authors = []
15
+ @project1.authors << OpenStruct.new(:first_name => "John", :last_name => "Doe", :projects => [@project1])
16
+ @project1.authors << OpenStruct.new(:first_name => "Michael", :last_name => "Smith", :projects => [@project1, @project2])
17
+ @project1.authors << OpenStruct.new(:first_name => "Jimmy", :last_name => "Parker", :projects => [@project1, @project2, @project3])
18
+ @project1.company = OpenStruct.new(:name => "HAL")
19
+
20
+ @project1.extend(ToHash)
21
+ end
22
+
23
+ should "return empty hash when no arguments given" do
24
+ hash = @project1.to_hash
25
+ assert_equal hash, {}
26
+ end
27
+
28
+ should "serialize data using title symbol" do
29
+ hash = @project1.to_hash(:title)
30
+ assert_equal hash, { :title => "Project 1" }
31
+ end
32
+
33
+ should "serialize data using title and description" do
34
+ hash = @project1.to_hash(:title, :description)
35
+ assert_equal hash, { :title => "Project 1", :description => "Project 1 description" }
36
+ end
37
+
38
+ should "allow to change one key in hash" do
39
+ hash = @project1.to_hash(:myTitle => :title)
40
+ assert_equal hash, { :myTitle => "Project 1" }
41
+ end
42
+
43
+ should "allow to change multiple keys in hash" do
44
+ hash = @project1.to_hash(:myTitle => :title, :myDesc => :description)
45
+ assert_equal hash, { :myTitle => "Project 1", :myDesc => "Project 1 description" }
46
+ end
47
+
48
+ # TODO: more test (no time right now)
49
+ end
50
+ end
51
+
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qoobaa-to_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - "Jakub Ku\xC5\xBAma"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-10 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Use to_hash(...) together with default to_json/to_xml/to_yaml methods to get quick and powerful object serialization.
17
+ email: qoobaa@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.rdoc
26
+ - VERSION.yml
27
+ - lib/to_hash.rb
28
+ - test/to_hash_test.rb
29
+ has_rdoc: false
30
+ homepage: http://github.com/qoobaa/to_hash
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.2.0
52
+ signing_key:
53
+ specification_version: 2
54
+ summary: ToHash module for easy object serialization to JSON, XML, YAML, etc.
55
+ test_files: []
56
+