recursive-struct 1.0.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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +66 -0
- data/Rakefile +1 -0
- data/lib/recursive-struct.rb +76 -0
- data/recursive-struct.gemspec +22 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2a92fbdc388f60e4064c7267d6a887c6d0c255b8
|
4
|
+
data.tar.gz: 1f50574b06297b43a6b34eeae8b42c34178a00c5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d87fa52ea29f5a600f9d482e896139f9636332170a3728a76b39fb74058001ac9966b42dfce4c189ce577356c3b8c93ac75b08e34c0c5959f223371dee0c5f9d
|
7
|
+
data.tar.gz: c7b492cac6d6f469d030f79f42e85d95c6aeea14cfd0811179de01843620d8bfeee73f88c191bdba4bbe8931c4aff4618b14cb167fbd4f9cc5f6e014ac80ed41
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Charles Chamberlain
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# Recursive-Struct
|
2
|
+
|
3
|
+
##### Perfect Open Structs.
|
4
|
+
|
5
|
+
## Examples
|
6
|
+
|
7
|
+
Setting a Recursive Struct is easy:
|
8
|
+
|
9
|
+
```
|
10
|
+
lord_trumpet = RecursiveStruct.new
|
11
|
+
lord_trumpet.species = 'dog'
|
12
|
+
lord_trumpet.fur.color = 'black'
|
13
|
+
lord_trumpet.owner.hair.color = 'brown'
|
14
|
+
```
|
15
|
+
|
16
|
+
Getting one is easier:
|
17
|
+
|
18
|
+
```
|
19
|
+
lord_trumpet.species # => "dog"
|
20
|
+
lord_trumpet.fur.color # => "black"
|
21
|
+
lord_trumpet.owner.hair.color # => "brown"
|
22
|
+
```
|
23
|
+
|
24
|
+
Note: You _cannot_ assign a value to an intermediate method.
|
25
|
+
```
|
26
|
+
lord_trumpet.owner # => #<RecursiveStruct>
|
27
|
+
lord_trumpet.owner = 'Paul'
|
28
|
+
lord_trumpet.owner # => #<RecursiveStruct>
|
29
|
+
```
|
30
|
+
|
31
|
+
|
32
|
+
#### Initialize with a Hash
|
33
|
+
|
34
|
+
```
|
35
|
+
jet_plane = RecursiveStruct.new seats: 24, passengers: { first: 'joe', second: 'bob' ... }
|
36
|
+
|
37
|
+
jet_plane.seats # => 24
|
38
|
+
jet_plane.passengers.first # => 'joe'
|
39
|
+
```
|
40
|
+
|
41
|
+
|
42
|
+
## Installation
|
43
|
+
|
44
|
+
Add this line to your application's Gemfile:
|
45
|
+
|
46
|
+
gem 'recursive-struct'
|
47
|
+
|
48
|
+
And then execute:
|
49
|
+
|
50
|
+
$ bundle
|
51
|
+
|
52
|
+
Or install it yourself as:
|
53
|
+
|
54
|
+
$ gem install recursive-struct
|
55
|
+
|
56
|
+
## Usage
|
57
|
+
|
58
|
+
TODO: Write usage instructions here
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
1. Fork it
|
63
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
64
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
65
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
66
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
class RecursiveStruct < OpenStruct
|
4
|
+
|
5
|
+
def initialize(input = nil)
|
6
|
+
@recursive_table = {}
|
7
|
+
hash = input
|
8
|
+
if hash
|
9
|
+
hash.each do |key, value|
|
10
|
+
if value.class == Hash
|
11
|
+
@recursive_table[key] = RecursiveStruct.new(value)
|
12
|
+
hash.delete(key)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
super(hash)
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_ary
|
20
|
+
self.to_h.to_a
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
# def new_ostruct_member(name)
|
25
|
+
# name = name.to_sym
|
26
|
+
# unless respond_to?(name)
|
27
|
+
# if name.nil?
|
28
|
+
# define_singleton_method(name) { @recursive_table[name] }
|
29
|
+
# else
|
30
|
+
# yield(name) if block_given?
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
# name
|
34
|
+
# end
|
35
|
+
|
36
|
+
def method_missing(mid, *args)
|
37
|
+
mname = mid.id2name
|
38
|
+
len = args.length
|
39
|
+
if mname.chomp!('=')
|
40
|
+
super(mid, *args)
|
41
|
+
elsif len == 0
|
42
|
+
return @recursive_table[mid] if @recursive_table[mid]
|
43
|
+
return @table[mid] if @table[mid]
|
44
|
+
@recursive_table[mid] = RecursiveStruct.new
|
45
|
+
else
|
46
|
+
raise NoMethodError, "undefined method `#{mid}' for #{self}", caller(1)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
r = RecursiveStruct.new
|
53
|
+
|
54
|
+
r.time = 'now'
|
55
|
+
|
56
|
+
puts r.time
|
57
|
+
|
58
|
+
r.dog.breed = 'poodle'
|
59
|
+
|
60
|
+
puts r.dog.breed
|
61
|
+
|
62
|
+
r.wow.hi.great.golly = 'nope'
|
63
|
+
|
64
|
+
puts r.wow.hi.great.golly
|
65
|
+
|
66
|
+
hash = {cat: 'none', dog: { color: 'black', breed: 'poodle' }}
|
67
|
+
|
68
|
+
i = RecursiveStruct.new hash
|
69
|
+
|
70
|
+
puts i.cat
|
71
|
+
|
72
|
+
puts i.dog
|
73
|
+
|
74
|
+
puts i.dog.color
|
75
|
+
|
76
|
+
puts i.dog.breed
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "recursive-struct"
|
7
|
+
spec.version = "1.0.0"
|
8
|
+
spec.authors = ["Charles Chamberlain"]
|
9
|
+
spec.email = ["charles@charlesetc.com"]
|
10
|
+
spec.description = %q{Recursive-Struct allows for inifinite Open Struct chains.}
|
11
|
+
spec.summary = %q{Perfect Open Structs.}
|
12
|
+
spec.homepage = "https://github.com/Charlesetc/recursive-struct"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
# spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
# spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: recursive-struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Charles Chamberlain
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Recursive-Struct allows for inifinite Open Struct chains.
|
42
|
+
email:
|
43
|
+
- charles@charlesetc.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/recursive-struct.rb
|
54
|
+
- recursive-struct.gemspec
|
55
|
+
homepage: https://github.com/Charlesetc/recursive-struct
|
56
|
+
licenses:
|
57
|
+
- MIT
|
58
|
+
metadata: {}
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 2.0.5
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: Perfect Open Structs.
|
79
|
+
test_files: []
|
80
|
+
has_rdoc:
|