recursive-open-struct 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec CHANGED
@@ -1 +1,2 @@
1
1
  --color
2
+ --drb
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
- source "http://rubygems.org"
1
+ source 'http://rubygems.org'
2
+
2
3
  # Add dependencies required to use your gem here.
3
4
  # Example:
4
5
  # gem "activesupport", ">= 2.3.5"
@@ -6,8 +7,8 @@ source "http://rubygems.org"
6
7
  # Add dependencies to develop your gem here.
7
8
  # Include everything needed to run rake, tests, features, etc.
8
9
  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"
10
+ gem 'rspec' #, "~> 2.3.0"
11
+ gem 'bundler' #, "~> 1.0.0"
12
+ gem 'jeweler' #, "~> 1.6.0"
13
+ gem 'rcov' #, ">= 0"
13
14
  end
data/Gemfile.lock CHANGED
@@ -7,13 +7,13 @@ GEM
7
7
  bundler (~> 1.0.0)
8
8
  git (>= 1.2.5)
9
9
  rake
10
- rake (0.8.7)
10
+ rake (0.9.0)
11
11
  rcov (0.9.9)
12
12
  rspec (2.6.0)
13
13
  rspec-core (~> 2.6.0)
14
14
  rspec-expectations (~> 2.6.0)
15
15
  rspec-mocks (~> 2.6.0)
16
- rspec-core (2.6.1)
16
+ rspec-core (2.6.2)
17
17
  rspec-expectations (2.6.0)
18
18
  diff-lcs (~> 1.1.2)
19
19
  rspec-mocks (2.6.0)
data/README.rdoc CHANGED
@@ -1,7 +1,7 @@
1
1
  = recursive-open-struct
2
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
3
+ RecursiveOpenStruct is a extension of OpenStruct.
4
+ It enhance OpenStruct in that it allows nested hashes to be treated in a recursive
5
5
  fashion. For example:
6
6
 
7
7
  ros = RecursiveOpenStruct.new({ :a => { :b => 'c' } })
@@ -11,6 +11,16 @@ Also, nested hashes can still be accessed as hashes:
11
11
 
12
12
  ros.a_as_a_hash # { :b => 'c' }
13
13
 
14
+ == Installation
15
+
16
+ So easy.
17
+
18
+ If you use bundler, just throw that in your gemfile :
19
+ gem 'recursive-open-struct'
20
+
21
+ You may also install the gem manually :
22
+ gem install recursive-open-struct
23
+
14
24
  == Note on Patches/Pull Requests
15
25
 
16
26
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -1,6 +1,7 @@
1
1
  require 'ostruct'
2
2
 
3
3
  class RecursiveOpenStruct < OpenStruct
4
+
4
5
  def new_ostruct_member(name)
5
6
  name = name.to_sym
6
7
  unless self.respond_to?(name)
@@ -15,4 +16,34 @@ class RecursiveOpenStruct < OpenStruct
15
16
  end
16
17
  name
17
18
  end
19
+
20
+ def debug_inspect(indent_level = 0, recursion_limit = 12)
21
+ display_recursive_open_struct(@table, indent_level, recursion_limit)
22
+ end
23
+
24
+ def display_recursive_open_struct(ostrct_or_hash, indent_level, recursion_limit)
25
+
26
+ if recursion_limit <= 0 then
27
+ # protection against recursive structure (like in the tests)
28
+ puts ' '*indent_level + '(recursion limit reached)'
29
+ else
30
+ #puts ostrct_or_hash.inspect
31
+ if ostrct_or_hash.is_a?(RecursiveOpenStruct) then
32
+ ostrct_or_hash = ostrct_or_hash.marshal_dump
33
+ end
34
+
35
+ data_indent = ostrct_or_hash.max {|a,b| a[0].to_s.length <=> b[0].to_s.length}[0].length
36
+ ostrct_or_hash.each do |key, value|
37
+ if (value.is_a?(RecursiveOpenStruct) || value.is_a?(Hash)) then
38
+ puts ' '*indent_level + key.to_s + '.'
39
+ display_recursive_open_struct(value, indent_level + 1, recursion_limit - 1)
40
+ else
41
+ puts ' '*indent_level + key.to_s + ' '*(data_indent - key.to_s.length) + '= ' + value.inspect
42
+ end
43
+ end
44
+ end
45
+
46
+ true
47
+ end
48
+
18
49
  end
