deepopenstruct 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +23 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +50 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/deepopenstruct.gemspec +52 -0
- data/lib/deepopenstruct.rb +26 -0
- data/test/setup/test_unit_extensions.rb +21 -0
- data/test/test_helper.rb +17 -0
- data/test/unit/deepopenstruct_test.rb +73 -0
- metadata +79 -0
data/.document
ADDED
data/.gitignore
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Aaron Gough (http://thingsaaronmade.com/)
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
== deepopenstruct
|
2
|
+
|
3
|
+
DeepOpenStruct is a simple library for creating easy-to-use data structures from complex sets of nested Hashes and Arrays. It is particularly suitable for creating easy-to-use data structures from YAML files, and as such is a useful tool for creating simple reflective API wrappers.
|
4
|
+
|
5
|
+
=== Installation
|
6
|
+
|
7
|
+
gem install deepopenstruct
|
8
|
+
|
9
|
+
=== Usage
|
10
|
+
|
11
|
+
require 'rubygems'
|
12
|
+
require 'deepopenstruct'
|
13
|
+
|
14
|
+
complex_data = {
|
15
|
+
:name => "Bob Winkle",
|
16
|
+
:age => 65,
|
17
|
+
:jobs => [
|
18
|
+
{'start_year' => 1980, 'title' => 'Chef'},
|
19
|
+
{'start_year' => 1985, 'title' => 'Programmer'}
|
20
|
+
],
|
21
|
+
:attributes => {
|
22
|
+
:birthplace => "Darwin",
|
23
|
+
:year_of_birth => 1945
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
27
|
+
simple_data = DeepOpenStruct.load(complex_data)
|
28
|
+
|
29
|
+
simple_data.name
|
30
|
+
# => "Bob Winkle"
|
31
|
+
|
32
|
+
simple_data.jobs.first.title
|
33
|
+
# => "Chef"
|
34
|
+
|
35
|
+
simple_data.attributes.birthplace
|
36
|
+
# => "Darwin"
|
37
|
+
|
38
|
+
=== Note on Patches/Pull Requests
|
39
|
+
|
40
|
+
* Fork the project.
|
41
|
+
* Make your feature addition or bug fix.
|
42
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
43
|
+
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
44
|
+
* Send me a pull request! Bonus points for topic/feature branches.
|
45
|
+
|
46
|
+
=== Author & Credits
|
47
|
+
|
48
|
+
Author:: {Aaron Gough}[mailto:aaron@aarongough.com]
|
49
|
+
|
50
|
+
Copyright (c) 2010 {Aaron Gough}[http://thingsaaronmade.com/] ({thingsaaronmade.com}[http://thingsaaronmade.com/]), released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "deepopenstruct"
|
8
|
+
gem.summary = %Q{A simple library for creating easy-to-use data structures from complex sets of nested Hashes and Arrays}
|
9
|
+
gem.description = %Q{DeepOpenStruct is a simple library for creating easy-to-use data structures from complex sets of nested Hashes and Arrays. It is particularly suitable for creating easy-to-use data structures from YAML files, and as such is a useful tool for creating simple reflective API wrappers.}
|
10
|
+
gem.email = "aaron@aarongough.com"
|
11
|
+
gem.homepage = "http://github.com/aarongough/deepopenstruct"
|
12
|
+
gem.authors = ["Aaron Gough"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/*_test.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
task :test => :check_dependencies
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "deepopenstruct #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{deepopenstruct}
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Aaron Gough"]
|
12
|
+
s.date = %q{2010-06-04}
|
13
|
+
s.description = %q{DeepOpenStruct is a simple library for creating easy-to-use data structures from complex sets of nested Hashes and Arrays. It is particularly suitable for creating easy-to-use data structures from YAML files, and as such is a useful tool for creating simple reflective API wrappers.}
|
14
|
+
s.email = %q{aaron@aarongough.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".document",
|
20
|
+
".gitignore",
|
21
|
+
"MIT-LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"deepopenstruct.gemspec",
|
26
|
+
"lib/deepopenstruct.rb",
|
27
|
+
"test/setup/test_unit_extensions.rb",
|
28
|
+
"test/test_helper.rb",
|
29
|
+
"test/unit/deepopenstruct_test.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/aarongough/deepopenstruct}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.7}
|
35
|
+
s.summary = %q{A simple library for creating easy-to-use data structures from complex sets of nested Hashes and Arrays}
|
36
|
+
s.test_files = [
|
37
|
+
"test/setup/test_unit_extensions.rb",
|
38
|
+
"test/test_helper.rb",
|
39
|
+
"test/unit/deepopenstruct_test.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
47
|
+
else
|
48
|
+
end
|
49
|
+
else
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
class DeepOpenStruct
|
4
|
+
def self.load(item)
|
5
|
+
raise ArgumentError, "DeepOpenStruct must be passed a Hash or Array" unless(item.is_a?(Hash) || item.is_a?(Array))
|
6
|
+
item = item.clone
|
7
|
+
if(item.is_a?(Hash))
|
8
|
+
item.merge!(item) do |key, value, oldvalue|
|
9
|
+
if(value.is_a?(Hash) || value.is_a?(Array))
|
10
|
+
DeepOpenStruct.load(value)
|
11
|
+
else
|
12
|
+
value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
return OpenStruct.new(item)
|
16
|
+
elsif(item.is_a?(Array))
|
17
|
+
return item.map do |value|
|
18
|
+
if(value.is_a?(Hash) || value.is_a?(Array))
|
19
|
+
DeepOpenStruct.load(value)
|
20
|
+
else
|
21
|
+
value
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Test::Unit
|
2
|
+
# Used to fix a minor minitest/unit incompatibility in flexmock
|
3
|
+
AssertionFailedError = Class.new(StandardError)
|
4
|
+
|
5
|
+
class TestCase
|
6
|
+
|
7
|
+
def self.test(name, &block)
|
8
|
+
test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
|
9
|
+
defined = instance_method(test_name) rescue false
|
10
|
+
raise "#{test_name} is already defined in #{self}" if defined
|
11
|
+
if block_given?
|
12
|
+
define_method(test_name, &block)
|
13
|
+
else
|
14
|
+
define_method(test_name) do
|
15
|
+
flunk "No implementation provided for #{name}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
7
|
+
require 'deepopenstruct'
|
8
|
+
|
9
|
+
require_files = []
|
10
|
+
require_files.concat Dir[File.join(File.dirname(__FILE__), 'setup', '*.rb')]
|
11
|
+
|
12
|
+
require_files.each do |file|
|
13
|
+
require File.expand_path(file)
|
14
|
+
end
|
15
|
+
|
16
|
+
class Test::Unit::TestCase
|
17
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestDeepOpenStruct < Test::Unit::TestCase
|
4
|
+
|
5
|
+
test "should convert hash into openstruct" do
|
6
|
+
hash = {:item_1 => 1, 'item_2' => 2, "item_3" => 3}
|
7
|
+
assert_nothing_raised do
|
8
|
+
deepstruct = DeepOpenStruct.load(hash)
|
9
|
+
assert_equal 1, deepstruct.item_1
|
10
|
+
assert_equal 2, deepstruct.item_2
|
11
|
+
assert_equal 3, deepstruct.item_3
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
test "should convert array of hashes into array of openstructs" do
|
16
|
+
array = [{:hash_1 => 1}, {:hash_2 => 2}, {:hash_3 => 3}]
|
17
|
+
assert_nothing_raised do
|
18
|
+
deepstruct = DeepOpenStruct.load(array)
|
19
|
+
assert_equal 1, deepstruct[0].hash_1
|
20
|
+
assert_equal 2, deepstruct[1].hash_2
|
21
|
+
assert_equal 3, deepstruct[2].hash_3
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
test "should convert nested hash into nested openstruct" do
|
26
|
+
hash = {:hash_1 => {:foo => :blah}, :hash_2 => {:foo => :blah, :deep_nest => {:foo => :blah}}}
|
27
|
+
assert_nothing_raised do
|
28
|
+
deepstruct = DeepOpenStruct.load(hash)
|
29
|
+
assert_equal :blah, deepstruct.hash_1.foo
|
30
|
+
assert_equal :blah, deepstruct.hash_2.foo
|
31
|
+
assert_equal :blah, deepstruct.hash_2.deep_nest.foo
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
test "should convert arrays of hashes in hash into arrays of openstructs" do
|
36
|
+
hash = {:array => [1, 2, {:foo => :blah, :array => [3, 4, {:see => :more}]}]}
|
37
|
+
assert_nothing_raised do
|
38
|
+
deepstruct = DeepOpenStruct.load(hash)
|
39
|
+
assert_kind_of Array, deepstruct.array
|
40
|
+
assert_equal 1, deepstruct.array[0]
|
41
|
+
assert_equal 2, deepstruct.array[1]
|
42
|
+
assert_kind_of OpenStruct, deepstruct.array[2]
|
43
|
+
assert_equal :blah, deepstruct.array[2].foo
|
44
|
+
assert_kind_of Array, deepstruct.array[2].array
|
45
|
+
assert_equal 3, deepstruct.array[2].array[0]
|
46
|
+
assert_equal 4, deepstruct.array[2].array[1]
|
47
|
+
assert_kind_of OpenStruct, deepstruct.array[2].array[2]
|
48
|
+
assert_equal :more, deepstruct.array[2].array[2].see
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
test "should raise ArgumentError if argument supplied to initialize is not a Hash or Array" do
|
53
|
+
[1, "hello", 3.00, OpenStruct.new({:blah => :foo})].each do |item|
|
54
|
+
assert_raises ArgumentError, "should have raised error when initialized with #{item.class}" do
|
55
|
+
test = DeepOpenStruct.load(item)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
test "should not not create side effects on array" do
|
61
|
+
array = [{:hash_1 => 1}, {:hash_2 => 2}, {:hash_3 => 3}]
|
62
|
+
deepstruct = DeepOpenStruct.load(array)
|
63
|
+
assert_kind_of Hash, array[0]
|
64
|
+
end
|
65
|
+
|
66
|
+
test "should not not create side effects on hash" do
|
67
|
+
hash = {:array => [1, 2, {:foo => :blah, :array => [3, 4, {:see => :more}]}]}
|
68
|
+
deepstruct = DeepOpenStruct.load(hash)
|
69
|
+
assert_kind_of Hash, hash[:array][2]
|
70
|
+
assert_kind_of Hash, hash[:array][2][:array][2]
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: deepopenstruct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Aaron Gough
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-06-04 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: DeepOpenStruct is a simple library for creating easy-to-use data structures from complex sets of nested Hashes and Arrays. It is particularly suitable for creating easy-to-use data structures from YAML files, and as such is a useful tool for creating simple reflective API wrappers.
|
23
|
+
email: aaron@aarongough.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- .document
|
32
|
+
- .gitignore
|
33
|
+
- MIT-LICENSE
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- VERSION
|
37
|
+
- deepopenstruct.gemspec
|
38
|
+
- lib/deepopenstruct.rb
|
39
|
+
- test/setup/test_unit_extensions.rb
|
40
|
+
- test/test_helper.rb
|
41
|
+
- test/unit/deepopenstruct_test.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/aarongough/deepopenstruct
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --charset=UTF-8
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.3.7
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: A simple library for creating easy-to-use data structures from complex sets of nested Hashes and Arrays
|
76
|
+
test_files:
|
77
|
+
- test/setup/test_unit_extensions.rb
|
78
|
+
- test/test_helper.rb
|
79
|
+
- test/unit/deepopenstruct_test.rb
|