hash-to-ostruct 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -0,0 +1,14 @@
1
+ Manifest.txt
2
+ README.rdoc
3
+ Rakefile
4
+ lib/hash-to-ostruct.rb
5
+ lib/hash-to-ostruct/extensions/array.rb
6
+ lib/hash-to-ostruct/extensions/hash.rb
7
+ lib/hash-to-ostruct/extensions/object.rb
8
+ script/console
9
+ script/destroy
10
+ script/generate
11
+ spec/hash_of_arrays_spec.rb
12
+ spec/hash_of_hashes_spec.rb
13
+ spec/hash_with_simple_values_spec.rb
14
+ tasks/spec.rake
@@ -0,0 +1,29 @@
1
+ == DESCRIPTION:
2
+ hash-to-ostruct recursively transforms Hash to OpenStruct
3
+
4
+ It supports:
5
+ * hash of hashes
6
+ * hash of arrays with hashes as elements
7
+
8
+ == SYNOPSIS:
9
+ # Example One: Hash of hashes
10
+ require 'hash-to-ostruct'
11
+
12
+ countries_hash = {}
13
+ countries_hash[:australia] = { :capital => 'Canberra', :currency_code => 'AUD' }
14
+ countries_hash[:england] = { :capital => 'London', :currency_code => 'GBP' }
15
+
16
+ countries = countries_hash.to_ostruct
17
+
18
+ countries.australia.capital # returns "Canberra"
19
+ countries.australia.currency_code # returns "AUD"
20
+
21
+ countries.england.capital # returns "London"
22
+ countries.england.currency_code # returns "GBP"
23
+
24
+ == INSTALL:
25
+
26
+ sudo gem install hash-to-ostruct
27
+
28
+
29
+ Copyright (c) 2009 Kunal Parikh (mailto:kunal@techthumb.in)
@@ -0,0 +1,24 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/hash-to-ostruct'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+
7
+ $hoe = Hoe.new('hash-to-ostruct', HashToOstruct::VERSION) do |p|
8
+ p.developer('Kunal Parikh', 'kunal@techthumb.in')
9
+ p.rubyforge_name = p.name
10
+ p.extra_dev_deps = [
11
+ ['newgem', ">= #{::Newgem::VERSION}"]
12
+ ]
13
+
14
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
15
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
16
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
17
+ p.rsync_args = '-av --delete --ignore-errors'
18
+ end
19
+
20
+ require 'newgem/tasks' # load /tasks/*.rake
21
+ Dir['tasks/**/*.rake'].each { |t| load t }
22
+
23
+ # TODO - want other tests/tasks run by default? Add them to the list
24
+ task :default => [:spec]
@@ -0,0 +1,9 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+ require 'ostruct'
4
+ require 'hash-to-ostruct/extensions/object'
5
+ require 'hash-to-ostruct/extensions/array'
6
+ require 'hash-to-ostruct/extensions/hash'
7
+ module HashToOstruct
8
+ VERSION = '0.0.1'
9
+ end
@@ -0,0 +1,5 @@
1
+ Array.class_eval do
2
+ def to_ostruct
3
+ map { | element | element.to_ostruct }
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ Hash.class_eval do
2
+ def to_ostruct
3
+ open_struct = OpenStruct.new
4
+ each { | key, value | open_struct.send("#{key}=", value.to_ostruct)}
5
+ open_struct
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ Object.class_eval do
2
+ def to_ostruct
3
+ self
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/hash-to-ostruct.rb'}"
9
+ puts "Loading hash-to-ostruct gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,37 @@
1
+ require 'lib/hash-to-ostruct'
2
+
3
+ describe 'hash of arrays with hashes as array elements' do
4
+ it 'should handle an array of hashes with the same keys' do
5
+ array_of_hashes = [{:same_key => 'child element one'}, {:same_key => 'child element two'}]
6
+ hash = {:children => array_of_hashes}
7
+ ostruct = hash.to_ostruct
8
+ children = ostruct.children
9
+ array_of_hashes.each_with_index do | hash, index |
10
+ hash.each do | key, value |
11
+ children[index].should respond_to key
12
+ children[index].send(key).should eql value
13
+ end
14
+ end
15
+ end
16
+
17
+ it 'should handle an array of hashes with the different keys' do
18
+ array_of_hashes = [{:some_key => 'child element one'}, {:different_key => 'child element two'}]
19
+ hash = {:children => array_of_hashes}
20
+ ostruct = hash.to_ostruct
21
+ children = ostruct.children
22
+ array_of_hashes.each_with_index do | hash, index |
23
+ hash.each do | key, value |
24
+ children[index].should respond_to key
25
+ children[index].send(key).should eql value
26
+ end
27
+ end
28
+ end
29
+
30
+ it 'should handle an array of strings' do
31
+ array = ["a", 1]
32
+ hash = {:children => array}
33
+ ostruct = hash.to_ostruct
34
+ ostruct.children.should eql array
35
+ end
36
+
37
+ end
@@ -0,0 +1,16 @@
1
+ require 'lib/hash-to-ostruct'
2
+
3
+ describe 'hash of hashes' do
4
+ it 'should handle chaining of child elements as hashes for depth level of 1' do
5
+ hash = {:child => {:some_key, :some_value}}
6
+ struct = hash.to_ostruct
7
+ struct.should respond_to :child
8
+ struct.child.should respond_to :some_key
9
+ end
10
+
11
+ it 'should handle chaining of child elements as hashes for any depth level (testing for 5 levels deep)' do
12
+ hash = {:level_one => {:level_two => {:level_three => {:level_four => {:level_five => "Hi!"} } } } }
13
+ struct = hash.to_ostruct
14
+ struct.level_one.level_two.level_three.level_four.level_five.should eql "Hi!"
15
+ end
16
+ end
@@ -0,0 +1,51 @@
1
+ require 'lib/hash-to-ostruct'
2
+
3
+ describe 'Hash to OpenStruct transformation with simple values' do
4
+ it 'should transform strings as values' do
5
+ keys = []
6
+ hash = Hash.new
7
+ hash['keyA'] = "Value A"
8
+ hash['keyB'] = "Value B"
9
+ ostruct = hash.to_ostruct
10
+ hash.each do | key, value |
11
+ ostruct.should respond_to key
12
+ ostruct.send(key).should eql value
13
+ end
14
+ end
15
+
16
+ it 'should transform numbers as values' do
17
+ keys = []
18
+ hash = Hash.new
19
+ hash['keyA'] = 1
20
+ hash['keyB'] = 2
21
+ ostruct = hash.to_ostruct
22
+ hash.each do | key, value |
23
+ ostruct.should respond_to key
24
+ ostruct.send(key).should eql value
25
+ end
26
+ end
27
+
28
+ it 'should transform symbols as values' do
29
+ keys = []
30
+ hash = Hash.new
31
+ hash[:keyA] = :ValueA
32
+ hash[:keyB] = :ValueB
33
+ ostruct = hash.to_ostruct
34
+ hash.each do | key, value |
35
+ ostruct.should respond_to key
36
+ ostruct.send(key).should eql value
37
+ end
38
+ end
39
+
40
+ it 'should transform symbols as keys' do
41
+ keys = []
42
+ hash = Hash.new
43
+ hash[:keyA] = "Value A"
44
+ hash[:keyB] = "Value B"
45
+ ostruct = hash.to_ostruct
46
+ hash.each do | key, value |
47
+ ostruct.should respond_to key
48
+ ostruct.send(key).should eql value
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,8 @@
1
+ require 'rake'
2
+ require 'spec/rake/spectask'
3
+
4
+ desc "Run all examples"
5
+ Spec::Rake::SpecTask.new('spec') do |t|
6
+ t.spec_opts = ['--format specdoc', '--color']
7
+ t.spec_files = FileList['spec/**/*.rb']
8
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash-to-ostruct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kunal Parikh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDMjCCAhqgAwIBAgIBADANBgkqhkiG9w0BAQUFADA/MQ4wDAYDVQQDDAVrdW5h
14
+ bDEZMBcGCgmSJomT8ixkARkWCXRlY2h0aHVtYjESMBAGCgmSJomT8ixkARkWAmlu
15
+ MB4XDTA5MDExODAxMTEwNloXDTEwMDExODAxMTEwNlowPzEOMAwGA1UEAwwFa3Vu
16
+ YWwxGTAXBgoJkiaJk/IsZAEZFgl0ZWNodGh1bWIxEjAQBgoJkiaJk/IsZAEZFgJp
17
+ bjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKFsL34fhFATqXvYy7Pk
18
+ wRzjFxdZoxbF6EeME1ZX9OOxMzZooyLeWAPHb9a7Cxg+UXfrCEl7m09BH+3Qr6SH
19
+ 0nA1S3RMTgSXr6XIscAiErW7EaSLU0J9oOtsgcrioYqmgYP8UUbObcM+h9Hh7AwL
20
+ 5J7FWPQYyd6nJdJILkCH5bi2FeMxJvoI3SkNFdIm4UnDRLCqiMYRsm3kkZvqpnx5
21
+ s+FR9u8mqEYA8aJqyYVvmMv9W5Um0eND4Wu0NU5g/WvwSk5988aQnsYEEbb5hv8U
22
+ qAoDnZFfN08RQQP8uXNkk3TSJ4vWQAAFs3S/NfUCSJtv/cwDuFlpVkoaKRbKlEqx
23
+ uLcCAwEAAaM5MDcwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0OBBYEFKqS
24
+ Is0RqZINNq4njnBmHfNrYW3tMA0GCSqGSIb3DQEBBQUAA4IBAQBDJn6Olo5i214P
25
+ GCiimxa5YbaDUWbRRS+gqXGMAmAs7LO/gJJW30s+KD5tTOqoTQDFAcEt/zBlo/nW
26
+ A8vI6q/cP/4Rx9sQjLLkpWYdXBou9jw9OW1Ru3/HxU4u6CBIGfBUX7ki+6qeYkWB
27
+ gw3XPyLE+waOz/2dGqB1iKBAUYsHK4WHohE293tS2LLseu7WPtI0EilG1HvAYTyM
28
+ vBjTrRRTWuzc3sTEcblUeiv02w1z3nXm+JAmkblC3vvP5yfcrjehcNzJxTHkVPSE
29
+ TiAejxDzSKCwxJhZF/HNttEs8Qm6r2I+TJmm/iKOBv0x68cQfE/mpooWC88aMiLp
30
+ 2ZurMrq1
31
+ -----END CERTIFICATE-----
32
+
33
+ date: 2009-01-18 00:00:00 +11:00
34
+ default_executable:
35
+ dependencies:
36
+ - !ruby/object:Gem::Dependency
37
+ name: newgem
38
+ type: :development
39
+ version_requirement:
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 1.2.3
45
+ version:
46
+ - !ruby/object:Gem::Dependency
47
+ name: hoe
48
+ type: :development
49
+ version_requirement:
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 1.8.0
55
+ version:
56
+ description: "hash-to-ostruct recursively transforms Hash to OpenStruct It supports: * hash of hashes *\thash of arrays with hashes as elements"
57
+ email:
58
+ - kunal@techthumb.in
59
+ executables: []
60
+
61
+ extensions: []
62
+
63
+ extra_rdoc_files:
64
+ - Manifest.txt
65
+ - README.rdoc
66
+ files:
67
+ - Manifest.txt
68
+ - README.rdoc
69
+ - Rakefile
70
+ - lib/hash-to-ostruct.rb
71
+ - lib/hash-to-ostruct/extensions/array.rb
72
+ - lib/hash-to-ostruct/extensions/hash.rb
73
+ - lib/hash-to-ostruct/extensions/object.rb
74
+ - script/console
75
+ - script/destroy
76
+ - script/generate
77
+ - spec/hash_of_arrays_spec.rb
78
+ - spec/hash_of_hashes_spec.rb
79
+ - spec/hash_with_simple_values_spec.rb
80
+ - tasks/spec.rake
81
+ has_rdoc: true
82
+ homepage: hash-to-ostruct recursively transforms Hash to OpenStruct
83
+ post_install_message:
84
+ rdoc_options:
85
+ - --main
86
+ - README.rdoc
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ version:
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: "0"
100
+ version:
101
+ requirements: []
102
+
103
+ rubyforge_project: hash-to-ostruct
104
+ rubygems_version: 1.3.0
105
+ signing_key:
106
+ specification_version: 2
107
+ summary: "hash-to-ostruct recursively transforms Hash to OpenStruct It supports: * hash of hashes *\thash of arrays with hashes as elements"
108
+ test_files: []
109
+
Binary file