dot_notation 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +37 -0
- data/Rakefile +1 -0
- data/dot_notation.gemspec +32 -0
- data/lib/dot_notation/version.rb +3 -0
- data/lib/dot_notation.rb +76 -0
- metadata +90 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 14e6c6373bdb4aeb50cdb10f4989a2daf2ea9194
|
4
|
+
data.tar.gz: 05861f1351dc654cf6836310bae609d29bf070ec
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0e3973461173426201bc08415238479a71d9e494870c6b70cd7d7304a19a0e40fce1df58042a1e00d2bfcb6527ce60e3c953b01e46ee9fd0e27f416ac0f7f18e
|
7
|
+
data.tar.gz: 0588a40a12e7b5790fdb514a530762fc7202cbb005db9549b68ec35ee3866b5ef2b69668c5fce51d74ac6df338404804e566f67fea886f169f21d7f8174ea2bd
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Joshua Szmajda
|
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
|
+
# DotNotation
|
2
|
+
|
3
|
+
Simple-ish enumberable-simplifier. Useful for APIs like Twitter, etc
|
4
|
+
|
5
|
+
If you have a hash or an array or something that quacks like one, you can do stuff
|
6
|
+
example:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
h = {a: {b: {c: [{d: 'hi'}]}}}
|
10
|
+
h.extend(DotNotation)
|
11
|
+
h.dot('a.b.c.0.d')
|
12
|
+
#=> 'hi'
|
13
|
+
h.dot('a.b.c.foo.bar.bz.whatever.124.whocares')
|
14
|
+
#=> nil
|
15
|
+
```
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
gem 'dot_notation'
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install dot_notation
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it ( http://github.com/<my-github-username>/dot_notation/fork )
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Add 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 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dot_notation/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dot_notation"
|
8
|
+
spec.version = DotNotation::VERSION
|
9
|
+
spec.authors = ["Joshua Szmajda"]
|
10
|
+
spec.email = ["josh@optoro.com"]
|
11
|
+
spec.summary = %q{An enumerable simplifier. Busts your method cache, I guess.}
|
12
|
+
spec.description = %q{Simple-ish enumberable-simplifier. Useful for APIs like Twitter, etc
|
13
|
+
|
14
|
+
If you have a hash or an array or something that quacks like one, you can do stuff
|
15
|
+
example:
|
16
|
+
h = {a: {b: {c: [{d: 'hi'}]}}}
|
17
|
+
h.extend(DotNotation)
|
18
|
+
h.dot('a.b.c.0.d')
|
19
|
+
#=> 'hi'
|
20
|
+
h.dot('a.b.c.foo.bar.bz.whatever.124.whocares')
|
21
|
+
#=> nil}
|
22
|
+
spec.homepage = ""
|
23
|
+
spec.license = "MIT"
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0")
|
26
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
27
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
31
|
+
spec.add_development_dependency "rake"
|
32
|
+
end
|
data/lib/dot_notation.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require "dot_notation/version"
|
2
|
+
|
3
|
+
module DotNotation
|
4
|
+
# Your code goes here...
|
5
|
+
def self.included(base)
|
6
|
+
raise "Unsupported Class" unless base.respond_to?('[]')
|
7
|
+
end
|
8
|
+
|
9
|
+
def dot(path)
|
10
|
+
return nil if self.nil? # optimization for NilClass
|
11
|
+
|
12
|
+
data = self
|
13
|
+
path.split(/\./).each do |selector|
|
14
|
+
|
15
|
+
selector = selector.to_i if selector =~ /^\d+$/
|
16
|
+
|
17
|
+
if value = access(data, selector)
|
18
|
+
data = value
|
19
|
+
else
|
20
|
+
return nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
if @dot_options && @dot_options[:autodot]
|
25
|
+
extend_result!(data)
|
26
|
+
data.each{|e| extend_result!(e) } if data.kind_of? Array
|
27
|
+
end
|
28
|
+
|
29
|
+
data
|
30
|
+
end
|
31
|
+
|
32
|
+
def dot_options(options)
|
33
|
+
@dot_options = (@dot_options || {}).merge(options)
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def extend_result!(result)
|
39
|
+
begin
|
40
|
+
result.extend(DotNotation)
|
41
|
+
result.dot_options(@dot_options)
|
42
|
+
rescue TypeError
|
43
|
+
# ignore, we get this when trying to extend an int or something
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def access(data, selector)
|
48
|
+
if defined?(ActiveRecord::Base) && data.kind_of?(ActiveRecord::Base)
|
49
|
+
access_active_record(data, selector)
|
50
|
+
else
|
51
|
+
access_generic(data, selector)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def access_active_record(data, selector)
|
56
|
+
selsym = selector.respond_to?(:to_sym) ? selector.to_sym : nil
|
57
|
+
if selsym && data.respond_to?(selsym)
|
58
|
+
data.send(selsym)
|
59
|
+
else
|
60
|
+
access_generic(data, selector)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def access_generic(data, selector)
|
65
|
+
if data[selector]
|
66
|
+
data[selector]
|
67
|
+
else
|
68
|
+
selsym = selector.respond_to?(:to_sym) ? selector.to_sym : nil
|
69
|
+
if selsym && data[selsym]
|
70
|
+
data[selsym]
|
71
|
+
else
|
72
|
+
nil
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dot_notation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joshua Szmajda
|
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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
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: |-
|
42
|
+
Simple-ish enumberable-simplifier. Useful for APIs like Twitter, etc
|
43
|
+
|
44
|
+
If you have a hash or an array or something that quacks like one, you can do stuff
|
45
|
+
example:
|
46
|
+
h = {a: {b: {c: [{d: 'hi'}]}}}
|
47
|
+
h.extend(DotNotation)
|
48
|
+
h.dot('a.b.c.0.d')
|
49
|
+
#=> 'hi'
|
50
|
+
h.dot('a.b.c.foo.bar.bz.whatever.124.whocares')
|
51
|
+
#=> nil
|
52
|
+
email:
|
53
|
+
- josh@optoro.com
|
54
|
+
executables: []
|
55
|
+
extensions: []
|
56
|
+
extra_rdoc_files: []
|
57
|
+
files:
|
58
|
+
- .gitignore
|
59
|
+
- Gemfile
|
60
|
+
- LICENSE.txt
|
61
|
+
- README.md
|
62
|
+
- Rakefile
|
63
|
+
- dot_notation.gemspec
|
64
|
+
- lib/dot_notation.rb
|
65
|
+
- lib/dot_notation/version.rb
|
66
|
+
homepage: ''
|
67
|
+
licenses:
|
68
|
+
- MIT
|
69
|
+
metadata: {}
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 2.2.2
|
87
|
+
signing_key:
|
88
|
+
specification_version: 4
|
89
|
+
summary: An enumerable simplifier. Busts your method cache, I guess.
|
90
|
+
test_files: []
|