corelib 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/Rakefile +5 -0
- data/corelib.gemspec +24 -0
- data/lib/corelib.rb +8 -0
- data/lib/corelib/array/alias.rb +10 -0
- data/lib/corelib/array/core.rb +4 -0
- data/lib/corelib/array/math.rb +27 -0
- data/lib/corelib/boolean/false.rb +9 -0
- data/lib/corelib/boolean/true.rb +10 -0
- data/lib/corelib/numeric/core.rb +57 -0
- data/lib/corelib/string/core.rb +23 -0
- data/lib/corelib/version.rb +3 -0
- data/spec/array/core_spec.rb +16 -0
- data/spec/spec_helper.rb +7 -0
- metadata +88 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Corlew Solutions
|
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,46 @@
|
|
1
|
+
# Corelib
|
2
|
+
|
3
|
+
Many languages provide a vast library of methods or functions for developers to use, whereas Ruby only provides basic (albeit powerful) building blocks. At best, this leads to more complex applications and time wasted writing code another developer has probably already written; at worst it results in core class extensions being placed in helper methods or on objects that have no business owning the methods. The lack of a good strategy for managing Ruby extentions really becomes apparent when a developer needs to share his extensions across multiple projects.
|
4
|
+
|
5
|
+
Corelib aims to solve this problem by providing a central gem for developers to share extensions & additions to the Ruby core. Corelib focuses on:
|
6
|
+
|
7
|
+
1. Reducing Code Duplication
|
8
|
+
2. Improving Code Readability
|
9
|
+
3. Sharing Developer Knowledge
|
10
|
+
4. Reducing Errors
|
11
|
+
5. Saving Developers Time
|
12
|
+
6. Improving Code Quality & Performance
|
13
|
+
|
14
|
+
We invite all like minded developers to join us in growing the corelib library of extensions.
|
15
|
+
|
16
|
+
## Inspiration
|
17
|
+
|
18
|
+
Corelib was inspired by Smalltalk where it is quite common (and encouraged) to modify base classes with additional convience methods. In addition, Smalltalk defines many useful classes that would benefit the Ruby community such as Set, OrderedSet, SortedCollection, Dictionary, Association, etc...
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
gem 'corelib'
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install corelib
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
Browse the /lib/corelib/<class name> directory find methods that extend the chosen classes. All of these methods are now available inside your application without any additional configuration.
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
If you have developed useful extensions to the Ruby core, please consider adding them to our "little" library.
|
41
|
+
|
42
|
+
1. Fork it
|
43
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
44
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
45
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
46
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/corelib.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
#http://nithinbekal.com/2011/writing-ruby-gems-part-5-setting-up-rspec/
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'corelib/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |gem|
|
8
|
+
gem.name = "corelib"
|
9
|
+
gem.version = Corelib::VERSION
|
10
|
+
gem.authors = ["Corlew Solutions"]
|
11
|
+
gem.email = ["support@corlewsolutions.com"]
|
12
|
+
gem.description = %q{"Many languages provide a vast library of methods or functions for developers to use, whereas Ruby only provides basic (albeit powerful) building blocks. At best, this leads to more complex applications and time wasted writing code another developer has probably already written; at worst it results in core class extensions being placed in helper methods or on objects that have no business owning the methods. The lack of a good strategy for managing Ruby extentions really becomes apparent when a developer needs to share his extensions across multiple projects.
|
13
|
+
|
14
|
+
Corelib aims to solve this problem by providing a central gem for developers to share extensions & additions to the Ruby core.}
|
15
|
+
gem.summary = %q{"Useful extensions & additions to the Ruby core classes}
|
16
|
+
gem.homepage = "https://github.com/corlewsolutions/corelib.git"
|
17
|
+
|
18
|
+
gem.files = `git ls-files`.split($/)
|
19
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
20
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
21
|
+
gem.require_paths = ["lib"]
|
22
|
+
|
23
|
+
gem.add_development_dependency 'rspec'
|
24
|
+
end
|
data/lib/corelib.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# options = {:key1 => 'default'}.merge(options)
|
2
|
+
|
3
|
+
class Array
|
4
|
+
|
5
|
+
def sum (options = {})
|
6
|
+
strict = options.fetch(:strict, true)
|
7
|
+
strict ? sum_strict : sum_loose
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def sum_strict
|
13
|
+
total = 0
|
14
|
+
self.each {|item| total = total + item }
|
15
|
+
total
|
16
|
+
end
|
17
|
+
|
18
|
+
def sum_loose
|
19
|
+
total = 0
|
20
|
+
self.each do |item|
|
21
|
+
val = item.is_a?(Numeric) ? item : 0
|
22
|
+
total = total + val
|
23
|
+
end
|
24
|
+
total
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
class Numeric
|
2
|
+
|
3
|
+
#format accepts (C-Capitalized, U-Uppercase, L-Lowercase)
|
4
|
+
def to_yes_no(format="C")
|
5
|
+
(self == 1 || self == 1.0).to_yes_no(format)
|
6
|
+
end
|
7
|
+
|
8
|
+
#Assumes numeric value is in seconds
|
9
|
+
def to_days_hours_minutes_seconds
|
10
|
+
total_seconds = self.to_i
|
11
|
+
|
12
|
+
days = total_seconds / 86400
|
13
|
+
hours = (total_seconds / 3600) - (days * 24)
|
14
|
+
minutes = (total_seconds / 60) - (hours * 60) - (days * 1440)
|
15
|
+
seconds = total_seconds % 60
|
16
|
+
|
17
|
+
display = ''
|
18
|
+
display_concat = ''
|
19
|
+
if days > 0
|
20
|
+
display = display + display_concat + "#{days}d"
|
21
|
+
display_concat = ' '
|
22
|
+
end
|
23
|
+
if hours > 0 || display.length > 0
|
24
|
+
display = display + display_concat + "#{hours}h"
|
25
|
+
display_concat = ' '
|
26
|
+
end
|
27
|
+
if minutes > 0 || display.length > 0
|
28
|
+
display = display + display_concat + "#{minutes}m"
|
29
|
+
display_concat = ' '
|
30
|
+
end
|
31
|
+
display = display + display_concat + "#{seconds}s"
|
32
|
+
display
|
33
|
+
end
|
34
|
+
|
35
|
+
#Assumes numeric value is in seconds
|
36
|
+
def to_hours_minutes(if_zero="")
|
37
|
+
total_seconds = self.to_i
|
38
|
+
|
39
|
+
return if_zero if total_seconds < 60
|
40
|
+
|
41
|
+
hours = (total_seconds / 3600)
|
42
|
+
minutes = (total_seconds / 60) - (hours * 60)
|
43
|
+
|
44
|
+
display = ''
|
45
|
+
display_concat = ''
|
46
|
+
|
47
|
+
if hours > 0
|
48
|
+
display = display + "#{hours}h"
|
49
|
+
display_concat = ' '
|
50
|
+
end
|
51
|
+
if minutes > 0 || display.length > 0
|
52
|
+
display = display + display_concat + "#{minutes}m"
|
53
|
+
end
|
54
|
+
display
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class String
|
2
|
+
|
3
|
+
#format accepts (C-Capitalized, U-Uppercase, L-Lowercase)
|
4
|
+
def to_yes_no(format="C", strict=false)
|
5
|
+
self.to_bool(strict).to_yes_no(format)
|
6
|
+
end
|
7
|
+
|
8
|
+
#true will always be returned if we can clearly match one of the true cases
|
9
|
+
#In unstrict mode, the string is assumed false if we cannot match true
|
10
|
+
#In strict mode, the string must clearly match a false condition to return false
|
11
|
+
#otherise an error is raised
|
12
|
+
def to_bool(strict=false)
|
13
|
+
return true if self =~ (/\A(true|t|yes|y|1)\Z/i)
|
14
|
+
|
15
|
+
if strict
|
16
|
+
return false if self.empty? || self =~ (/\A(false|f|no|n|0)\Z/i)
|
17
|
+
raise ArgumentError.new("cannot convert \"#{self}\" to boolean")
|
18
|
+
end
|
19
|
+
|
20
|
+
false
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
describe "#sum" do
|
5
|
+
it 'sums correctly with all positive or all negative numbers' do
|
6
|
+
[0, 1,2,3,10,100, 5].sum.should == 121
|
7
|
+
[0, -1,-2,-3, -10, -100, -6].sum.should == -122
|
8
|
+
end
|
9
|
+
it 'sums correctly with letters & numbers in loose mode' do
|
10
|
+
[0, "a","should be 0","", nil, 3, 9].sum({:strict => false}).should == 12
|
11
|
+
end
|
12
|
+
it 'throws an error with letters & numbers in strict mode' do
|
13
|
+
lambda {[0, "a","should be 0","", nil, 3, 9].sum({:strict => true})}.should raise_error TypeError
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: corelib
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Corlew Solutions
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: ! "\"Many languages provide a vast library of methods or functions for
|
31
|
+
developers to use, whereas Ruby only provides basic (albeit powerful) building blocks.
|
32
|
+
At best, this leads to more complex applications and time wasted writing code another
|
33
|
+
developer has probably already written; at worst it results in core class extensions
|
34
|
+
being placed in helper methods or on objects that have no business owning the methods.
|
35
|
+
The lack of a good strategy for managing Ruby extentions really becomes apparent
|
36
|
+
when a developer needs to share his extensions across multiple projects.\n\n Corelib
|
37
|
+
aims to solve this problem by providing a central gem for developers to share extensions
|
38
|
+
& additions to the Ruby core."
|
39
|
+
email:
|
40
|
+
- support@corlewsolutions.com
|
41
|
+
executables: []
|
42
|
+
extensions: []
|
43
|
+
extra_rdoc_files: []
|
44
|
+
files:
|
45
|
+
- .gitignore
|
46
|
+
- Gemfile
|
47
|
+
- LICENSE.txt
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- corelib.gemspec
|
51
|
+
- lib/corelib.rb
|
52
|
+
- lib/corelib/array/alias.rb
|
53
|
+
- lib/corelib/array/core.rb
|
54
|
+
- lib/corelib/array/math.rb
|
55
|
+
- lib/corelib/boolean/false.rb
|
56
|
+
- lib/corelib/boolean/true.rb
|
57
|
+
- lib/corelib/numeric/core.rb
|
58
|
+
- lib/corelib/string/core.rb
|
59
|
+
- lib/corelib/version.rb
|
60
|
+
- spec/array/core_spec.rb
|
61
|
+
- spec/spec_helper.rb
|
62
|
+
homepage: https://github.com/corlewsolutions/corelib.git
|
63
|
+
licenses: []
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.8.25
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: ! '"Useful extensions & additions to the Ruby core classes'
|
86
|
+
test_files:
|
87
|
+
- spec/array/core_spec.rb
|
88
|
+
- spec/spec_helper.rb
|