enumeradical 0.9.0

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.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ -I lib/enumeradical
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3@enumeradical
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+
7
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in enumeradical.gemspec
4
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,78 @@
1
+ [![Build Status](https://secure.travis-ci.org/coreyhaines/enumeradical.png)](http://travis-ci.org/coreyhaines/enumeradical)
2
+
3
+ # Enumeradical
4
+ ## A most amazing collection of useful functions filling common tasks when iterating over collections
5
+
6
+ ## What is this?
7
+ I love enumerable. I really do. I use the functions it provides with the utmost alacrity. Nothing makes me sadder than seeing a #each used to populate an array. Once you start using them a lot in production systems, you notice a bunch of common patterns.
8
+
9
+ ## How do I use it?
10
+ Install it, then
11
+ require 'enumeradical'
12
+
13
+ ###I have an array of objects, and I need to convert them to another type.
14
+
15
+ class MyNumberPresenter
16
+ def initialize(number)
17
+ @number = number
18
+ end
19
+ end
20
+
21
+ [1,2,3].map { |number| MyNumberPresenter.new(number) }
22
+ # => [#<MyNumberPresenter:0x0000010086b9c8 @number=1>, #<MyNumberPresenter:0x0000010086b630 @number=2>,
23
+ #<MyNumberPresenter:0x0000010086b540 @number=3>]
24
+
25
+ NO MORE! Use Enumerable#map_to(type)
26
+
27
+ class MyNumberPresenter
28
+ def initialize(number)
29
+ @number = number
30
+ end
31
+ end
32
+
33
+ [1,2,3].map_to MyNumberPresenter
34
+ # => [#<MyNumberPresenter:0x0000010086b9c8 @number=1>, #<MyNumberPresenter:0x0000010086b630 @number=2>,
35
+ #<MyNumberPresenter:0x0000010086b540 @number=3>]
36
+
37
+ ###I have an array of objects, and I want to map them to the value they give from indexing into another object.
38
+
39
+ require 'date'
40
+ [1,2,3].map { |index| Date::ABBR_DAYNAMES[index] } # => ["Mon", "Tue", "Wed"]
41
+
42
+ NO MORE! Use Enumerable#map_into
43
+
44
+ require 'date'
45
+ [1,2,3].map_into Date::ABBR_DAYNAMES # => ["Mon", "Tue", "Wed"]
46
+
47
+
48
+ ###I have an array of objects, and I'd like to convert them using a given object's method.
49
+
50
+ class Converter
51
+ def hellos(times)
52
+ "hello"*times
53
+ end
54
+ end
55
+
56
+ converter = Converter.new
57
+
58
+ [1,2,3].map { |times| converter.hellos(times) }
59
+ # => ["hello", "hellohello", "hellohellohello"]
60
+
61
+ NO MORE! Use Object#map_over
62
+
63
+ class Converter
64
+ def hellos(times)
65
+ "hello"*times
66
+ end
67
+ end
68
+
69
+ converter = Converter.new
70
+
71
+ converter.map_over [1,2,3], :hellos
72
+ # => ["hello", "hellohello", "hellohellohello"]
73
+
74
+ ## Is this useful?
75
+ YES!!!!! Use it.
76
+
77
+ ## Who built this
78
+ Originally, [Corey Haines](http://github.com/coreyhaines) and [Ryan Briones](http://github.com/ryanbriones)
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "enumeradical/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "enumeradical"
7
+ s.version = Enumeradical::VERSION
8
+ s.authors = ["coreyhaines"]
9
+ s.email = ["coreyhaines@gmail.com"]
10
+ s.summary = %q{Enumeradical takes bland enumerable functions and uses them to create AMAZINGALITY!}
11
+ s.description = %q{Enumeradical is a collection of useful functions for iterating over collections in common ways.}
12
+
13
+ s.rubyforge_project = "enumeradical"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ # specify any dependencies here; for example:
21
+ s.add_development_dependency "travis-lint"
22
+ s.add_development_dependency "rspec"
23
+ end
@@ -0,0 +1,19 @@
1
+ require 'enumeradical/core_extensions/object'
2
+
3
+ module Enumerable
4
+ def map_into(into = nil)
5
+ do_map into, :[]
6
+ end
7
+
8
+ def map_to(to = nil)
9
+ do_map to, :new
10
+ end
11
+
12
+ private
13
+ def do_map(obj, method)
14
+ return self unless obj
15
+ raise ArgumentError, "argument must respond to #{method}" unless obj.respond_to?(method)
16
+ obj.map_over(self, method)
17
+ end
18
+ end
19
+
@@ -0,0 +1,11 @@
1
+ module Enumeradical
2
+ module CoreExtensions
3
+ module Object
4
+ def map_over(this_list, with_this_method)
5
+ return [] unless this_list
6
+ raise ArgumentError.new("given list must support map") unless this_list.respond_to?(:map)
7
+ this_list.map &method(with_this_method)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Enumeradical
2
+ VERSION = "0.9.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ require_relative "enumeradical/version"
2
+ require_relative "enumeradical/core_extensions/object"
3
+ require_relative "enumeradical/core_extensions/enumerable"
4
+
5
+ module Enumeradical
6
+ end
7
+ Object.send :include, Enumeradical::CoreExtensions::Object
@@ -0,0 +1,61 @@
1
+ require "core_extensions/enumerable"
2
+ Object.send :include, Enumeradical::CoreExtensions::Object
3
+
4
+ describe "CoreExtensions::Enumerable" do
5
+ describe "#map_into" do
6
+ example "empty array maps into empty array" do
7
+ [].map_into.should be_empty
8
+ end
9
+
10
+ example "not passing an object is considered identity map" do
11
+ [1].map_into.should == [1]
12
+ end
13
+
14
+ it "looks up the value from given object" do
15
+ [1].map_into({1 => "foo"}).should == ["foo"]
16
+ end
17
+
18
+ context "invalid situations" do
19
+ context "mapping into a non-indexable object" do
20
+ it "raises ArgumentError" do
21
+ lambda {
22
+ [1].map_into(Object.new)
23
+ }.should raise_error(ArgumentError, "argument must respond to []")
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ describe "#map_to" do
30
+ class ObjectWithOnConstructorArgument
31
+ attr_accessor :arg
32
+ def initialize(arg)
33
+ @arg = arg
34
+ end
35
+ end
36
+
37
+ example "empty array maps into empty array" do
38
+ [].map_to.should be_empty
39
+ end
40
+
41
+ example "not passing an object is considered identity map" do
42
+ [1].map_to.should == [1]
43
+ end
44
+
45
+ it "instantiates the given object with item from array" do
46
+ created_object = [2].map_to(ObjectWithOnConstructorArgument).first
47
+ created_object.arg.should == 2
48
+ end
49
+
50
+ context "invalid situations" do
51
+ context "mapping into a object without .new" do
52
+ it "raises ArgumentError" do
53
+ lambda {
54
+ [1].map_to(Object.new)
55
+ }.should raise_error(ArgumentError, "argument must respond to new")
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+
@@ -0,0 +1,34 @@
1
+ require 'core_extensions/object'
2
+
3
+ describe "CoreExtensions::Object" do
4
+ class MixedIn
5
+ include Enumeradical::CoreExtensions::Object
6
+ def multiple_by_two(number)
7
+ number*2
8
+ end
9
+ end
10
+
11
+ describe "#map_over" do
12
+ let(:object) { MixedIn.new }
13
+ let(:list) { [1,2,3] }
14
+ it "maps the method over each element of the given list" do
15
+ object.map_over(list, :multiple_by_two).should == [2,4,6]
16
+ end
17
+
18
+ it "supports passing a string as the name of the method" do
19
+ object.map_over(list, "multiple_by_two").should == [2,4,6]
20
+ end
21
+
22
+ context "invalid usage" do
23
+ it "requires a list that can be iterated over" do
24
+ lambda {
25
+ object.map_over(Object.new, :multiple_by_two).should == [2,4,6]
26
+ }.should raise_error(ArgumentError, "given list must support map")
27
+ end
28
+
29
+ it "returns an empty list if list is nil" do
30
+ object.map_over(nil, :multiple_by_two).should be_empty
31
+ end
32
+ end
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enumeradical
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - coreyhaines
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: travis-lint
16
+ requirement: &2152294480 !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: *2152294480
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &2152293900 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2152293900
36
+ description: Enumeradical is a collection of useful functions for iterating over collections
37
+ in common ways.
38
+ email:
39
+ - coreyhaines@gmail.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - .rspec
46
+ - .rvmrc
47
+ - .travis.yml
48
+ - Gemfile
49
+ - README.markdown
50
+ - Rakefile
51
+ - enumeradical.gemspec
52
+ - lib/enumeradical.rb
53
+ - lib/enumeradical/core_extensions/enumerable.rb
54
+ - lib/enumeradical/core_extensions/object.rb
55
+ - lib/enumeradical/version.rb
56
+ - spec/core_extensions/enumerable_spec.rb
57
+ - spec/core_extensions/object_spec.rb
58
+ homepage:
59
+ licenses: []
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project: enumeradical
78
+ rubygems_version: 1.8.10
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Enumeradical takes bland enumerable functions and uses them to create AMAZINGALITY!
82
+ test_files:
83
+ - spec/core_extensions/enumerable_spec.rb
84
+ - spec/core_extensions/object_spec.rb