lightcore 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +86 -0
- data/Rakefile +8 -0
- data/lib/lightcore.rb +63 -0
- data/lib/lightcore/version.rb +3 -0
- data/lightcore.gemspec +24 -0
- data/test/lightcore_test.rb +89 -0
- data/test/test_helper.rb +4 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d5243e01896a5088e59cb0fe00f6f624cddaa3e9
|
4
|
+
data.tar.gz: 92552e51a9b55693c61f2447932aa38fe32cab31
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 71a0bfe970975c5e19b654e7fde77a7ad98c06c93dc98b6d8f75d162114edf5a5776828b1043d8b5670b2a1923e5a522b1ddd842d7f7037be2c36996f98b8582
|
7
|
+
data.tar.gz: a376b6bfbe02db8fa1590198b5cd14cbab6968ae6ed3de92d4ff4701336f9211c7da77a9c0bcc374d67b4ddf0c23a8ae2b9e3d86d620cad603f8bbeefe189c0e
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Andrew Kane, Nick Elser
|
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,86 @@
|
|
1
|
+
# Lightcore
|
2
|
+
|
3
|
+
Lightweight Ruby core extensions
|
4
|
+
|
5
|
+
- count_by
|
6
|
+
- hash_map
|
7
|
+
- mapk, mapk!
|
8
|
+
- mapv, mapv!
|
9
|
+
- to_b
|
10
|
+
|
11
|
+
Plays nicely with the Ruby core and [ActiveSupport](http://guides.rubyonrails.org/active_support_core_extensions.html).
|
12
|
+
|
13
|
+
## Get Started
|
14
|
+
|
15
|
+
Add this line to your application’s Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'lightcore'
|
19
|
+
```
|
20
|
+
|
21
|
+
## Methods
|
22
|
+
|
23
|
+
#### count_by
|
24
|
+
|
25
|
+
Groups and counts an array
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
users.count_by(&:country_code)
|
29
|
+
# {
|
30
|
+
# "US" => 5,
|
31
|
+
# "DE" => 2
|
32
|
+
# }
|
33
|
+
```
|
34
|
+
|
35
|
+
#### hash_map
|
36
|
+
|
37
|
+
Converts an array of key-value pairs to a hash
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
users.hash_map{|u| [u.id, u.name] }
|
41
|
+
# {
|
42
|
+
# 1 => "Nick",
|
43
|
+
# 2 => "Andrew"
|
44
|
+
# }
|
45
|
+
```
|
46
|
+
|
47
|
+
#### mapk
|
48
|
+
|
49
|
+
Updates the keys of a hash
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
hash.mapk{|k| k.to_sym }
|
53
|
+
```
|
54
|
+
|
55
|
+
`mapk!` updates the original hash
|
56
|
+
|
57
|
+
#### mapv
|
58
|
+
|
59
|
+
Updates the values of a hash
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
hash.mapv{|v| v[0] }
|
63
|
+
```
|
64
|
+
|
65
|
+
`mapv!` updates the original hash
|
66
|
+
|
67
|
+
#### to_b
|
68
|
+
|
69
|
+
Converts an object to a boolean - works great with strings
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
"true".to_b # true
|
73
|
+
"hi".to_b # true
|
74
|
+
"false".to_b # false
|
75
|
+
"f".to_b # false
|
76
|
+
nil.to_b # false
|
77
|
+
```
|
78
|
+
|
79
|
+
## Contributing
|
80
|
+
|
81
|
+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
82
|
+
|
83
|
+
- [Report bugs](https://github.com/ankane/lightcore/issues)
|
84
|
+
- Fix bugs and [submit pull requests](https://github.com/ankane/lightcore/pulls)
|
85
|
+
- Write, clarify, or fix documentation
|
86
|
+
- Suggest or add new features
|
data/Rakefile
ADDED
data/lib/lightcore.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require "lightcore/version"
|
2
|
+
|
3
|
+
module Enumerable
|
4
|
+
|
5
|
+
def count_by(&block)
|
6
|
+
inject(Hash.new(0)){|memo, obj| memo[yield(obj)] += 1; memo }
|
7
|
+
end
|
8
|
+
|
9
|
+
def hash_map(&block)
|
10
|
+
Hash[ map(&block) ]
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
class Hash
|
16
|
+
|
17
|
+
def mapk
|
18
|
+
new_h = {}
|
19
|
+
each_pair do |k, v|
|
20
|
+
new_h[yield(k)] = v
|
21
|
+
end
|
22
|
+
new_h
|
23
|
+
end
|
24
|
+
|
25
|
+
def mapk!
|
26
|
+
each_pair.to_a.each do |k, v|
|
27
|
+
self[yield(k)] = delete(k)
|
28
|
+
end
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
def mapv
|
33
|
+
new_h = {}
|
34
|
+
each_pair do |k, v|
|
35
|
+
new_h[k] = yield(v)
|
36
|
+
end
|
37
|
+
new_h
|
38
|
+
end
|
39
|
+
|
40
|
+
def mapv!
|
41
|
+
each_pair do |k, v|
|
42
|
+
self[k] = yield(v)
|
43
|
+
end
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
module Kernel
|
50
|
+
|
51
|
+
def to_b
|
52
|
+
!!self
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
class String
|
58
|
+
|
59
|
+
def to_b
|
60
|
+
!["f", "false"].include?(downcase)
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
data/lightcore.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'lightcore/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "lightcore"
|
8
|
+
spec.version = Lightcore::VERSION
|
9
|
+
spec.authors = ["Andrew Kane", "Nick Elser"]
|
10
|
+
spec.email = ["andrew@chartkick.com"]
|
11
|
+
spec.summary = %q{Lightweight Ruby core extensions}
|
12
|
+
spec.description = %q{Lightweight Ruby core extensions}
|
13
|
+
spec.homepage = "https://github.com/ankane/lightcore"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "minitest"
|
24
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class TestLightcore < Minitest::Test
|
4
|
+
|
5
|
+
def test_count_by
|
6
|
+
people = [
|
7
|
+
{name: "Andrew"},
|
8
|
+
{name: "Nick"},
|
9
|
+
{name: "Nick"}
|
10
|
+
]
|
11
|
+
expected = {
|
12
|
+
"Andrew" => 1,
|
13
|
+
"Nick" => 2
|
14
|
+
}
|
15
|
+
assert_equal expected, people.count_by{|p| p[:name] }
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_hash_map
|
19
|
+
people = [
|
20
|
+
{id: 1, name: "Andrew"},
|
21
|
+
{id: 2, name: "Nick"}
|
22
|
+
]
|
23
|
+
expected = {
|
24
|
+
1 => "Andrew",
|
25
|
+
2 => "Nick"
|
26
|
+
}
|
27
|
+
assert_equal expected, people.hash_map{|p| [p[:id], p[:name]] }
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_mapk
|
31
|
+
hash = {
|
32
|
+
"key1" => "value1",
|
33
|
+
"key2" => "value2"
|
34
|
+
}
|
35
|
+
expected = {
|
36
|
+
key1: "value1",
|
37
|
+
key2: "value2"
|
38
|
+
}
|
39
|
+
assert_equal expected, hash.mapk{|k| k.to_sym }
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_mapk!
|
43
|
+
hash = {
|
44
|
+
"key1" => "value1",
|
45
|
+
"key2" => "value2"
|
46
|
+
}
|
47
|
+
expected = {
|
48
|
+
key1: "value1",
|
49
|
+
key2: "value2"
|
50
|
+
}
|
51
|
+
hash.mapk!{|k| k.to_sym }
|
52
|
+
assert_equal expected, hash
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_mapv
|
56
|
+
hash = {
|
57
|
+
"key1" => "value1",
|
58
|
+
"key2" => "value2"
|
59
|
+
}
|
60
|
+
expected = {
|
61
|
+
"key1" => "1",
|
62
|
+
"key2" => "2"
|
63
|
+
}
|
64
|
+
assert_equal expected, hash.mapv{|v| v[-1] }
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_mapv!
|
68
|
+
hash = {
|
69
|
+
"key1" => "value1",
|
70
|
+
"key2" => "value2"
|
71
|
+
}
|
72
|
+
expected = {
|
73
|
+
"key1" => "1",
|
74
|
+
"key2" => "2"
|
75
|
+
}
|
76
|
+
hash.mapv!{|v| v[-1] }
|
77
|
+
assert_equal expected, hash
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_to_b
|
81
|
+
assert "true".to_b
|
82
|
+
assert "t".to_b
|
83
|
+
assert "hi".to_b
|
84
|
+
assert !"false".to_b
|
85
|
+
assert !"f".to_b
|
86
|
+
assert !nil.to_b
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lightcore
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Kane
|
8
|
+
- Nick Elser
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-03-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.5'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.5'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: minitest
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
description: Lightweight Ruby core extensions
|
57
|
+
email:
|
58
|
+
- andrew@chartkick.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/lightcore.rb
|
69
|
+
- lib/lightcore/version.rb
|
70
|
+
- lightcore.gemspec
|
71
|
+
- test/lightcore_test.rb
|
72
|
+
- test/test_helper.rb
|
73
|
+
homepage: https://github.com/ankane/lightcore
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.2.2
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Lightweight Ruby core extensions
|
97
|
+
test_files:
|
98
|
+
- test/lightcore_test.rb
|
99
|
+
- test/test_helper.rb
|