lxa_core_extensions 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lxa_core_extensions.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Levin Alexander
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,29 @@
1
+ # LxaCoreExtensions
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'lxa_core_extensions'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install lxa_core_extensions
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'test'
6
+ end
7
+
8
+
9
+ task :default => [:test]
10
+
data/hash_spec.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Hash#flip" do
4
+ it "should work" do
5
+ {1=>2, 3=>4, 5=>6, 6=>2, 7=>4}.flip.should == {2=>[1, 6], 4=>[3, 7], 6=>[5]}
6
+ end
7
+ end
8
+
9
+
10
+ describe "Hash#compact" do
11
+ it "should compact without block" do
12
+ { 4=>35, 9=>nil, 10=>false }.compact.should == {4=>35, 10=>false}
13
+ end
14
+
15
+ it "should compact with block" do
16
+ { 4=>35, 9=>nil, 10=>false }.compact(&:blank?).should == {4=>35}
17
+ end
18
+ end
19
+
20
+ describe "Hash#rekey" do
21
+ it "should work" do
22
+ {1=>2, 3=>4, 5=>{6=>7, 8=>9}}.rekey(&:to_s).should == {"1"=>2,"3"=>4, "5"=>{"6"=>7, "8"=>9}}
23
+ end
24
+ end
25
+
26
+
27
+ describe "Hash#nested_keys" do
28
+ it "should work" do
29
+ {1=>2, 3=>4, 5=>{6=>7, 8=> {9=>10}}}.nested_keys.to_a.should == [1,3,5,6,8,9]
30
+ end
31
+
32
+ end
33
+
@@ -0,0 +1,9 @@
1
+ require "lxa_core_extensions/version"
2
+
3
+ require "lxa_core_extensions/enumerable"
4
+ require "lxa_core_extensions/hash"
5
+ require "lxa_core_extensions/array"
6
+ require "lxa_core_extensions/constants"
7
+
8
+ module LxaCoreExtensions
9
+ end
@@ -0,0 +1,6 @@
1
+ class Array
2
+ def only
3
+ raise "called Array#only with array of length #{length}" if length != 1
4
+ first
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ # define the global constants Infinity and NaN
2
+
3
+ Infinity = 1/0.0 unless defined?(Infinity)
4
+ NaN = 0.0/0.0 unless defined?(NaN)
5
+
@@ -0,0 +1,37 @@
1
+ module Enumerable
2
+
3
+ # shorthand for the common idiom of using inject to populate a hash
4
+ # from an Enumerable
5
+ #
6
+ # [[1,2],[3,4],[5,6]].build_hash #=> {1=>2, 3=>4, 5=>6}
7
+ # [[1,2],[3,4],[5,6]].build_hash(:last) #=> {2=>[1,2], 4=>[...],...}
8
+ #
9
+ # [1,2,3].build_hash { |h,k| h[k] = k } #=> {1=>1, 2=>2, 3=>3}
10
+ #
11
+ # # the last example is identical to:
12
+ #
13
+ # [1,2,3].inject({}) { |h,k| h[k] = k; h }
14
+ #
15
+ def build_hash(hsh_or_action = {})
16
+ if block_given?
17
+ raise ArgumentError if hsh_or_action.is_a?(Symbol)
18
+
19
+ inject(hsh_or_action) do |h,*args|
20
+ yield h, *args
21
+ h
22
+ end
23
+ else
24
+ hsh, action = *if hsh_or_action.is_a?(Symbol)
25
+ [{}, hsh_or_action]
26
+ else
27
+ [hsh_or_action, nil]
28
+ end
29
+
30
+ if action
31
+ build_hash(hsh) { |h,v| k = v.send(action); h[k] = v }
32
+ else
33
+ build_hash(hsh) { |h,(k,v)| h[k] = v }
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,52 @@
1
+ require 'ostruct'
2
+
3
+ class Hash
4
+
5
+ # { 4=>35, 9=>nil, 10=>false }.compact #=> {4=>35, 10=>false}
6
+ # { 4=>35, 9=>nil, 10=>false }.compact(&:blank?) #=> {4=>35}
7
+ #
8
+ def compact
9
+ reject { |k,v| block_given? ? yield(v) : v.nil? }
10
+ end
11
+
12
+ # make a new hash with the existing values as the new keys
13
+ # - like #rassoc for each existing key
14
+ # - like invert but with *all* keys as array
15
+ #
16
+ # {1=>2, 3=>4, 5=>6, 6=>2, 7=>4}.flip #=> {2=>[1, 6], 4=>[3, 7], 6=>[5]}
17
+ #
18
+ def flip
19
+ build_hash { |h,(k,v)| h[v] ||= []; h[v] << k }
20
+ end
21
+
22
+
23
+ # change all the keys of the has by yielding it to the supplied block, recursively
24
+ #
25
+ # {1=>2, 3=>4, 5=>{6=>7, 8=>9}}.rekey(&:to_s) #=> {"1"=>2,"3"=>4, "5"=>{"6"=>7, "8"=>9}}
26
+ #
27
+ def rekey(&block)
28
+ build_hash do |h,(k,v)|
29
+ new_key = yield(k)
30
+ new_val = v.is_a?(Hash) ? v.rekey(&block) : v
31
+
32
+ h[new_key] = new_val
33
+ end
34
+ end
35
+
36
+ # return all the keys of all the sub hashes
37
+ #
38
+ def nested_keys(&block)
39
+ Enumerator.new do |y|
40
+ to_a.each do |k,v|
41
+ y.yield k
42
+ v.nested_keys { |nk| y.yield(nk) } if v.is_a?(Hash)
43
+ end
44
+ end.each(&block)
45
+ end
46
+
47
+
48
+ def to_ostruct
49
+ OpenStruct.new(self)
50
+ end
51
+ end
52
+
@@ -0,0 +1,3 @@
1
+ module LxaCoreExtensions
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lxa_core_extensions/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "lxa_core_extensions"
8
+ gem.version = LxaCoreExtensions::VERSION
9
+ gem.authors = ["Levin Alexander"]
10
+ gem.email = ["mail@levinalex.net"]
11
+ gem.description = "simple core extensions"
12
+ gem.summary = "simple utility methods like Hash#compact and Enumerable#build_hash"
13
+ gem.homepage = ""
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
+ gem.add_development_dependency "minitest"
21
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+
4
+ require 'lxa_core_extensions'
@@ -0,0 +1,18 @@
1
+ require 'helper'
2
+
3
+ describe "Array#only" do
4
+ it "should raise on multi element arrays" do
5
+ assert_raises(RuntimeError) do
6
+ [1,2,3].only
7
+ end
8
+
9
+ assert_raises(RuntimeError) do
10
+ [].only
11
+ end
12
+ end
13
+
14
+ it "should return first element" do
15
+ assert_equal(12, [12].only)
16
+ end
17
+
18
+ end
@@ -0,0 +1,9 @@
1
+ require 'helper'
2
+
3
+ describe "Constants" do
4
+ it "should have defined constants" do
5
+ assert_equal 1/0.0, Infinity
6
+ assert defined?(NaN)
7
+ end
8
+
9
+ end
@@ -0,0 +1,21 @@
1
+ require 'helper'
2
+
3
+ describe "Enumerable#build_hash" do
4
+ it "should return correct things" do
5
+ assert_equal({1=>2, 3=>4, 5=>6},
6
+ [[1,2],[3,4],[5,6]].build_hash)
7
+
8
+ assert_equal( {2=>[1,2], 4=>[3,4],6=>[5,6]},
9
+ [[1,2],[3,4],[5,6]].build_hash(:last))
10
+
11
+ assert_equal({1=>1, 2=>2, 3=>3},
12
+ [1,2,3].build_hash { |h,k| h[k] = k })
13
+ end
14
+
15
+ it "should raise an exception when called with symbol and block" do
16
+ assert_raises(ArgumentError) do
17
+ [].build_hash(:last) { |s,v| "#{v}x" }
18
+ end
19
+ end
20
+ end
21
+
data/test/test_hash.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'helper'
2
+
3
+ describe "Hash#flip" do
4
+ it "should work" do
5
+ assert_equal({2=>[1, 6], 4=>[3, 7], 6=>[5]},
6
+ {1=>2, 3=>4, 5=>6, 6=>2, 7=>4}.flip)
7
+ end
8
+ end
9
+
10
+
11
+ describe "Hash#compact" do
12
+ it "should compact without block" do
13
+ assert_equal({4=>35, 10=>false},
14
+ { 4=>35, 9=>nil, 10=>false }.compact)
15
+ end
16
+
17
+ it "should compact with block" do
18
+ assert_equal({9=>[1]},
19
+ { 4=>[], 9=>[1], 10=>{} }.compact(&:empty?))
20
+ end
21
+ end
22
+
23
+ describe "Hash#rekey" do
24
+ it "should work" do
25
+ assert_equal({"1"=>2,"3"=>4, "5"=>{"6"=>7, "8"=>9}},
26
+ {1=>2, 3=>4, 5=>{6=>7, 8=>9}}.rekey(&:to_s))
27
+ end
28
+ end
29
+
30
+
31
+ describe "Hash#nested_keys" do
32
+ it "should work" do
33
+ assert_equal([1,3,5,6,8,9],
34
+ {1=>2, 3=>4, 5=>{6=>7, 8=> {9=>10}}}.nested_keys.to_a)
35
+ end
36
+
37
+ end
38
+
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lxa_core_extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Levin Alexander
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
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: simple core extensions
31
+ email:
32
+ - mail@levinalex.net
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - hash_spec.rb
43
+ - lib/lxa_core_extensions.rb
44
+ - lib/lxa_core_extensions/array.rb
45
+ - lib/lxa_core_extensions/constants.rb
46
+ - lib/lxa_core_extensions/enumerable.rb
47
+ - lib/lxa_core_extensions/hash.rb
48
+ - lib/lxa_core_extensions/version.rb
49
+ - lxa_core_extensions.gemspec
50
+ - test/helper.rb
51
+ - test/test_array.rb
52
+ - test/test_constants.rb
53
+ - test/test_enumerable.rb
54
+ - test/test_hash.rb
55
+ homepage: ''
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.24
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: simple utility methods like Hash#compact and Enumerable#build_hash
79
+ test_files:
80
+ - test/helper.rb
81
+ - test/test_array.rb
82
+ - test/test_constants.rb
83
+ - test/test_enumerable.rb
84
+ - test/test_hash.rb
85
+ has_rdoc: