recursive-open-struct 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/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +28 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +26 -0
- data/Rakefile +60 -0
- data/VERSION +1 -0
- data/lib/recursive_open_struct.rb +18 -0
- data/spec/recursive_open_struct_spec.rb +78 -0
- data/spec/spec_helper.rb +12 -0
- metadata +111 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec" #, "~> 2.3.0"
|
10
|
+
gem "bundler" #, "~> 1.0.0"
|
11
|
+
gem "jeweler" #, "~> 1.6.0"
|
12
|
+
gem "rcov" #, ">= 0"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.6.0)
|
7
|
+
bundler (~> 1.0.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rake (0.8.7)
|
11
|
+
rcov (0.9.9)
|
12
|
+
rspec (2.6.0)
|
13
|
+
rspec-core (~> 2.6.0)
|
14
|
+
rspec-expectations (~> 2.6.0)
|
15
|
+
rspec-mocks (~> 2.6.0)
|
16
|
+
rspec-core (2.6.1)
|
17
|
+
rspec-expectations (2.6.0)
|
18
|
+
diff-lcs (~> 1.1.2)
|
19
|
+
rspec-mocks (2.6.0)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
bundler
|
26
|
+
jeweler
|
27
|
+
rcov
|
28
|
+
rspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 William (B.J.) Snow Orvis
|
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,26 @@
|
|
1
|
+
= recursive-open-struct
|
2
|
+
|
3
|
+
RecursiveOpenStruct is a subclass of OpenStruct. It differs from
|
4
|
+
OpenStruct in that it allows nested hashes to be treated in a recursive
|
5
|
+
fashion. For example:
|
6
|
+
|
7
|
+
ros = RecursiveOpenStruct.new({ :a => { :b => 'c' } })
|
8
|
+
ros.a.b # 'c'
|
9
|
+
|
10
|
+
Also, nested hashes can still be accessed as hashes:
|
11
|
+
|
12
|
+
ros.a_as_a_hash # { :b => 'c' }
|
13
|
+
|
14
|
+
== Note on Patches/Pull Requests
|
15
|
+
|
16
|
+
* Fork the project.
|
17
|
+
* Make your feature addition or bug fix.
|
18
|
+
* Add tests for it. This is important so I don't break it in a
|
19
|
+
future version unintentionally.
|
20
|
+
* Commit, do not mess with rakefile, version, or history.
|
21
|
+
(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)
|
22
|
+
* Send me a pull request. Bonus points for topic branches.
|
23
|
+
|
24
|
+
== Copyright
|
25
|
+
|
26
|
+
Copyright (c) 2010 William (B.J.) Snow Orvis. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "recursive-open-struct"
|
18
|
+
gem.homepage = "http://github.com/aetherknight/recursive-open-struct"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{OpenStruct subclass that returns nested hash attributes as RecursiveOpenStructs}
|
21
|
+
gem.description = <<EOF
|
22
|
+
RecursiveOpenStruct is a subclass of OpenStruct. It differs from
|
23
|
+
OpenStruct in that it allows nested hashes to be treated in a recursive
|
24
|
+
fashion. For example:
|
25
|
+
|
26
|
+
ros = RecursiveOpenStruct.new({ :a => { :b => 'c' } })
|
27
|
+
ros.a.b # 'c'
|
28
|
+
|
29
|
+
Also, nested hashes can still be accessed as hashes:
|
30
|
+
|
31
|
+
ros.a_as_a_hash # { :b => 'c' }
|
32
|
+
EOF
|
33
|
+
gem.email = "aetherknight@gmail.com"
|
34
|
+
gem.authors = ["William (B.J.) Snow Orvis"]
|
35
|
+
# dependencies defined in Gemfile
|
36
|
+
end
|
37
|
+
Jeweler::RubygemsDotOrgTasks.new
|
38
|
+
|
39
|
+
require 'rspec/core'
|
40
|
+
require 'rspec/core/rake_task'
|
41
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
42
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
43
|
+
end
|
44
|
+
|
45
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
46
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
47
|
+
spec.rcov = true
|
48
|
+
end
|
49
|
+
|
50
|
+
task :default => :spec
|
51
|
+
|
52
|
+
require 'rake/rdoctask'
|
53
|
+
Rake::RDocTask.new do |rdoc|
|
54
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
55
|
+
|
56
|
+
rdoc.rdoc_dir = 'rdoc'
|
57
|
+
rdoc.title = "recursive-open-struct #{version}"
|
58
|
+
rdoc.rdoc_files.include('README*')
|
59
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
60
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
class RecursiveOpenStruct < OpenStruct
|
4
|
+
def new_ostruct_member(name)
|
5
|
+
name = name.to_sym
|
6
|
+
unless self.respond_to?(name)
|
7
|
+
class << self; self; end.class_eval do
|
8
|
+
define_method(name) {
|
9
|
+
v = @table[name]
|
10
|
+
v.is_a?(Hash) ? RecursiveOpenStruct.new(v) : v
|
11
|
+
}
|
12
|
+
define_method("#{name}=") { |x| modifiable[name] = x }
|
13
|
+
define_method("#{name}_as_a_hash") { @table[name] }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
name
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'recursive_open_struct'
|
3
|
+
|
4
|
+
describe RecursiveOpenStruct do
|
5
|
+
describe "behavior it inherits from OpenStruct" do
|
6
|
+
|
7
|
+
it "can represent arbitrary data objects" do
|
8
|
+
ros = RecursiveOpenStruct.new
|
9
|
+
ros.blah = "John Smith"
|
10
|
+
ros.blah.should == "John Smith"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "can be created from a hash" do
|
14
|
+
h = { :asdf => 'John Smith' }
|
15
|
+
ros = RecursiveOpenStruct.new(h)
|
16
|
+
ros.asdf.should == "John Smith"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "can modify an existing key" do
|
20
|
+
h = { :blah => 'John Smith' }
|
21
|
+
ros = RecursiveOpenStruct.new(h)
|
22
|
+
ros.blah = "George Washington"
|
23
|
+
ros.blah.should == "George Washington"
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "handling of arbitrary attributes" do
|
27
|
+
before(:each) do
|
28
|
+
@ros = RecursiveOpenStruct.new
|
29
|
+
@ros.blah = "John Smith"
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#respond?" do
|
33
|
+
it { @ros.should respond_to :blah }
|
34
|
+
it { @ros.should respond_to :blah= }
|
35
|
+
it { @ros.should_not respond_to :asdf }
|
36
|
+
it { @ros.should_not respond_to :asdf= }
|
37
|
+
end # describe #respond?
|
38
|
+
|
39
|
+
describe "#methods" do
|
40
|
+
it { @ros.methods.should include :blah }
|
41
|
+
it { @ros.methods.should include :blah= }
|
42
|
+
it { @ros.methods.should_not include :asdf }
|
43
|
+
it { @ros.methods.should_not include :asdf= }
|
44
|
+
end # describe #methods
|
45
|
+
end # describe handling of arbitrary attributes
|
46
|
+
end # describe behavior it inherits from OpenStruct
|
47
|
+
|
48
|
+
describe "recursive behavior" do
|
49
|
+
before(:each) do
|
50
|
+
h = { :blah => { :another => 'value' } }
|
51
|
+
@ros = RecursiveOpenStruct.new(h)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "returns accessed hashes as RecursiveOpenStructs instead of hashes" do
|
55
|
+
@ros.blah.another.should == 'value'
|
56
|
+
end
|
57
|
+
|
58
|
+
it "uses #key_as_a_hash to return key as a Hash" do
|
59
|
+
@ros.blah_as_a_hash.should == { :another => 'value' }
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "handling loops in the origin Hashes" do
|
63
|
+
before(:each) do
|
64
|
+
h1 = { :a => 'a'}
|
65
|
+
h2 = { :a => 'b', :h1 => h1 }
|
66
|
+
h1[:h2] = h2
|
67
|
+
@ros = RecursiveOpenStruct.new(h2)
|
68
|
+
end
|
69
|
+
|
70
|
+
it { @ros.h1.a.should == 'a' }
|
71
|
+
it { @ros.h1.h2.a.should == 'b' }
|
72
|
+
it { @ros.h1.h2.h1.a.should == 'a' }
|
73
|
+
it { @ros.h1.h2.h1.h2.a.should == 'b' }
|
74
|
+
it { @ros.h1.should == @ros.h1.h2.h1 }
|
75
|
+
it { @ros.h1.should_not == @ros.h1.h2 }
|
76
|
+
end # describe handling loops in the origin Hashes
|
77
|
+
end # recursive behavior
|
78
|
+
end # describe RecursiveOpenStruct
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'recursive_open_struct'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: recursive-open-struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- William (B.J.) Snow Orvis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-05-20 00:00:00.000000000 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
requirement: &72265070 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *72265070
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: bundler
|
28
|
+
requirement: &72264810 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *72264810
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: jeweler
|
39
|
+
requirement: &72234790 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *72234790
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rcov
|
50
|
+
requirement: &72234520 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *72234520
|
59
|
+
description: ! "RecursiveOpenStruct is a subclass of OpenStruct. It differs from\nOpenStruct
|
60
|
+
in that it allows nested hashes to be treated in a recursive\nfashion. For example:\n\n
|
61
|
+
\ ros = RecursiveOpenStruct.new({ :a => { :b => 'c' } })\n ros.a.b # 'c'\n\nAlso,
|
62
|
+
nested hashes can still be accessed as hashes:\n\n ros.a_as_a_hash # { :b =>
|
63
|
+
'c' }\n"
|
64
|
+
email: aetherknight@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files:
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.rdoc
|
70
|
+
files:
|
71
|
+
- .document
|
72
|
+
- .rspec
|
73
|
+
- Gemfile
|
74
|
+
- Gemfile.lock
|
75
|
+
- LICENSE.txt
|
76
|
+
- README.rdoc
|
77
|
+
- Rakefile
|
78
|
+
- VERSION
|
79
|
+
- lib/recursive_open_struct.rb
|
80
|
+
- spec/recursive_open_struct_spec.rb
|
81
|
+
- spec/spec_helper.rb
|
82
|
+
has_rdoc: true
|
83
|
+
homepage: http://github.com/aetherknight/recursive-open-struct
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
segments:
|
97
|
+
- 0
|
98
|
+
hash: -718088931
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ! '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 1.6.2
|
108
|
+
signing_key:
|
109
|
+
specification_version: 3
|
110
|
+
summary: OpenStruct subclass that returns nested hash attributes as RecursiveOpenStructs
|
111
|
+
test_files: []
|