beefup 0.0.2
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 +7 -0
- data/.gitignore +17 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +99 -0
- data/Rakefile +12 -0
- data/beef.gemspec +25 -0
- data/lib/beef/core_ext/hash/deep_merge.rb +15 -0
- data/lib/beef/core_ext/hash/method_access.rb +25 -0
- data/lib/beef/core_ext/hash.rb +2 -0
- data/lib/beef/core_ext/string/titleise.rb +32 -0
- data/lib/beef/core_ext/string/to_boolean.rb +11 -0
- data/lib/beef/core_ext/string.rb +2 -0
- data/lib/beef/version.rb +9 -0
- data/lib/beef.rb +5 -0
- data/spec/beef/core_ext/hash_spec.rb +43 -0
- data/spec/beef/core_ext/string_spec.rb +59 -0
- data/spec/spec_helper.rb +9 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 785c7bdf9c4444e415aef2d4e310ccda7124ee6f
|
4
|
+
data.tar.gz: 87f6b53026511bb56a705c932c3abb9fbfcd456d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 044c919d3039277d0326bd5b85ba8362955b2c7eba4aedb86aae6247277c5ca7eae3b5d45db68076c9687772bf9fd73187aab30603bfdbb5b891aff27cbd081d
|
7
|
+
data.tar.gz: c6bf320dc3cac01f1a355c2db6c00cf5ba805fc59e84762312dbf00f6f1c0491b0296da1f91e7c817c12af7c02befa00e443773c4202e326600f298da3c60a5c
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
beef
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.0.0-p0
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Simon Jones
|
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,99 @@
|
|
1
|
+
# Beef (beefup)
|
2
|
+
|
3
|
+
Beef up your Ruby core with these useful extensions
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'beefup'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install beefup
|
20
|
+
|
21
|
+
And require it:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'beefup'
|
25
|
+
```
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
### String
|
30
|
+
|
31
|
+
#### To Boolean
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
"true".to_boolean
|
35
|
+
=> true
|
36
|
+
"yes".to_b
|
37
|
+
=> true
|
38
|
+
"no".to_b
|
39
|
+
=> false
|
40
|
+
"false".to_b
|
41
|
+
=> false
|
42
|
+
"something that isn't falsey".to_boolean
|
43
|
+
=> true
|
44
|
+
```
|
45
|
+
|
46
|
+
#### Titleise
|
47
|
+
|
48
|
+
Smarter titleising of a string which ignores certain words (E.g an, a, is, of, etc..) when they don't appear at the beginning or the end of the string.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
"is this an original string? yes it is".titleise
|
52
|
+
=> "Is This an Original String? Yes It Is"
|
53
|
+
```
|
54
|
+
|
55
|
+
### Hash
|
56
|
+
|
57
|
+
#### Method Access
|
58
|
+
|
59
|
+
Easy access to hash values via method calls of the keys.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
hash = {one: 1, two: 2, three: 3, four: 4}
|
63
|
+
|
64
|
+
# Value access
|
65
|
+
hash.one
|
66
|
+
=> 1
|
67
|
+
|
68
|
+
# Key query
|
69
|
+
hash.one?
|
70
|
+
=> true
|
71
|
+
hash.five?
|
72
|
+
=> false
|
73
|
+
hash.five
|
74
|
+
=> NoMethodError
|
75
|
+
|
76
|
+
# Value assignment
|
77
|
+
hash.two = "new value"
|
78
|
+
hash.two
|
79
|
+
=> "new value"
|
80
|
+
```
|
81
|
+
|
82
|
+
#### Deep Merge
|
83
|
+
|
84
|
+
Recursively merge two hashes
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
hash_1 = { :a => "a", :b => "b", :c => { :c1 => "c1", :c2 => { :c2a => "c2a" } } }
|
88
|
+
hash_2 = { :a => 1, :c => { :c1 => 2, :c2 => { :c2b => "c2b" } } }
|
89
|
+
hash_1.deep_merge(hash_2)
|
90
|
+
=> { :a => 1, :b => "b", :c => { :c1 => 2, :c2 => { :c2a => "c2a", :c2b => "c2b" } } }
|
91
|
+
```
|
92
|
+
|
93
|
+
## Contributing
|
94
|
+
|
95
|
+
1. Fork it
|
96
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
97
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
98
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
99
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
require 'rspec/core'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
7
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
8
|
+
#spec.rspec_opts = '--order random' need to fix some test to make this randow :( bad tests!
|
9
|
+
end
|
10
|
+
|
11
|
+
desc 'Default: Run all specs.'
|
12
|
+
task :default => :spec
|
data/beef.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'beef/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "beefup"
|
8
|
+
gem.version = Beef::Version::STRING
|
9
|
+
gem.authors = ["Simon Jones"]
|
10
|
+
gem.email = ["spj3rd@googlemail.com"]
|
11
|
+
gem.description = "Beef up your Ruby core with these useful extensions"
|
12
|
+
gem.summary = "A Ruby core exntension library."
|
13
|
+
gem.homepage = "http://simonjones.github.io/beef"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
|
21
|
+
gem.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
gem.add_development_dependency "rake"
|
23
|
+
|
24
|
+
gem.add_development_dependency "rspec", "~> 2.14"
|
25
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Hash
|
2
|
+
|
3
|
+
def deep_merge(other_hash)
|
4
|
+
dup.deep_merge!(other_hash)
|
5
|
+
end
|
6
|
+
|
7
|
+
def deep_merge!(other_hash)
|
8
|
+
other_hash.each do |k,v|
|
9
|
+
tv = self[k]
|
10
|
+
self[k] = tv.is_a?(::Hash) && v.is_a?(::Hash) ? tv.deep_merge(v) : v
|
11
|
+
end
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class Hash
|
2
|
+
|
3
|
+
def respond_to?(name, include_private = false)
|
4
|
+
return true if key?(name.to_s) || key?(name.to_sym)
|
5
|
+
return true if name.to_s =~ /=$/
|
6
|
+
return true if name.to_s =~ /(.*)\?$/ && (key?($1) || key?($1.to_sym))
|
7
|
+
super
|
8
|
+
end
|
9
|
+
|
10
|
+
def method_missing(name, *args)
|
11
|
+
return self[name.to_s] if key?(name.to_s)
|
12
|
+
return self[name.to_sym] if key?(name.to_sym)
|
13
|
+
if args.size == 1 && name.to_s =~ /(.*)=$/
|
14
|
+
return self[$1.to_s] = args.first
|
15
|
+
end
|
16
|
+
if args.empty? && name.to_s =~ /(.*)\?$/
|
17
|
+
if (key?($1) || key?($1.to_sym))
|
18
|
+
return self[$1] || self[$1.to_sym]
|
19
|
+
else
|
20
|
+
return false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
super
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class String
|
2
|
+
IGNORE = %w( the is to a an and as at but by for if in of on or via )
|
3
|
+
REG_IGNORE = IGNORE.join('|')
|
4
|
+
|
5
|
+
def titleise
|
6
|
+
result = ""
|
7
|
+
self.gsub(/[_-]/, ' ').split(/( [:.;?!][ ] | (?:[ ]|^)["“] )/x).each do |substring|
|
8
|
+
substring.gsub!(/ \b( [[:alpha:]] [[:lower:].'’]* )\b /x) do |word|
|
9
|
+
# Ignore words that contain dots - e.g urls
|
10
|
+
(word =~ / [[:alpha:]] [.] [[:alpha:]] /x) ? word : word.capitalize
|
11
|
+
end #gsub!
|
12
|
+
|
13
|
+
# Downcase the list of words to ignore
|
14
|
+
substring.gsub!(/\b(#{REG_IGNORE})\b/io) { |word| word.downcase }
|
15
|
+
|
16
|
+
# If the first or last words are in the ignore list then capetlise them regardless.
|
17
|
+
substring.gsub!(/\A([[:punct:]]*)(#{REG_IGNORE})\b/io) { |word| $1 + $2.capitalize }
|
18
|
+
substring.gsub!(/\b(#{REG_IGNORE})([[:punct:]]*)\Z/io) { |word| $1.capitalize + $2 }
|
19
|
+
|
20
|
+
result += substring
|
21
|
+
end
|
22
|
+
|
23
|
+
# Special case "'s"
|
24
|
+
result.gsub!(/(['’])S\b/, '\1s')
|
25
|
+
result
|
26
|
+
end
|
27
|
+
|
28
|
+
def titleise!
|
29
|
+
self.replace(self.titleise)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
data/lib/beef/version.rb
ADDED
data/lib/beef.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'beef/core_ext/hash'
|
5
|
+
|
6
|
+
describe "Hash access" do
|
7
|
+
before do
|
8
|
+
@hash = {:one => "first", :two => "second", :three => "third", :four => "fourth"}
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should allow method-like read" do
|
12
|
+
@hash.one.should == "first"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should allow method-like write" do
|
16
|
+
@hash.three = "3"
|
17
|
+
@hash.three.should == "3"
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should allow method-like query" do
|
21
|
+
@hash.four?.should be_true
|
22
|
+
@hash.five?.should be_false
|
23
|
+
lambda { @hash.five }.should raise_error(NoMethodError)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "Hash deep merge" do
|
29
|
+
before do
|
30
|
+
@hash1 = { :a => "a", :b => "b", :c => { :c1 => "c1", :c2 => "c2", :c3 => { :d1 => "d1" } } }
|
31
|
+
@hash2 = { :a => 1, :c => { :c1 => 2, :c3 => { :d2 => "d2" } } }
|
32
|
+
@expected_hash = { :a => 1, :b => "b", :c => { :c1 => 2, :c2 => "c2", :c3 => { :d1 => "d1", :d2 => "d2" } } }
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should deep merge two hashes' do
|
36
|
+
@hash1.deep_merge(@hash2).should == @expected_hash
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should deep merge two hashes with bang method' do
|
40
|
+
@hash1.deep_merge!(@hash2)
|
41
|
+
@hash1.should == @expected_hash
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'beef/core_ext/string'
|
5
|
+
|
6
|
+
describe "String falsey" do
|
7
|
+
it "should return false when to_b is called" do
|
8
|
+
"false".to_b.should be_false
|
9
|
+
"0".to_b.should be_false
|
10
|
+
"nil".to_b.should be_false
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "String truey" do
|
15
|
+
it "should return true when to_b is called" do
|
16
|
+
"true".to_b.should be_true
|
17
|
+
"1".to_b.should be_true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe String do
|
22
|
+
before do
|
23
|
+
@original_string = "is this is an original string? yes it is"
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "titleised" do
|
27
|
+
before do
|
28
|
+
@titleised = @original_string.titleise
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return a new string" do
|
32
|
+
@titleised.should_not equal(@original_string)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should titleise the first word reglardless" do
|
36
|
+
@titleised.should =~ /^Is/
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should titleise the last word reglardless" do
|
40
|
+
@titleised.should =~ /Is$/
|
41
|
+
end
|
42
|
+
|
43
|
+
String::IGNORE.each do |ignore_word|
|
44
|
+
it "should not titleise '#{ignore_word}'" do
|
45
|
+
"This '#{ignore_word}' should be ignored".titleise.should == "This '#{ignore_word}' Should Be Ignored"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "titleised!" do
|
51
|
+
before do
|
52
|
+
@titleised = @original_string.titleise!
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should replace original string" do
|
56
|
+
@titleised.should equal(@original_string)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: beefup
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Simon Jones
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-27 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.14'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.14'
|
55
|
+
description: Beef up your Ruby core with these useful extensions
|
56
|
+
email:
|
57
|
+
- spj3rd@googlemail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .ruby-gemset
|
64
|
+
- .ruby-version
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- beef.gemspec
|
70
|
+
- lib/beef.rb
|
71
|
+
- lib/beef/core_ext/hash.rb
|
72
|
+
- lib/beef/core_ext/hash/deep_merge.rb
|
73
|
+
- lib/beef/core_ext/hash/method_access.rb
|
74
|
+
- lib/beef/core_ext/string.rb
|
75
|
+
- lib/beef/core_ext/string/titleise.rb
|
76
|
+
- lib/beef/core_ext/string/to_boolean.rb
|
77
|
+
- lib/beef/version.rb
|
78
|
+
- spec/beef/core_ext/hash_spec.rb
|
79
|
+
- spec/beef/core_ext/string_spec.rb
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
homepage: http://simonjones.github.io/beef
|
82
|
+
licenses: []
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.0.3
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: A Ruby core exntension library.
|
104
|
+
test_files:
|
105
|
+
- spec/beef/core_ext/hash_spec.rb
|
106
|
+
- spec/beef/core_ext/string_spec.rb
|
107
|
+
- spec/spec_helper.rb
|