positify 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/README.md +24 -0
- data/lib/positify.rb +32 -0
- data/lib/positify/version.rb +9 -0
- data/spec/lib/positify_spec.rb +46 -0
- data/spec/spec_helper.rb +1 -0
- metadata +101 -0
data/README.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
[](https://travis-ci.org/bukowskis/positify)
|
2
|
+
|
3
|
+
# Positify
|
4
|
+
|
5
|
+
Makes anything positive.
|
6
|
+
|
7
|
+
# Installation
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem install positify
|
11
|
+
```
|
12
|
+
|
13
|
+
# Examples
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
Positify.it(-5) #=> 5
|
17
|
+
Positify.it(0) #=> 1
|
18
|
+
Positify.it('huh?') #=> 1
|
19
|
+
Positify.it(Weirdo.new) #=> 1
|
20
|
+
Positify.it(5.9) #=> 5
|
21
|
+
|
22
|
+
Positify.it(30, max: 22) #=> 22
|
23
|
+
Positify.it(max: 25) { 30 } #=> 25
|
24
|
+
```
|
data/lib/positify.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'positify/version'
|
2
|
+
|
3
|
+
module Positify
|
4
|
+
|
5
|
+
def self.it(*args, &block)
|
6
|
+
if block_given?
|
7
|
+
raise(ArgumentError, "You can only pass in one options Hash in block mode, not #{args.inspect}") if args.size > 1
|
8
|
+
object = block.call
|
9
|
+
options = args.shift || {}
|
10
|
+
else
|
11
|
+
case args.size
|
12
|
+
when 1
|
13
|
+
object = args.shift
|
14
|
+
options = {}
|
15
|
+
raise(ArgumentError, "You forgot the brackets in your call. Try: Positify::it(...) do") if object.is_a?(Hash)
|
16
|
+
when 2
|
17
|
+
object = args.shift
|
18
|
+
options = args.shift
|
19
|
+
else
|
20
|
+
raise ArgumentError, "You must pass in an object and may pass in an options Hash, not #{args.inspect}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
options[:max] = it(options[:max]) if options[:max]
|
25
|
+
|
26
|
+
return 1 unless object.respond_to?(:to_i)
|
27
|
+
result = object.to_i.abs > 0 ? object.to_i.abs : 1
|
28
|
+
result = options[:max] if options[:max] && options[:max] < result
|
29
|
+
result
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
describe Positify do
|
5
|
+
|
6
|
+
describe '.it' do
|
7
|
+
it 'makes negative numbers positive' do
|
8
|
+
Positify.it(-5).should == 5
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'turns 0 to 1' do
|
12
|
+
Positify.it(0).should == 1
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'returns 1 on non Fixnumerable objects' do
|
16
|
+
Positify.it(OpenStruct.new).should == 1
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'chops Floats to Fixnums' do
|
20
|
+
Positify.it(5.9).should == 5
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'sets a ceiling' do
|
24
|
+
Positify.it(30, max: 22).should == 22
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'ignores the ceiling if the value is below it ' do
|
28
|
+
Positify.it(30, max: 35).should == 30
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'block mode' do
|
32
|
+
it 'makes negative numbers positive' do
|
33
|
+
Positify.it { -5 }.should == 5
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'sets a ceiling' do
|
37
|
+
Positify.it(max: 25) { 30 }.should == 25
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'ignores the ceiling if the value is below it ' do
|
41
|
+
Positify.it(max: 35) { 30 }.should == 30
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'positify'
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: positify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- bukowskis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-24 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
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: guard-rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rb-fsevent
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Makes anything positive. Works inline and block and you can specify a
|
63
|
+
maximum value. Zero is not positive btw.
|
64
|
+
email:
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- lib/positify/version.rb
|
70
|
+
- lib/positify.rb
|
71
|
+
- spec/lib/positify_spec.rb
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
- README.md
|
74
|
+
homepage: https://github.com/bukowskis/positify
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options:
|
79
|
+
- --encoding
|
80
|
+
- UTF-8
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 1.8.23
|
98
|
+
signing_key:
|
99
|
+
specification_version: 3
|
100
|
+
summary: Makes anything positive.
|
101
|
+
test_files: []
|