@@ -0,0 +1,70 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{recursive-open-struct}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = [%q{William (B.J.) Snow Orvis}]
12
+ s.date = %q{2011-05-25}
13
+ s.description = %q{RecursiveOpenStruct is a subclass of OpenStruct. It differs from
14
+ OpenStruct in that it allows nested hashes to be treated in a recursive
15
+ fashion. For example:
16
+
17
+ ros = RecursiveOpenStruct.new({ :a => { :b => 'c' } })
18
+ ros.a.b # 'c'
19
+
20
+ Also, nested hashes can still be accessed as hashes:
21
+
22
+ ros.a_as_a_hash # { :b => 'c' }
23
+ }
24
+ s.email = %q{aetherknight@gmail.com}
25
+ s.extra_rdoc_files = [
26
+ "LICENSE.txt",
27
+ "README.rdoc"
28
+ ]
29
+ s.files = [
30
+ ".document",
31
+ ".rspec",
32
+ "Gemfile",
33
+ "Gemfile.lock",
34
+ "LICENSE.txt",
35
+ "README.rdoc",
36
+ "Rakefile",
37
+ "VERSION",
38
+ "lib/recursive_open_struct.rb",
39
+ "recursive-open-struct.gemspec",
40
+ "spec/recursive_open_struct_spec.rb",
41
+ "spec/spec_helper.rb"
42
+ ]
43
+ s.homepage = %q{http://github.com/aetherknight/recursive-open-struct}
44
+ s.licenses = [%q{MIT}]
45
+ s.require_paths = [%q{lib}]
46
+ s.rubygems_version = %q{1.8.3}
47
+ s.summary = %q{OpenStruct subclass that returns nested hash attributes as RecursiveOpenStructs}
48
+
49
+ if s.respond_to? :specification_version then
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
+ s.add_development_dependency(%q<rspec>, [">= 0"])
54
+ s.add_development_dependency(%q<bundler>, [">= 0"])
55
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
56
+ s.add_development_dependency(%q<rcov>, [">= 0"])
57
+ else
58
+ s.add_dependency(%q<rspec>, [">= 0"])
59
+ s.add_dependency(%q<bundler>, [">= 0"])
60
+ s.add_dependency(%q<jeweler>, [">= 0"])
61
+ s.add_dependency(%q<rcov>, [">= 0"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<rspec>, [">= 0"])
65
+ s.add_dependency(%q<bundler>, [">= 0"])
66
+ s.add_dependency(%q<jeweler>, [">= 0"])
67
+ s.add_dependency(%q<rcov>, [">= 0"])
68
+ end
69
+ end
70
+
@@ -2,40 +2,41 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
  require 'recursive_open_struct'
3
3
 
4
4
  describe RecursiveOpenStruct do
5
+
5
6
  describe "behavior it inherits from OpenStruct" do
6
-
7
+
7
8
  it "can represent arbitrary data objects" do
8
9
  ros = RecursiveOpenStruct.new
9
10
  ros.blah = "John Smith"
10
11
  ros.blah.should == "John Smith"
11
12
  end
12
-
13
+
13
14
  it "can be created from a hash" do
14
15
  h = { :asdf => 'John Smith' }
15
16
  ros = RecursiveOpenStruct.new(h)
16
17
  ros.asdf.should == "John Smith"
17
18
  end
18
-
19
+
19
20
  it "can modify an existing key" do
20
21
  h = { :blah => 'John Smith' }
21
22
  ros = RecursiveOpenStruct.new(h)
22
23
  ros.blah = "George Washington"
23
24
  ros.blah.should == "George Washington"
24
25
  end
25
-
26
+
26
27
  describe "handling of arbitrary attributes" do
27
28
  before(:each) do
28
29
  @ros = RecursiveOpenStruct.new
29
30
  @ros.blah = "John Smith"
30
31
  end
31
-
32
+
32
33
  describe "#respond?" do
33
34
  it { @ros.should respond_to :blah }
34
35
  it { @ros.should respond_to :blah= }
35
36
  it { @ros.should_not respond_to :asdf }
36
37
  it { @ros.should_not respond_to :asdf= }
37
38
  end # describe #respond?
38
-
39
+
39
40
  describe "#methods" do
40
41
  it { @ros.methods.should include :blah }
41
42
  it { @ros.methods.should include :blah= }
@@ -50,15 +51,15 @@ describe RecursiveOpenStruct do
50
51
  h = { :blah => { :another => 'value' } }
51
52
  @ros = RecursiveOpenStruct.new(h)
52
53
  end
53
-
54
+
54
55
  it "returns accessed hashes as RecursiveOpenStructs instead of hashes" do
55
56
  @ros.blah.another.should == 'value'
56
57
  end
57
-
58
+
58
59
  it "uses #key_as_a_hash to return key as a Hash" do
59
60
  @ros.blah_as_a_hash.should == { :another => 'value' }
60
61
  end
61
-
62
+
62
63
  describe "handling loops in the origin Hashes" do
63
64
  before(:each) do
64
65
  h1 = { :a => 'a'}
@@ -66,7 +67,7 @@ describe RecursiveOpenStruct do
66
67
  h1[:h2] = h2
67
68
  @ros = RecursiveOpenStruct.new(h2)
68
69
  end
69
-
70
+
70
71
  it { @ros.h1.a.should == 'a' }
71
72
  it { @ros.h1.h2.a.should == 'b' }
72
73
  it { @ros.h1.h2.h1.a.should == 'a' }
@@ -75,4 +76,19 @@ describe RecursiveOpenStruct do
75
76
  it { @ros.h1.should_not == @ros.h1.h2 }
76
77
  end # describe handling loops in the origin Hashes
77
78
  end # recursive behavior
79
+
80
+ describe "additionnel features" do
81
+
82
+ before(:each) do
83
+ h1 = { :a => 'a'}
84
+ h2 = { :a => 'b', :h1 => h1 }
85
+ h1[:h2] = h2
86
+ @ros = RecursiveOpenStruct.new(h2)
87
+ end
88
+
89
+ it "should have a simple way of display" do
90
+ @ros.debug_inspect
91
+ end
92
+ end # additionnel features
93
+
78
94
  end # describe RecursiveOpenStruct
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
  require 'rspec'
4
- require 'recursive_open_struct'
5
4
 
6
5
  # Requires supporting files with custom matchers and macros, etc,
7
6
  # in ./support/ and its subdirectories.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: recursive-open-struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-05-20 00:00:00.000000000 +02:00
13
- default_executable:
12
+ date: 2011-05-25 00:00:00.000000000Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: rspec
17
- requirement: &72265070 !ruby/object:Gem::Requirement
16
+ requirement: &74135210 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ! '>='
@@ -22,10 +21,10 @@ dependencies:
22
21
  version: '0'
23
22
  type: :development
24
23
  prerelease: false
25
- version_requirements: *72265070
24
+ version_requirements: *74135210
26
25
  - !ruby/object:Gem::Dependency
27
26
  name: bundler
28
- requirement: &72264810 !ruby/object:Gem::Requirement
27
+ requirement: &74134960 !ruby/object:Gem::Requirement
29
28
  none: false
30
29
  requirements:
31
30
  - - ! '>='
@@ -33,10 +32,10 @@ dependencies:
33
32
  version: '0'
34
33
  type: :development
35
34
  prerelease: false
36
- version_requirements: *72264810
35
+ version_requirements: *74134960
37
36
  - !ruby/object:Gem::Dependency
38
37
  name: jeweler
39
- requirement: &72234790 !ruby/object:Gem::Requirement
38
+ requirement: &74134720 !ruby/object:Gem::Requirement
40
39
  none: false
41
40
  requirements:
42
41
  - - ! '>='
@@ -44,10 +43,10 @@ dependencies:
44
43
  version: '0'
45
44
  type: :development
46
45
  prerelease: false
47
- version_requirements: *72234790
46
+ version_requirements: *74134720
48
47
  - !ruby/object:Gem::Dependency
49
48
  name: rcov
50
- requirement: &72234520 !ruby/object:Gem::Requirement
49
+ requirement: &74134480 !ruby/object:Gem::Requirement
51
50
  none: false
52
51
  requirements:
53
52
  - - ! '>='
@@ -55,7 +54,7 @@ dependencies:
55
54
  version: '0'
56
55
  type: :development
57
56
  prerelease: false
58
- version_requirements: *72234520
57
+ version_requirements: *74134480
59
58
  description: ! "RecursiveOpenStruct is a subclass of OpenStruct. It differs from\nOpenStruct
60
59
  in that it allows nested hashes to be treated in a recursive\nfashion. For example:\n\n
61
60
  \ ros = RecursiveOpenStruct.new({ :a => { :b => 'c' } })\n ros.a.b # 'c'\n\nAlso,
@@ -77,9 +76,9 @@ files:
77
76
  - Rakefile
78
77
  - VERSION
79
78
  - lib/recursive_open_struct.rb
79
+ - recursive-open-struct.gemspec
80
80
  - spec/recursive_open_struct_spec.rb
81
81
  - spec/spec_helper.rb
82
- has_rdoc: true
83
82
  homepage: http://github.com/aetherknight/recursive-open-struct
84
83
  licenses:
85
84
  - MIT
@@ -95,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
94
  version: '0'
96
95
  segments:
97
96
  - 0
98
- hash: -718088931
97
+ hash: 914381613
99
98
  required_rubygems_version: !ruby/object:Gem::Requirement
100
99
  none: false
101
100
  requirements:
@@ -104,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
103
  version: '0'
105
104
  requirements: []
106
105
  rubyforge_project:
107
- rubygems_version: 1.6.2
106
+ rubygems_version: 1.8.3
108
107
  signing_key:
109
108
  specification_version: 3
110
109
  summary: OpenStruct subclass that returns nested hash attributes as RecursiveOpenStructs