backport_dig 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Manifest.txt +3 -0
- data/README.md +68 -0
- data/Rakefile +19 -0
- data/ext/backport_dig/backport_dig.c +136 -0
- data/ext/backport_dig/extconf.rb +42 -0
- data/lib/backport_dig.rb +1 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1a4dde83bd36c3f7fdfd4e389d0b2484277d3bb5
|
4
|
+
data.tar.gz: d3a7ff89a6858e0a2b716ac2fd6f3bd87d0d43b5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3f111c6a90ab86cb36164d8b5e28c814d31900f468e4d02b505d6960eafe9610142011741022bd64e33eea93e6f3ae863f6ad3b841519da14df88a76372d15db
|
7
|
+
data.tar.gz: 9ebb36d8b31583a19d22726b3c716076f322c0443e260f13f42ed28ec3eac554fc990afe98382884829bb7785e0d94d2bfe212ad21cd54048479329face18f1a
|
data/Manifest.txt
ADDED
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# backport_dig [![Build Status](https://travis-ci.org/koic/backport_dig.svg)](https://travis-ci.org/koic/backport_dig)
|
2
|
+
|
3
|
+
backport_dig is the backport of Hash#dig and Array#dig in Ruby 2.3.0 to older Ruby versions.
|
4
|
+
|
5
|
+
The best way is to use Ruby 2.3.0 or later. Because you can use original dig methods and so on.
|
6
|
+
|
7
|
+
## Ruby 2.3 incompatible
|
8
|
+
|
9
|
+
* Struct#dig is not supported
|
10
|
+
|
11
|
+
## Synopsis
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
h = {foo: {bar: {baz: 1}}}
|
15
|
+
h.dig(:foo, :bar, :baz) #=> 1
|
16
|
+
|
17
|
+
a = [[1, [2, 3]]]
|
18
|
+
a.dig(0, 1, 1) #=> 3
|
19
|
+
```
|
20
|
+
|
21
|
+
## Supported Versions
|
22
|
+
|
23
|
+
* Ruby 2.0
|
24
|
+
* Ruby 2.1
|
25
|
+
* Ruby 2.2
|
26
|
+
|
27
|
+
## INSTALL
|
28
|
+
|
29
|
+
Add these lines to your application's Gemfile:
|
30
|
+
|
31
|
+
```
|
32
|
+
gem 'backport_dig'
|
33
|
+
```
|
34
|
+
|
35
|
+
And then execute:
|
36
|
+
|
37
|
+
```
|
38
|
+
$ bundle
|
39
|
+
```
|
40
|
+
|
41
|
+
Or install it yourself as:
|
42
|
+
|
43
|
+
```
|
44
|
+
$ gem install backport_dig
|
45
|
+
```
|
46
|
+
|
47
|
+
And require it as:
|
48
|
+
|
49
|
+
```
|
50
|
+
require 'backport_dig'
|
51
|
+
```
|
52
|
+
|
53
|
+
## Special Thanks
|
54
|
+
|
55
|
+
* Ruby Development Team
|
56
|
+
* Pay homage to [ruby_dig](https://rubygems.org/gems/ruby_dig)
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
1. Fork it
|
61
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
62
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
63
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
64
|
+
5. Create new Pull Request
|
65
|
+
|
66
|
+
## License
|
67
|
+
|
68
|
+
backport_dig is released under the [MIT License](http://www.opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
require 'hoe'
|
4
|
+
gem 'rake-compiler'
|
5
|
+
require 'rake/extensiontask'
|
6
|
+
|
7
|
+
name = 'backport_dig'
|
8
|
+
|
9
|
+
Hoe.plugin :gemspec
|
10
|
+
|
11
|
+
HOE = Hoe.spec name do
|
12
|
+
VERSION = BackportDig::VERSION
|
13
|
+
developer('Koichi ITO', 'koic.ito@gmail.com')
|
14
|
+
license 'MIT'
|
15
|
+
end
|
16
|
+
|
17
|
+
RET = Rake::ExtensionTask.new(name, HOE.spec) do |ext|
|
18
|
+
ext.lib_dir = File.join('lib', name)
|
19
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include <ruby/intern.h>
|
3
|
+
|
4
|
+
// This RBASIC_CLASS definition is for Ruby 2.0
|
5
|
+
#ifndef RBASIC_CLASS
|
6
|
+
#define RBASIC_CLASS(obj) (RBASIC(obj)->klass)
|
7
|
+
#endif
|
8
|
+
|
9
|
+
struct dig_method {
|
10
|
+
VALUE klass;
|
11
|
+
int basic;
|
12
|
+
};
|
13
|
+
|
14
|
+
static ID id_dig;
|
15
|
+
|
16
|
+
static int
|
17
|
+
dig_basic_p(VALUE obj, struct dig_method *cache)
|
18
|
+
{
|
19
|
+
VALUE klass = RBASIC_CLASS(obj);
|
20
|
+
if (klass != cache->klass) {
|
21
|
+
cache->klass = klass;
|
22
|
+
cache->basic = rb_method_basic_definition_p(klass, id_dig);
|
23
|
+
}
|
24
|
+
return cache->basic;
|
25
|
+
}
|
26
|
+
|
27
|
+
static void
|
28
|
+
no_dig_method(int found, VALUE data)
|
29
|
+
{
|
30
|
+
if (!found) {
|
31
|
+
rb_raise(rb_eTypeError, "%"PRIsVALUE" does not have #dig method", CLASS_OF(data));
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
VALUE
|
36
|
+
rb_ary_at(VALUE ary, VALUE pos)
|
37
|
+
{
|
38
|
+
return rb_ary_entry(ary, NUM2LONG(pos));
|
39
|
+
}
|
40
|
+
|
41
|
+
VALUE
|
42
|
+
rb_obj_dig(int argc, VALUE *argv, VALUE obj, VALUE notfound)
|
43
|
+
{
|
44
|
+
struct dig_method hash = {Qnil}, ary = {Qnil};
|
45
|
+
|
46
|
+
for (; argc > 0; ++argv, --argc) {
|
47
|
+
if (NIL_P(obj)) return notfound;
|
48
|
+
if (!SPECIAL_CONST_P(obj)) {
|
49
|
+
switch (BUILTIN_TYPE(obj)) {
|
50
|
+
case T_HASH:
|
51
|
+
if (dig_basic_p(obj, &hash)) {
|
52
|
+
obj = rb_hash_aref(obj, *argv);
|
53
|
+
continue;
|
54
|
+
}
|
55
|
+
break;
|
56
|
+
case T_ARRAY:
|
57
|
+
if (dig_basic_p(obj, &ary)) {
|
58
|
+
obj = rb_ary_at(obj, *argv);
|
59
|
+
continue;
|
60
|
+
}
|
61
|
+
break;
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
int respond = rb_respond_to(obj, id_dig);
|
66
|
+
no_dig_method(respond, obj);
|
67
|
+
|
68
|
+
return rb_check_funcall(obj, id_dig, argc, argv);
|
69
|
+
}
|
70
|
+
return obj;
|
71
|
+
}
|
72
|
+
|
73
|
+
/*
|
74
|
+
* call-seq:
|
75
|
+
* hsh.dig(key, ...) -> object
|
76
|
+
*
|
77
|
+
* Extracts the nested value specified by the sequence of <i>idx</i>
|
78
|
+
* objects by calling +dig+ at each step, returning +nil+ if any
|
79
|
+
* intermediate step is +nil+.
|
80
|
+
*
|
81
|
+
* h = { foo: {bar: {baz: 1}}}
|
82
|
+
*
|
83
|
+
* h.dig(:foo, :bar, :baz) #=> 1
|
84
|
+
* h.dig(:foo, :zot, :xyz) #=> nil
|
85
|
+
*
|
86
|
+
* g = { foo: [10, 11, 12] }
|
87
|
+
* g.dig(:foo, 1) #=> 11
|
88
|
+
*/
|
89
|
+
VALUE
|
90
|
+
rb_hash_dig(int argc, VALUE *argv, VALUE self)
|
91
|
+
{
|
92
|
+
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
|
93
|
+
self = rb_hash_aref(self, *argv);
|
94
|
+
if (!--argc) return self;
|
95
|
+
++argv;
|
96
|
+
return rb_obj_dig(argc, argv, self, Qnil);
|
97
|
+
}
|
98
|
+
|
99
|
+
/*
|
100
|
+
* call-seq:
|
101
|
+
* ary.dig(idx, ...) -> object
|
102
|
+
*
|
103
|
+
* Extracts the nested value specified by the sequence of <i>idx</i>
|
104
|
+
* objects by calling +dig+ at each step, returning +nil+ if any
|
105
|
+
* intermediate step is +nil+.
|
106
|
+
*
|
107
|
+
* a = [[1, [2, 3]]]
|
108
|
+
*
|
109
|
+
* a.dig(0, 1, 1) #=> 3
|
110
|
+
* a.dig(1, 2, 3) #=> nil
|
111
|
+
* a.dig(0, 0, 0) #=> TypeError: Fixnum does not have #dig method
|
112
|
+
* [42, {foo: :bar}].dig(1, :foo) #=> :bar
|
113
|
+
*/
|
114
|
+
VALUE
|
115
|
+
rb_ary_dig(int argc, VALUE *argv, VALUE self)
|
116
|
+
{
|
117
|
+
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
|
118
|
+
self = rb_ary_at(self, *argv);
|
119
|
+
if (!--argc) return self;
|
120
|
+
++argv;
|
121
|
+
return rb_obj_dig(argc, argv, self, Qnil);
|
122
|
+
}
|
123
|
+
|
124
|
+
void
|
125
|
+
Init_backport_dig(void)
|
126
|
+
{
|
127
|
+
#undef rb_intern
|
128
|
+
#define rb_intern(str) rb_intern_const(str)
|
129
|
+
id_dig = rb_intern_const("dig");
|
130
|
+
|
131
|
+
rb_cHash = rb_define_class("Hash", rb_cObject);
|
132
|
+
rb_define_method(rb_cHash, "dig", rb_hash_dig, -1);
|
133
|
+
|
134
|
+
rb_cArray = rb_define_class("Array", rb_cObject);
|
135
|
+
rb_define_method(rb_cArray, "dig", rb_ary_dig, -1);
|
136
|
+
}
|
@@ -0,0 +1,42 @@
|
|
1
|
+
ENV['RC_ARCHS'] = '' if RUBY_PLATFORM =~ /darwin/
|
2
|
+
|
3
|
+
# :stopdoc:
|
4
|
+
|
5
|
+
require 'mkmf'
|
6
|
+
|
7
|
+
LIBDIR = RbConfig::CONFIG['libdir']
|
8
|
+
INCLUDEDIR = RbConfig::CONFIG['includedir']
|
9
|
+
|
10
|
+
$CFLAGS << " -O3 -Wall -Wcast-qual -Wwrite-strings -Wconversion -Wmissing-noreturn -Winline"
|
11
|
+
|
12
|
+
HEADER_DIRS = [
|
13
|
+
# First search /opt/local for macports
|
14
|
+
'/opt/local/include',
|
15
|
+
|
16
|
+
# Then search /usr/local for people that installed from source
|
17
|
+
'/usr/local/include',
|
18
|
+
|
19
|
+
# Check the ruby install locations
|
20
|
+
INCLUDEDIR,
|
21
|
+
|
22
|
+
# Finally fall back to /usr
|
23
|
+
'/usr/include',
|
24
|
+
]
|
25
|
+
|
26
|
+
LIB_DIRS = [
|
27
|
+
# First search /opt/local for macports
|
28
|
+
'/opt/local/lib',
|
29
|
+
|
30
|
+
# Then search /usr/local for people that installed from source
|
31
|
+
'/usr/local/lib',
|
32
|
+
|
33
|
+
# Check the ruby install locations
|
34
|
+
LIBDIR,
|
35
|
+
|
36
|
+
# Finally fall back to /usr
|
37
|
+
'/usr/lib',
|
38
|
+
]
|
39
|
+
|
40
|
+
create_makefile('backport_dig/backport_dig')
|
41
|
+
|
42
|
+
# :startdoc:
|
data/lib/backport_dig.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'backport_dig/backport_dig'
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: backport_dig
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Koichi ITO
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: hoe
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake-compiler
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.0.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.0.0
|
55
|
+
description: backport_dig is the backport of Hash#dig and Array#dig in Ruby 2.3.0
|
56
|
+
to older Ruby versions.
|
57
|
+
email: koic.ito@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions:
|
60
|
+
- ext/backport_dig/extconf.rb
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- Manifest.txt
|
64
|
+
- README.md
|
65
|
+
- Rakefile
|
66
|
+
- ext/backport_dig/backport_dig.c
|
67
|
+
- ext/backport_dig/extconf.rb
|
68
|
+
- lib/backport_dig.rb
|
69
|
+
homepage: https://github.com/koic/backport_dig
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata: {}
|
73
|
+
post_install_message: |
|
74
|
+
backport_dig is the backport of Hash#dig and Array#dig in Ruby 2.3.0 to older Ruby versions.
|
75
|
+
|
76
|
+
The best way is to use Ruby 2.3.0 or later.
|
77
|
+
|
78
|
+
Thanks.
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: 2.0.0
|
87
|
+
- - "<"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 2.3.0
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.4.5.1
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: backport_dig is the backport of Hash#dig and Array#dig in Ruby 2.3.0 to older
|
101
|
+
Ruby versions.
|
102
|
+
test_files: []
|
103
|
+
has_rdoc:
|