spiel 0.0.1

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,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .rspec
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in spiel.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jordi Carres
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,82 @@
1
+ # Spiel
2
+
3
+ Spiel has helper methods and classes to make your code clean.
4
+ The main concepts come from Scala and Haskell and other languages which
5
+ has embraced the functional paradigm in different degrees
6
+
7
+ It contains very lightweight solutions with no extra dependencies on
8
+ other gems.
9
+
10
+ Supports Ruby 1.9.2 and up
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'spiel'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install spiel
25
+
26
+ ## Usage
27
+
28
+ ### Maybe
29
+
30
+ Ruby's maybe inspired by Haskell's Maybe.
31
+
32
+ Imagine you want to do know who of your friends boss is richer:
33
+ ```Ruby
34
+ User.find_by_name(name).boss.credit_card(:main).balance(:credit)
35
+ ```
36
+
37
+ But find_by_name, parent, credit_card or balance can be nil, you will
38
+ get a NoMethodError and your program will die.
39
+
40
+ You can check for nil on each step:
41
+ ```Ruby
42
+ user = User.find_by_name(name)
43
+ parent &&= user.parent
44
+ card &&= parent.credit_card(:main)
45
+ balance &&= card.balance(:credit)
46
+ ```
47
+
48
+ But your buggy first version read almost like English and now this looks so
49
+ much .... like C.
50
+
51
+ You can also use ActiveSupport and its try method:
52
+ ```Ruby
53
+ User.find_by_name(name).try(:parent).try(:credit_card, :main).try(:balance, :credit)
54
+ ```
55
+
56
+ That was long! And it kind of repetitive and, what is going on with
57
+ those double symbols on these methods. Ugly, ugly.
58
+
59
+ So you try this gem and you get this awesome line:
60
+ ```Ruby
61
+ Maybe(User.find_by_name(name)).parent.credit_card(:main).balance(:credit)
62
+ ```
63
+
64
+ And that's it, this will get you an object that works as balance and the
65
+ chain will never explote because of some nil on the line.
66
+
67
+ You can even do
68
+ ```Ruby
69
+ substracted = Maybe(User.find_by_name(name)).parent.credit_card(:main).balance(:credit).get_or_else(0)
70
+ ```
71
+
72
+ Maybe objects has one method only 'get_or_else' it will return the
73
+ object at that point of your chain or some default you pass as param if the chain got
74
+ broken
75
+
76
+
77
+
78
+ ## Contributing
79
+
80
+ 1. Fork it
81
+ 2. Do your changes, the specs pass
82
+ 3. Send a pull request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/spiel.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'spiel/version'
2
+ require 'spiel/maybe'
@@ -0,0 +1,46 @@
1
+ module Spiel
2
+ require 'singleton'
3
+
4
+ class Just
5
+ def initialize(object)
6
+ @object = object
7
+ end
8
+
9
+ def method_missing(meth, *args, &block)
10
+ Maybe(@object.send(meth, *args, &block))
11
+ end
12
+
13
+ def get_or_else(value)
14
+ @object
15
+ end
16
+ end
17
+
18
+ class NothingClass
19
+ include Singleton
20
+
21
+ def method_missing(meth, *args, &block)
22
+ Nothing
23
+ end
24
+
25
+ def get_or_else(value)
26
+ value
27
+ end
28
+
29
+ def nil?
30
+ true
31
+ end
32
+
33
+ def to_s
34
+ "Nothing"
35
+ end
36
+
37
+ end
38
+
39
+ # only one Nothing in the system, saving resources FTW
40
+ Nothing = NothingClass.instance
41
+ end
42
+
43
+ def Maybe(object)
44
+ object.nil? ? Spiel::Nothing : Spiel::Just.new(object)
45
+ end
46
+
@@ -0,0 +1,3 @@
1
+ module Spiel
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ User = Struct.new(:name, :nickname, :father)
4
+
5
+ describe "Maybe" do
6
+ let(:father) { User.new("Truusmis", nil, nil) }
7
+ let(:gincho) { User.new("John", "gincho", father) }
8
+
9
+ context "With nil object" do
10
+ let(:name) {nil}
11
+ subject { Maybe(name) }
12
+ its(:nil?) { should be_true }
13
+ its(:class) { should == Spiel::NothingClass }
14
+ it "should return the default value for nil" do
15
+ expect( subject.get_or_else("") ).to eq("")
16
+ end
17
+ end
18
+
19
+ context "With nil in the chain" do
20
+ subject { Maybe(gincho).father.nickname.gsub('uu', 'u') }
21
+ its(:nil?) { should be_true }
22
+ its(:class) { should == Spiel::NothingClass }
23
+ it "should return the default value for nil" do
24
+ expect( subject.get_or_else("") ).to eq("")
25
+ end
26
+ end
27
+
28
+ context "With valid objects in the chain" do
29
+ subject { Maybe(gincho).father.name.gsub('uu', 'u') }
30
+ its(:nil?) { should be_false }
31
+ its(:class) { should == Spiel::Just }
32
+ it "should return the value of the object" do
33
+ expect( subject.get_or_else("") ).to eq("Trusmis")
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,10 @@
1
+
2
+ $:.unshift(File.expand_path('../lib', File.dirname(__FILE__)))
3
+ require 'spiel.rb'
4
+
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+ config.order = 'random'
10
+ end
data/spiel.gemspec ADDED
@@ -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 'spiel/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "spiel"
8
+ gem.version = Spiel::VERSION
9
+ gem.authors = ["Jordi Polo Carres"]
10
+ gem.email = ["mumismo@gmail.com"]
11
+ gem.description = "Methods and utilities to help you write clean code"
12
+ gem.summary = "Methods inspired by functional languages to DRY, simplify and make code more beautiful"
13
+ gem.homepage = "https://github.com/JordiPolo/spiel"
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 "rspec"
21
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spiel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jordi Polo Carres
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-26 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: Methods and utilities to help you write clean code
31
+ email:
32
+ - mumismo@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - .travis.yml
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/spiel.rb
44
+ - lib/spiel/maybe.rb
45
+ - lib/spiel/version.rb
46
+ - spec/maybe_spec.rb
47
+ - spec/spec_helper.rb
48
+ - spiel.gemspec
49
+ homepage: https://github.com/JordiPolo/spiel
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.24
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Methods inspired by functional languages to DRY, simplify and make code more
73
+ beautiful
74
+ test_files:
75
+ - spec/maybe_spec.rb
76
+ - spec/spec_helper.rb
77
+ has_rdoc: