to_hash 0.1.2

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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jakub
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
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 methods.
4
+
5
+ == Usage
6
+
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.
61
+
62
+ = License
63
+
64
+ Copyright (c) 2009 Jakub Kuźma, released under the MIT license
@@ -0,0 +1,58 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = "to_hash"
10
+ gem.summary = %Q{TODO}
11
+ gem.email = "qoobaa@gmail.com"
12
+ gem.homepage = "http://github.com/qoobaa/to_hash"
13
+ gem.authors = ["Jakub Kuźma"]
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/*_test.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/*_test.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION.yml')
47
+ config = YAML.load(File.read('VERSION.yml'))
48
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
49
+ else
50
+ version = ""
51
+ end
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "to_hash #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
58
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
@@ -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,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'to_hash'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,81 @@
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
+ 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
79
+ end
80
+ end
81
+
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_hash
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 2
10
+ version: 0.1.2
11
+ platform: ruby
12
+ authors:
13
+ - "Jakub Ku\xC5\xBAma"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2009-05-13 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description:
22
+ email: qoobaa@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.rdoc
30
+ files:
31
+ - .document
32
+ - .gitignore
33
+ - LICENSE
34
+ - README.rdoc
35
+ - Rakefile
36
+ - VERSION
37
+ - lib/to_hash.rb
38
+ - test/test_helper.rb
39
+ - test/to_hash_test.rb
40
+ homepage: http://github.com/qoobaa/to_hash
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --charset=UTF-8
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.10
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: A fork of qoobaa's to_hash gem forked to build the gem on rubygems.org
73
+ test_files:
74
+ - test/to_hash_test.rb
75
+ - test/test_helper.rb