better_open_struct 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +37 -0
- data/Rakefile +2 -0
- data/better_open_struct.gemspec +16 -0
- data/lib/better_open_struct.rb +35 -0
- metadata +54 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Ryan Closner
|
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,37 @@
|
|
1
|
+
# BetterOpenStruct
|
2
|
+
|
3
|
+
BetterOpenStruct is a subclass of OpenStruct that recursively wraps attribute hashes in an OpenStruct.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'better_open_struct'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install better_open_struct
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
bos = BetterOpenStruct.new(:foo => { :bar => [ {:baz => 'some value'} ] })
|
23
|
+
```
|
24
|
+
|
25
|
+
can now be accessed in the following manner:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
bos.foo.bar.first.baz # 'some value'
|
29
|
+
```
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.authors = ["Ryan Closner"]
|
5
|
+
gem.email = ["ryan@ryanclosner.com"]
|
6
|
+
gem.description = %q{BetterOpenStruct is a subclass of OpenStruct that recursively wraps attribute hashes in an OpenStruct.}
|
7
|
+
gem.summary = %q{BetterOpenStruct is a subclass of OpenStruct that recursively wraps attribute hashes in an OpenStruct.}
|
8
|
+
gem.homepage = "http://github.com/rclosner/better_open_struct"
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "better_open_struct"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = "0.0.1"
|
16
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
|
3
|
+
class BetterOpenStruct < OpenStruct
|
4
|
+
|
5
|
+
def new_ostruct_member(name)
|
6
|
+
name = name.to_sym
|
7
|
+
|
8
|
+
unless self.respond_to?(name)
|
9
|
+
class << self; self; end.class_eval do
|
10
|
+
define_method(name) { typecast(@table[name]) }
|
11
|
+
define_method("#{name}=") { |x| modifiable[name] = x }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
name
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def typecast(value)
|
21
|
+
case value
|
22
|
+
when Hash
|
23
|
+
self.class.new(value)
|
24
|
+
when Array
|
25
|
+
typecast_array(value)
|
26
|
+
else
|
27
|
+
value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def typecast_array(values)
|
32
|
+
values.map {|v| typecast(v) }
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: better_open_struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ryan Closner
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-24 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: BetterOpenStruct is a subclass of OpenStruct that recursively wraps attribute
|
15
|
+
hashes in an OpenStruct.
|
16
|
+
email:
|
17
|
+
- ryan@ryanclosner.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- better_open_struct.gemspec
|
28
|
+
- lib/better_open_struct.rb
|
29
|
+
homepage: http://github.com/rclosner/better_open_struct
|
30
|
+
licenses: []
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 1.8.23
|
50
|
+
signing_key:
|
51
|
+
specification_version: 3
|
52
|
+
summary: BetterOpenStruct is a subclass of OpenStruct that recursively wraps attribute
|
53
|
+
hashes in an OpenStruct.
|
54
|
+
test_files: []
|