bendy 0.2.3 → 0.3.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 +4 -4
- data/.gitignore +1 -0
- data/README.md +3 -1
- data/lib/bendy/array.rb +13 -13
- data/lib/bendy/hash.rb +21 -0
- data/lib/bendy/kernel.rb +18 -0
- data/lib/bendy/logical.rb +2 -6
- data/lib/bendy/object.rb +19 -12
- data/lib/bendy/version.rb +1 -1
- data/spec/bendy/hash_spec.rb +21 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0dfc661e95d819b889ed872393dc2a882329bc14
|
4
|
+
data.tar.gz: 7e0fb92dc1a7b384ba80d3bde27971bd143896c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b38391de2e2102c8b944ad76faba5bf7e52a92765b0309dd697c21a353c1af86191894864b035cecaa6e95e9eac63d24d31da0db679aaef5a71456fe4038c982
|
7
|
+
data.tar.gz: 1354f7f3b762e8348f8d69101974a854acc40e6ffd664830520bd32c3ea2884c29dec6b317c42030f48195f9300fe34801bb1d6c663ddb390894872d0bc1cca1
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Bendy
|
2
2
|
|
3
|
+
[](http://badge.fury.io/rb/bendy)
|
4
|
+
|
3
5
|
This is a collection of helpful utilities in use at Bendyworks.
|
4
6
|
|
5
7
|
## Installation
|
@@ -23,7 +25,7 @@ are best off reading the code for things that catch your eye.
|
|
23
25
|
|
24
26
|
## Contributing
|
25
27
|
|
26
|
-
1. Fork it ( http://github.com
|
28
|
+
1. Fork it ( http://github.com/twopoint718/bendygem/fork )
|
27
29
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
28
30
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
29
31
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/bendy/array.rb
CHANGED
@@ -3,27 +3,27 @@
|
|
3
3
|
|
4
4
|
module Bendy
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
##
|
10
|
-
# +tails+ produces all sub-arrays by successively dropping leading
|
11
|
-
# elements:
|
6
|
+
# @!method tails()
|
7
|
+
# +tails+ produces all sub-arrays by successively dropping leading
|
8
|
+
# elements:
|
12
9
|
#
|
13
|
-
#
|
14
|
-
#
|
10
|
+
# [1, 2, 3].tails
|
11
|
+
# # => [[1, 2, 3], [2, 3], [3], []]
|
15
12
|
|
13
|
+
refine Array do
|
16
14
|
def tails
|
17
15
|
(self.length + 1).times.map{ |i| self.drop(i) }
|
18
16
|
end
|
17
|
+
end
|
19
18
|
|
20
|
-
|
21
|
-
#
|
22
|
-
#
|
19
|
+
# @!method inits()
|
20
|
+
# +inits+ produces all sub-arrays by successively taking leading
|
21
|
+
# elements:
|
23
22
|
#
|
24
|
-
#
|
25
|
-
#
|
23
|
+
# [1, 2, 3].inits
|
24
|
+
# # => [[], [1], [1, 2], [1, 2, 3]]
|
26
25
|
|
26
|
+
refine Array do
|
27
27
|
def inits
|
28
28
|
(self.length + 1).times.map{ |i| self.take(i) }
|
29
29
|
end
|
data/lib/bendy/hash.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
##
|
2
|
+
# We're using refinements to _freedom patch_ Hash in a safe way.
|
3
|
+
|
4
|
+
module Bendy
|
5
|
+
|
6
|
+
# @!method get_deep()
|
7
|
+
# +get_deep+ digs into a hash along the "route" given by fields
|
8
|
+
# elements:
|
9
|
+
#
|
10
|
+
# { foo: { bar: { baz: 1 } } }.get_deep(:foo, :bar, :baz)
|
11
|
+
# # => 1
|
12
|
+
#
|
13
|
+
# { foo: { bar: { baz: 1 } } }.get_deep(:foo, :bar, :baz, :quux)
|
14
|
+
# # => nil
|
15
|
+
|
16
|
+
refine Hash do
|
17
|
+
def get_deep(*fields)
|
18
|
+
fields.inject(self) {|acc,e| acc[e] if acc.is_a?(Hash)}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/bendy/kernel.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
##
|
2
|
+
# Refine the Kernel module
|
3
|
+
|
4
|
+
module Bendy
|
5
|
+
|
6
|
+
# @!method require()
|
7
|
+
# +require+ does the normal thing that require does, but also logs it
|
8
|
+
|
9
|
+
refine Kernel do
|
10
|
+
def require_with_log(name)
|
11
|
+
puts "require: #{name}"
|
12
|
+
old_require name
|
13
|
+
end
|
14
|
+
|
15
|
+
alias_method :old_require, :require
|
16
|
+
alias_method :require, :require_with_log
|
17
|
+
end
|
18
|
+
end
|
data/lib/bendy/logical.rb
CHANGED
@@ -1,13 +1,9 @@
|
|
1
1
|
module Bendy
|
2
2
|
|
3
|
-
##
|
4
3
|
# Some logical methods
|
5
|
-
|
6
4
|
module Logical
|
7
5
|
|
8
|
-
|
9
|
-
# call-seq:
|
10
|
-
# implies(antecedent) { consequent } #=> true or consequent
|
6
|
+
# @!method implies(antecedent){ consequent } #=> true or consequent
|
11
7
|
#
|
12
8
|
# Material implication
|
13
9
|
#
|
@@ -15,8 +11,8 @@ module Bendy
|
|
15
11
|
# (antecedent), then pay attention to the second thing (consequent). But
|
16
12
|
# if the first thing is false then the whole thing is true:
|
17
13
|
#
|
14
|
+
# @example Extract the bar value given foo exists
|
18
15
|
# implies(foo){foo[:bar]}
|
19
|
-
|
20
16
|
def implies(antecedent)
|
21
17
|
!!antecedent ? yield : true
|
22
18
|
end
|
data/lib/bendy/object.rb
CHANGED
@@ -1,19 +1,17 @@
|
|
1
|
+
# Bendy module that _freedom patches_ Object and NilClass to add `try`, like
|
2
|
+
# in ActiveSupport
|
1
3
|
module Bendy
|
2
|
-
refine Object do
|
3
4
|
|
4
|
-
|
5
|
-
# call
|
6
|
-
#
|
7
|
-
#
|
8
|
-
# +try+ acts like a normal method call, unless it is being called
|
9
|
-
# on a +nil+ object, in which case it returns +nil+:
|
5
|
+
# !@method try(:some_method)
|
6
|
+
# +try+ acts like a normal method call, unless it is being called
|
7
|
+
# on a +nil+ object, in which case it returns +nil+:
|
10
8
|
#
|
11
|
-
#
|
12
|
-
#
|
9
|
+
# nil.try(:to_i) # => nil
|
10
|
+
# "10".try(:to_i) # => 10
|
13
11
|
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
|
12
|
+
# Copied pretty much verbatim from ActiveSupport:
|
13
|
+
# https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/try.rb
|
14
|
+
refine Object do
|
17
15
|
def try(*a, &b)
|
18
16
|
if a.empty? && block_given?
|
19
17
|
yield self
|
@@ -23,6 +21,15 @@ module Bendy
|
|
23
21
|
end
|
24
22
|
end
|
25
23
|
|
24
|
+
# !@method try(:some_method)
|
25
|
+
# +try+ acts like a normal method call, unless it is being called
|
26
|
+
# on a +nil+ object, in which case it returns +nil+:
|
27
|
+
#
|
28
|
+
# nil.try(:to_i) # => nil
|
29
|
+
# "10".try(:to_i) # => 10
|
30
|
+
#
|
31
|
+
# Copied pretty much verbatim from ActiveSupport:
|
32
|
+
# https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/try.rb
|
26
33
|
refine NilClass do
|
27
34
|
def try(*args)
|
28
35
|
nil
|
data/lib/bendy/version.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'minitest/spec'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'bendy/hash'
|
4
|
+
|
5
|
+
using Bendy
|
6
|
+
|
7
|
+
describe Hash do
|
8
|
+
before do
|
9
|
+
@hash = { foo: { bar: { baz: 1 } } }
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#get_deep' do
|
13
|
+
it 'accesses a key inside a nested hash' do
|
14
|
+
@hash.get_deep(:foo, :bar, :baz).must_equal 1
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'accesses a key inside a nested hash' do
|
18
|
+
@hash.get_deep(:foo, :bar, :baz, :quux).must_equal nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bendy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Wilson
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-09-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -54,10 +54,13 @@ files:
|
|
54
54
|
- bendy.gemspec
|
55
55
|
- lib/bendy.rb
|
56
56
|
- lib/bendy/array.rb
|
57
|
+
- lib/bendy/hash.rb
|
58
|
+
- lib/bendy/kernel.rb
|
57
59
|
- lib/bendy/logical.rb
|
58
60
|
- lib/bendy/object.rb
|
59
61
|
- lib/bendy/version.rb
|
60
62
|
- spec/bendy/array_spec.rb
|
63
|
+
- spec/bendy/hash_spec.rb
|
61
64
|
- spec/bendy/logical_spec.rb
|
62
65
|
- spec/bendy/object_spec.rb
|
63
66
|
homepage: https://github.com/twopoint718/bendygem
|
@@ -86,5 +89,6 @@ specification_version: 4
|
|
86
89
|
summary: A collection of helpful libraries
|
87
90
|
test_files:
|
88
91
|
- spec/bendy/array_spec.rb
|
92
|
+
- spec/bendy/hash_spec.rb
|
89
93
|
- spec/bendy/logical_spec.rb
|
90
94
|
- spec/bendy/object_spec.rb
|