beefup 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 785c7bdf9c4444e415aef2d4e310ccda7124ee6f
4
- data.tar.gz: 87f6b53026511bb56a705c932c3abb9fbfcd456d
3
+ metadata.gz: eb4330631738c32173a3b6b82ee7925418b5a350
4
+ data.tar.gz: 2eea3b965ef479118db5a24732bbae46d9e2d745
5
5
  SHA512:
6
- metadata.gz: 044c919d3039277d0326bd5b85ba8362955b2c7eba4aedb86aae6247277c5ca7eae3b5d45db68076c9687772bf9fd73187aab30603bfdbb5b891aff27cbd081d
7
- data.tar.gz: c6bf320dc3cac01f1a355c2db6c00cf5ba805fc59e84762312dbf00f6f1c0491b0296da1f91e7c817c12af7c02befa00e443773c4202e326600f298da3c60a5c
6
+ metadata.gz: 14e917c56dc866bde551d74c32911a2e1e7860722fc7d045d9bd0631eb459c6cdff6c677f18d74295f99577ca808bbf1c541ab88c4afe6f8ac2b75553b2becb6
7
+ data.tar.gz: a8003abcb6843279a2a203d418c9af4b96566367e9e9516760e094e2265425c0253598ac83ea42690f33866f89ba56b97d511f385f1c6ba2f1419bc91b03921a
data/README.md CHANGED
@@ -50,6 +50,22 @@ Smarter titleising of a string which ignores certain words (E.g an, a, is, of, e
50
50
  ```ruby
51
51
  "is this an original string? yes it is".titleise
52
52
  => "Is This an Original String? Yes It Is"
53
+
54
+ "this is a title".titleise
55
+ => "This is a Title"
56
+ ```
57
+
58
+ ### Fixnum
59
+
60
+ #### To Boolean
61
+
62
+ ```ruby
63
+ 1.to_boolean
64
+ => true
65
+ 0.to_b
66
+ => false
67
+ 25.to_b
68
+ => true
53
69
  ```
54
70
 
55
71
  ### Hash
@@ -90,6 +106,17 @@ Recursively merge two hashes
90
106
  => { :a => 1, :b => "b", :c => { :c1 => 2, :c2 => { :c2a => "c2a", :c2b => "c2b" } } }
91
107
  ```
92
108
 
109
+ #### Array Access
110
+
111
+ Access a nested Hash element with a given array depicting the path
112
+
113
+ ```ruby
114
+ hash = { :a => {:b => {:c => { :d => "Value" } } } }
115
+ array = [:a, :b, :c, :d]
116
+ hash.access_by_array(array)
117
+ => "Value"
118
+ ```
119
+
93
120
  ## Contributing
94
121
 
95
122
  1. Fork it
@@ -0,0 +1 @@
1
+ require 'beef/core_ext/fixnum/to_boolean'
@@ -0,0 +1,11 @@
1
+ class Fixnum
2
+
3
+ def to_boolean
4
+ case self
5
+ when 0 then false
6
+ else true
7
+ end
8
+ end
9
+ alias :to_b :to_boolean
10
+
11
+ end
@@ -1,2 +1,3 @@
1
1
  require 'beef/core_ext/hash/method_access'
2
- require 'beef/core_ext/hash/deep_merge'
2
+ require 'beef/core_ext/hash/deep_merge'
3
+ require 'beef/core_ext/hash/array_access'
@@ -0,0 +1,23 @@
1
+ class Hash
2
+
3
+ # Access nested element with a given array of keys depciting the path
4
+ def access_by_array(key_array)
5
+ return nil unless key_array.is_a? Array
6
+ current_element = self
7
+ key_array.each do |key|
8
+ if current_element.is_a? Hash
9
+ current_element = current_element[key]
10
+ else
11
+ current_element = nil
12
+ end
13
+
14
+ end
15
+ current_element
16
+ end
17
+
18
+ # Coming soon
19
+ # def set_by_array(key_array, value)
20
+ # access_by_array(key_array)
21
+ # end
22
+
23
+ end
@@ -0,0 +1 @@
1
+ require 'beef/core_ext/nil_class/arrayify'
@@ -0,0 +1,7 @@
1
+ class NilClass
2
+
3
+ # Avoid unecessary checks on a potential nil array
4
+ def each
5
+ []
6
+ end
7
+ end
data/lib/beef/version.rb CHANGED
@@ -2,7 +2,7 @@ module Beef
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 2
5
+ TINY = 3
6
6
  PRE = nil
7
7
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
8
8
  end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'beef/core_ext/fixnum'
5
+
6
+ describe "Fixnum falsey" do
7
+ it "should return false when to_b is called" do
8
+ 0.to_b.should be_false
9
+ end
10
+ end
11
+
12
+ describe "Fixnum truthy" do
13
+ it "should return true when to_b is called" do
14
+ 1.to_b.should be_true
15
+ 20.to_b.should be_true
16
+ end
17
+ end
@@ -40,4 +40,26 @@ describe "Hash deep merge" do
40
40
  @hash1.deep_merge!(@hash2)
41
41
  @hash1.should == @expected_hash
42
42
  end
43
+ end
44
+
45
+ describe "Hash array access" do
46
+ before do
47
+ @hash = { :a => { :b => { :c => { :d => "Value" } } } }
48
+ @key_array = [:a, :b, :c, :d]
49
+ @key_array_fail = [:a, :b, :c, :d, :e]
50
+ @key_array_fail2 = [:a, :e, :c]
51
+ end
52
+
53
+ it "should return correct value given an array of keys" do
54
+ @hash.access_by_array(@key_array).should == "Value"
55
+ end
56
+
57
+ it "should return nil given array of keys that is too long" do
58
+ @hash.access_by_array(@key_array_fail).should be_nil
59
+ end
60
+
61
+ it "should return nil given an invalid array of keys" do
62
+ @hash.access_by_array(@key_array_fail2).should be_nil
63
+ end
64
+
43
65
  end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'beef/core_ext/nil_class'
5
+
6
+ describe "Nil class" do
7
+ it "should return an empty array when calling each" do
8
+ nil.each {|a| a }.should == []
9
+ end
10
+ end
@@ -11,7 +11,7 @@ describe "String falsey" do
11
11
  end
12
12
  end
13
13
 
14
- describe "String truey" do
14
+ describe "String truthy" do
15
15
  it "should return true when to_b is called" do
16
16
  "true".to_b.should be_true
17
17
  "1".to_b.should be_true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beefup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Jones
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-27 00:00:00.000000000 Z
11
+ date: 2013-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -68,14 +68,21 @@ files:
68
68
  - Rakefile
69
69
  - beef.gemspec
70
70
  - lib/beef.rb
71
+ - lib/beef/core_ext/fixnum.rb
72
+ - lib/beef/core_ext/fixnum/to_boolean.rb
71
73
  - lib/beef/core_ext/hash.rb
74
+ - lib/beef/core_ext/hash/array_access.rb
72
75
  - lib/beef/core_ext/hash/deep_merge.rb
73
76
  - lib/beef/core_ext/hash/method_access.rb
77
+ - lib/beef/core_ext/nil_class.rb
78
+ - lib/beef/core_ext/nil_class/arrayify.rb
74
79
  - lib/beef/core_ext/string.rb
75
80
  - lib/beef/core_ext/string/titleise.rb
76
81
  - lib/beef/core_ext/string/to_boolean.rb
77
82
  - lib/beef/version.rb
83
+ - spec/beef/core_ext/fixnum_spec.rb
78
84
  - spec/beef/core_ext/hash_spec.rb
85
+ - spec/beef/core_ext/nil_class_spec.rb
79
86
  - spec/beef/core_ext/string_spec.rb
80
87
  - spec/spec_helper.rb
81
88
  homepage: http://simonjones.github.io/beef
@@ -102,6 +109,8 @@ signing_key:
102
109
  specification_version: 4
103
110
  summary: A Ruby core exntension library.
104
111
  test_files:
112
+ - spec/beef/core_ext/fixnum_spec.rb
105
113
  - spec/beef/core_ext/hash_spec.rb
114
+ - spec/beef/core_ext/nil_class_spec.rb
106
115
  - spec/beef/core_ext/string_spec.rb
107
116
  - spec/spec_helper.rb