structure 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/README.md +25 -0
- data/lib/structure.rb +43 -2
- data/structure.gemspec +3 -6
- data/test/structure_test.rb +46 -0
- metadata +10 -17
- data/lib/structure/version.rb +0 -3
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Structure
|
2
|
+
=========
|
3
|
+
|
4
|
+
Structure is a nested OpenStruct implementation. Or, recursively put, Structure is a truly OpenStruct OpenStruct.
|
5
|
+
|
6
|
+
require 'structure'
|
7
|
+
|
8
|
+
source = {
|
9
|
+
:title => 'Mille Plateaux',
|
10
|
+
:authors => [
|
11
|
+
{
|
12
|
+
:name => "Deleuze",
|
13
|
+
}
|
14
|
+
],
|
15
|
+
:publisher => {
|
16
|
+
:name => "Minuit",
|
17
|
+
}
|
18
|
+
|
19
|
+
book = Structure.new(source)
|
20
|
+
|
21
|
+
puts book.authors.first.name
|
22
|
+
=> "Gilles Deleuze"
|
23
|
+
|
24
|
+
puts book.publisher.name
|
25
|
+
=> "Minuit"
|
data/lib/structure.rb
CHANGED
@@ -1,3 +1,44 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
#
|
4
|
+
# Structure is a nested OpenStruct implementation.
|
5
|
+
#
|
6
|
+
class Structure < OpenStruct
|
7
|
+
def initialize(source = {})
|
8
|
+
@table = {}
|
9
|
+
source.each do |k, v|
|
10
|
+
@table[k.to_sym] = structure(v)
|
11
|
+
new_ostruct_member(k)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def method_missing(mid, *args) # :nodoc:
|
16
|
+
mname = mid.id2name
|
17
|
+
len = args.length
|
18
|
+
if mname.chomp!('=')
|
19
|
+
if len != 1
|
20
|
+
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
|
21
|
+
end
|
22
|
+
modifiable[new_ostruct_member(mname)] = structure(args[0])
|
23
|
+
elsif len == 0
|
24
|
+
@table[mid]
|
25
|
+
else
|
26
|
+
raise NoMethodError, "undefined method `#{mname}' for #{self}", caller(1)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
alias :to_hash :marshal_dump
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def structure(o)
|
35
|
+
case o
|
36
|
+
when Hash
|
37
|
+
self.class.new(o)
|
38
|
+
when Array
|
39
|
+
o.map { |o| structure(o) }
|
40
|
+
else
|
41
|
+
o
|
42
|
+
end
|
43
|
+
end
|
3
44
|
end
|
data/structure.gemspec
CHANGED
@@ -1,16 +1,13 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
$:.push File.expand_path("../lib", __FILE__)
|
3
|
-
require "structure/version"
|
4
|
-
|
5
2
|
Gem::Specification.new do |s|
|
6
3
|
s.name = "structure"
|
7
|
-
s.version =
|
4
|
+
s.version = "0.1.0"
|
8
5
|
s.platform = Gem::Platform::RUBY
|
9
6
|
s.authors = ["Paper Cavalier"]
|
10
7
|
s.email = ["code@papercavalier.com"]
|
11
8
|
s.homepage = ""
|
12
|
-
s.summary = %q{
|
13
|
-
s.description = %q{
|
9
|
+
s.summary = %q{Structure is a nested OpenStruct implementation.}
|
10
|
+
s.description = %q{A nested OpenStruct implementation}
|
14
11
|
|
15
12
|
s.rubyforge_project = "structure"
|
16
13
|
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'minitest/spec'
|
2
|
+
require File.expand_path('../../lib/structure', __FILE__)
|
3
|
+
|
4
|
+
MiniTest::Unit.autorun
|
5
|
+
|
6
|
+
describe Structure do
|
7
|
+
before do
|
8
|
+
@hash = {
|
9
|
+
:name => "John",
|
10
|
+
:children => [ { :name => "Jim" } ],
|
11
|
+
:location => { :city => { :name => "London" } }
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
describe ".new" do
|
16
|
+
before do
|
17
|
+
@person = Structure.new(@hash)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "structures nested hashes" do
|
21
|
+
@person.location.city.name.must_equal "London"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "structures hashes in arrays" do
|
25
|
+
@person.children.must_be_instance_of Array
|
26
|
+
@person.children.first.name.must_equal "Jim"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#=" do
|
31
|
+
before do
|
32
|
+
@person = Structure.new
|
33
|
+
end
|
34
|
+
|
35
|
+
it "structures nested hashes" do
|
36
|
+
@person.location = { :city => { :name => "London" } }
|
37
|
+
@person.location.city.name.must_equal "London"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "structures hashes in arrays" do
|
41
|
+
@person.children = [ { :name => "Jim" } ]
|
42
|
+
@person.children.must_be_instance_of Array
|
43
|
+
@person.children.first.name.must_equal "Jim"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: structure
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
version: 0.0.1
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Paper Cavalier
|
@@ -14,11 +10,11 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date:
|
13
|
+
date: 2011-02-09 00:00:00 +00:00
|
18
14
|
default_executable:
|
19
15
|
dependencies: []
|
20
16
|
|
21
|
-
description:
|
17
|
+
description: A nested OpenStruct implementation
|
22
18
|
email:
|
23
19
|
- code@papercavalier.com
|
24
20
|
executables: []
|
@@ -30,10 +26,11 @@ extra_rdoc_files: []
|
|
30
26
|
files:
|
31
27
|
- .gitignore
|
32
28
|
- Gemfile
|
29
|
+
- README.md
|
33
30
|
- Rakefile
|
34
31
|
- lib/structure.rb
|
35
|
-
- lib/structure/version.rb
|
36
32
|
- structure.gemspec
|
33
|
+
- test/structure_test.rb
|
37
34
|
has_rdoc: true
|
38
35
|
homepage: ""
|
39
36
|
licenses: []
|
@@ -48,23 +45,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
48
45
|
requirements:
|
49
46
|
- - ">="
|
50
47
|
- !ruby/object:Gem::Version
|
51
|
-
segments:
|
52
|
-
- 0
|
53
48
|
version: "0"
|
54
49
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
50
|
none: false
|
56
51
|
requirements:
|
57
52
|
- - ">="
|
58
53
|
- !ruby/object:Gem::Version
|
59
|
-
segments:
|
60
|
-
- 0
|
61
54
|
version: "0"
|
62
55
|
requirements: []
|
63
56
|
|
64
57
|
rubyforge_project: structure
|
65
|
-
rubygems_version: 1.
|
58
|
+
rubygems_version: 1.5.0
|
66
59
|
signing_key:
|
67
60
|
specification_version: 3
|
68
|
-
summary:
|
69
|
-
test_files:
|
70
|
-
|
61
|
+
summary: Structure is a nested OpenStruct implementation.
|
62
|
+
test_files:
|
63
|
+
- test/structure_test.rb
|
data/lib/structure/version.rb
DELETED