super_recursive_open_struct 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8d592a04a8b8f5f8bc0ff957c1b2575c84e9950b
4
+ data.tar.gz: 31e1545862034248f907bb1d9805f4b864cdf845
5
+ SHA512:
6
+ metadata.gz: 798a7bccdb299e99b0a21311cfcda1e5cda760ceca97cf59a5203c74114a7b9fc867a8cbef76d140206ba576ff1bb76c1ad27cb1a7483dbbc056aad0ebbb5222
7
+ data.tar.gz: 837f57307351d32114725d0948cbacf566f5cc5def0897ea9825d28fdc120b445e180e07b53e9752c1725f9aa2f40b6bfd39ea512f1e72617dfe1286f7838d66
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1
4
+ - ruby-head
5
+ - rbx-19mode
6
+ matrix:
7
+ allow_failures:
8
+ - rvm: ruby-head
9
+ - rvm: rbx-19mode
10
+ notifications:
11
+ email:
12
+ on_success: always
13
+ on_failure: always
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in super_recursive_open_struct.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Cédric Félizard
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,32 @@
1
+ # SuperRecursiveOpenStruct
2
+
3
+ [![Build Status](https://travis-ci.org/infertux/super_recursive_open_struct.svg)](https://travis-ci.org/infertux/super_recursive_open_struct)
4
+ [![Dependency Status](http://img.shields.io/gemnasium/infertux/super_recursive_open_struct.svg)](https://gemnasium.com/infertux/super_recursive_open_struct)
5
+ [![Code Climate](http://img.shields.io/codeclimate/github/infertux/super_recursive_open_struct.svg)](https://codeclimate.com/github/infertux/super_recursive_open_struct)
6
+ [![Gem Version](http://img.shields.io/gem/v/super_recursive_open_struct.svg)](https://rubygems.org/gems/super_recursive_open_struct)
7
+
8
+ `SuperRecursiveOpenStruct` is similar to `RecursiveOpenStruct` but with these two differences:
9
+
10
+ - it supports arrays (either at the top level or nested within a hash)
11
+ - it raises `NoMethodError` instead of returning `nil` when accessing an undefined hash key (can be disabled by passing `raise_on_missing_methods: false` to `#initialize`)
12
+
13
+ ## Usage
14
+
15
+ ```ruby
16
+ require "super_recursive_open_struct"
17
+
18
+ sros = SuperRecursiveOpenStruct.new(a: {b: 1})
19
+ sros.a.b #=> 1
20
+ sros.z #=> NoMethodError: undefined method `z' for #<SuperRecursiveOpenStruct:0x489e1e0 a={:b=>1}>
21
+
22
+ sros_with_array = SuperRecursiveOpenStruct.new(a: [1, 2, 3])
23
+ sros_with_array.a #=> [1, 2, 3]
24
+ sros_with_array.a.last #=> 3
25
+
26
+ SuperRecursiveOpenStruct.new({a: 1}, raise_on_missing_methods: false).z #=> nil
27
+ ```
28
+
29
+ ## License
30
+
31
+ MIT
32
+
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |t|
5
+ t.ruby_opts = '-w'
6
+ end
7
+
8
+ task :default => :spec
9
+
@@ -0,0 +1,38 @@
1
+ require "recursive_open_struct"
2
+
3
+ class SuperRecursiveOpenStruct
4
+ def initialize(hash, args = {})
5
+ @raise_on_missing_methods = args.fetch(:raise_on_missing_methods, true)
6
+
7
+ @target = if hash.is_a?(Array)
8
+ hash.map { |item| self.class.new(item) }
9
+ else
10
+ RecursiveOpenStruct.new(hash, recurse_over_arrays: true)
11
+ end
12
+
13
+ if @raise_on_missing_methods
14
+ @target.instance_eval do
15
+ def method_missing(method, *_args)
16
+ raise NoMethodError, "undefined method \`#{method}' for #{self.inspect}"
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ def method_missing(method, *args, &block)
23
+ if !@raise_on_missing_methods || @target.respond_to?(method)
24
+ @target.__send__(method, *args, &block)
25
+ else
26
+ super
27
+ end
28
+ end
29
+
30
+ def respond_to_missing?(method, include_private = false)
31
+ @target.respond_to?(method, include_private)
32
+ end
33
+
34
+ alias_method :to_s, def inspect
35
+ contents = @target.inspect.gsub(/\A#<RecursiveOpenStruct (.+)>\Z/, "\\1")
36
+ "#<SuperRecursiveOpenStruct:0x#{__id__.to_s(16)} #{contents}>".freeze
37
+ end
38
+ end
@@ -0,0 +1,49 @@
1
+ require "super_recursive_open_struct"
2
+ require "rspec"
3
+
4
+ describe SuperRecursiveOpenStruct do
5
+
6
+ describe "#initialize" do
7
+ it "accepts nil" do
8
+ expect { SuperRecursiveOpenStruct.new(nil) }.not_to raise_error
9
+ end
10
+
11
+ it "accepts an empty array" do
12
+ sros = SuperRecursiveOpenStruct.new([])
13
+ expect(sros.size).to eq 0
14
+ end
15
+
16
+ it "does raise on missing methods" do
17
+ sros = SuperRecursiveOpenStruct.new(a: 1)
18
+ expect { sros.baz }.to raise_error(NoMethodError)
19
+ end
20
+
21
+ context "with raise_on_missing_methods = false" do
22
+ let(:args) { { raise_on_missing_methods: false } }
23
+
24
+ it "does NOT raise on missing methods" do
25
+ sros = SuperRecursiveOpenStruct.new({a: 1}, args)
26
+ expect(sros.baz).to be nil
27
+ end
28
+ end
29
+ end
30
+
31
+ describe "#respond_to?" do
32
+ it "is implemented properly with #respond_to_missing?" do
33
+ sros = SuperRecursiveOpenStruct.new(a: 1)
34
+ expect(sros.respond_to?(:a)).to be true
35
+ expect(sros.respond_to?(:b)).to be false
36
+ end
37
+ end
38
+
39
+ describe "#inspect" do
40
+ it "returns the expected output" do
41
+ sros = SuperRecursiveOpenStruct.new(a: { z: 9 }, b: 1)
42
+
43
+ id = sros.object_id.to_s(16)
44
+ string = "#<SuperRecursiveOpenStruct:0x#{id} a={:z=>9}, b=1>"
45
+ expect(sros.inspect).to eq string
46
+ end
47
+ end
48
+
49
+ end
@@ -0,0 +1,25 @@
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 = "super_recursive_open_struct"
7
+ spec.version = "1.0.0"
8
+ spec.authors = ["Cédric Félizard"]
9
+ spec.email = ["cedric@felizard.fr"]
10
+ spec.summary = %q{Like recursive-open-struct but slightly different}
11
+ spec.description = spec.summary
12
+ spec.homepage = "https://github.com/infertux/super_recursive_open_struct"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
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_dependency "recursive-open-struct", ">= 0.5.0"
21
+
22
+ spec.add_development_dependency "bundler", ">= 1.6"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", ">= 3"
25
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: super_recursive_open_struct
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Cédric Félizard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: recursive-open-struct
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.5.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '3'
69
+ description: Like recursive-open-struct but slightly different
70
+ email:
71
+ - cedric@felizard.fr
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/super_recursive_open_struct.rb
83
+ - spec/lib/super_recursive_open_struct_spec.rb
84
+ - super_recursive_open_struct.gemspec
85
+ homepage: https://github.com/infertux/super_recursive_open_struct
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Like recursive-open-struct but slightly different
109
+ test_files:
110
+ - spec/lib/super_recursive_open_struct_spec.rb