pebbles-php_cond 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,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 pebbles-php_cond.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 do-aki
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,67 @@
1
+ # Pebbles::PhpCond
2
+
3
+ PhpCond is emulate PHP condition in ruby
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'pebbles-php_cond'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install pebbles-php_cond
19
+
20
+ ## Usage
21
+
22
+ ### bool method
23
+ cast to boolean that used if statements in php.
24
+
25
+ ```ruby
26
+ puts Pebbles::PhpCond.bool(1) # true
27
+
28
+ puts Pebbles::PhpCond.bool(0) # false
29
+ puts Pebbles::PhpCond.bool('0') # false
30
+ puts Pebbles::PhpCond.bool('') # false
31
+ puts Pebbles::PhpCond.bool([]) # false
32
+ ```
33
+
34
+ ### equal? method
35
+ emulate php's equal (==) operator.
36
+
37
+ ```ruby
38
+ puts Pebbles::equal?(0, 0) # true
39
+ puts Pebbles::equal?('0', 0) # true
40
+ puts Pebbles::equal?('0', false) # true
41
+ puts Pebbles::equal?('0', nil) # false
42
+ puts Pebbles::equal?(nil, false) # true
43
+ puts Pebbles::equal?(nil, []) # true
44
+ ```
45
+
46
+
47
+ ### identical? method
48
+ emulate php's identical (===) operator.
49
+
50
+ ```ruby
51
+ puts Pebbles::identical?(0, 0) # true
52
+ puts Pebbles::identical?('0', 0) # false
53
+ puts Pebbles::identical?('0', false) # false
54
+ puts Pebbles::identical?('0', nil) # false
55
+ puts Pebbles::identical?(nil, false) # false
56
+ puts Pebbles::identical?(nil, []) # false
57
+ ```
58
+
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ B
67
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,42 @@
1
+ module Pebbles
2
+ module PhpCond
3
+ def self.bool(value)
4
+ return false if value.nil? || value == false || value == 0 || value == 0.0
5
+ return value != '' && value != '0' if value.is_a?(String)
6
+ return value.count != 0 if value.respond_to?(:count)
7
+ return value.to_s.size != 0
8
+ end
9
+
10
+ def self.equal?(rhs, lhs)
11
+ return true if rhs == lhs
12
+
13
+ return bool(lhs) if rhs == true
14
+ return bool(rhs) if lhs == true
15
+
16
+ return !bool(lhs) if rhs == false
17
+ return !bool(rhs) if lhs == false
18
+
19
+ return equal_as_numeric(rhs, lhs) if rhs.is_a?(Numeric)
20
+ return equal_as_numeric(lhs, rhs) if lhs.is_a?(Numeric)
21
+
22
+ return !(lhs == '0' || bool(lhs)) if (rhs.nil?)
23
+ return !(rhs == '0' || bool(rhs)) if (lhs.nil?)
24
+
25
+ return false if rhs.is_a?(String) || lhs.is_a?(String)
26
+
27
+ return true if rhs.is_a?(Enumerable) && lhs.is_a?(Enumerable) && rhs.count == 0 && lhs.count == 0
28
+
29
+ return false;
30
+ end
31
+
32
+ def self.identical?(rhs, lhs)
33
+ return rhs == lhs
34
+ end
35
+
36
+ private
37
+ def self.equal_as_numeric(own, other)
38
+ return other.respond_to?(:to_f) && other.to_f == own || other.respond_to?(:to_i) && other.to_i == own
39
+ end
40
+ end
41
+ end
42
+
@@ -0,0 +1,5 @@
1
+ module Pebbles
2
+ module PhpCond
3
+ VERSION = '0.0.1'
4
+ end
5
+ 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 'pebbles-php_cond/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "pebbles-php_cond"
8
+ gem.version = Pebbles::PhpCond::VERSION
9
+ gem.authors = ["do-aki"]
10
+ gem.email = ["do.hiroaki@gmail.com"]
11
+ gem.description = %q{emulate PHP condition}
12
+ gem.summary = %q{emulate PHP condition}
13
+ gem.homepage = "https://github.com/do-aki/pebbles-php_cond"
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
@@ -0,0 +1,218 @@
1
+ require 'pebbles-php_cond'
2
+ include Pebbles
3
+
4
+ describe Pebbles::PhpCond do
5
+ it 'expr' do
6
+ PhpCond::bool(0).should be false
7
+ PhpCond::bool(0.0).should be false
8
+ PhpCond::bool('').should be false
9
+ PhpCond::bool([]).should be false
10
+ PhpCond::bool({}).should be false
11
+ PhpCond::bool(false).should be false
12
+ PhpCond::bool(nil).should be false
13
+ PhpCond::bool("0").should be false
14
+
15
+ PhpCond::bool(true).should be true
16
+ PhpCond::bool(1).should be true
17
+ PhpCond::bool("abc").should be true
18
+ PhpCond::bool("0abc").should be true
19
+ PhpCond::bool("0.0").should be true
20
+ PhpCond::bool("01").should be true
21
+ end
22
+
23
+ it 'equal_with_true' do
24
+ PhpCond::equal?(true, true ).should be true
25
+ PhpCond::equal?(true, false).should be false
26
+ PhpCond::equal?(true, 1 ).should be true
27
+ PhpCond::equal?(true, 0 ).should be false
28
+ PhpCond::equal?(true, -1 ).should be true
29
+ PhpCond::equal?(true, '1' ).should be true
30
+ PhpCond::equal?(true, '0' ).should be false
31
+ PhpCond::equal?(true, '-1' ).should be true
32
+ PhpCond::equal?(true, nil ).should be false
33
+ PhpCond::equal?(true, [] ).should be false
34
+ PhpCond::equal?(true, 'php').should be true
35
+ PhpCond::equal?(true, '' ).should be false
36
+ end
37
+
38
+ it 'equal_with_false' do
39
+ PhpCond::equal?(false, true ).should be false
40
+ PhpCond::equal?(false, false).should be true
41
+ PhpCond::equal?(false, 1 ).should be false
42
+ PhpCond::equal?(false, 0 ).should be true
43
+ PhpCond::equal?(false, -1 ).should be false
44
+ PhpCond::equal?(false, '1' ).should be false
45
+ PhpCond::equal?(false, '0' ).should be true
46
+ PhpCond::equal?(false, '-1' ).should be false
47
+ PhpCond::equal?(false, nil ).should be true
48
+ PhpCond::equal?(false, [] ).should be true
49
+ PhpCond::equal?(false, 'php').should be false
50
+ PhpCond::equal?(false, '' ).should be true
51
+ end
52
+
53
+ it 'equal_with_1' do
54
+ PhpCond::equal?(1, true ).should be true
55
+ PhpCond::equal?(1, false).should be false
56
+ PhpCond::equal?(1, 1 ).should be true
57
+ PhpCond::equal?(1, 0 ).should be false
58
+ PhpCond::equal?(1, -1 ).should be false
59
+ PhpCond::equal?(1, '1' ).should be true
60
+ PhpCond::equal?(1, '0' ).should be false
61
+ PhpCond::equal?(1, '-1' ).should be false
62
+ PhpCond::equal?(1, nil ).should be false
63
+ PhpCond::equal?(1, [] ).should be false
64
+ PhpCond::equal?(1, 'php').should be false
65
+ PhpCond::equal?(1, '' ).should be false
66
+ end
67
+
68
+ it 'equal_with_0' do
69
+ PhpCond::equal?(0, true ).should be false
70
+ PhpCond::equal?(0, false).should be true
71
+ PhpCond::equal?(0, 1 ).should be false
72
+ PhpCond::equal?(0, 0 ).should be true
73
+ PhpCond::equal?(0, -1 ).should be false
74
+ PhpCond::equal?(0, '1' ).should be false
75
+ PhpCond::equal?(0, '0' ).should be true
76
+ PhpCond::equal?(0, '-1' ).should be false
77
+ PhpCond::equal?(0, nil ).should be true
78
+ PhpCond::equal?(0, [] ).should be false
79
+ PhpCond::equal?(0, 'php').should be true
80
+ PhpCond::equal?(0, '' ).should be true
81
+ end
82
+
83
+ it 'equal_with_-1' do
84
+ PhpCond::equal?(-1, true ).should be true
85
+ PhpCond::equal?(-1, false).should be false
86
+ PhpCond::equal?(-1, 1 ).should be false
87
+ PhpCond::equal?(-1, 0 ).should be false
88
+ PhpCond::equal?(-1, -1 ).should be true
89
+ PhpCond::equal?(-1, '1' ).should be false
90
+ PhpCond::equal?(-1, '0' ).should be false
91
+ PhpCond::equal?(-1, '-1' ).should be true
92
+ PhpCond::equal?(-1, nil ).should be false
93
+ PhpCond::equal?(-1, [] ).should be false
94
+ PhpCond::equal?(-1, 'php').should be false
95
+ PhpCond::equal?(-1, '' ).should be false
96
+ end
97
+
98
+ it 'equal_with_string_1' do
99
+ PhpCond::equal?('1', true ).should be true
100
+ PhpCond::equal?('1', false).should be false
101
+ PhpCond::equal?('1', 1 ).should be true
102
+ PhpCond::equal?('1', 0 ).should be false
103
+ PhpCond::equal?('1', -1 ).should be false
104
+ PhpCond::equal?('1', '1' ).should be true
105
+ PhpCond::equal?('1', '0' ).should be false
106
+ PhpCond::equal?('1', '-1' ).should be false
107
+ PhpCond::equal?('1', nil ).should be false
108
+ PhpCond::equal?('1', [] ).should be false
109
+ PhpCond::equal?('1', 'php').should be false
110
+ PhpCond::equal?('1', '' ).should be false
111
+ end
112
+
113
+ it 'equal_with_string_0' do
114
+ PhpCond::equal?('0', true ).should be false
115
+ PhpCond::equal?('0', false).should be true
116
+ PhpCond::equal?('0', 1 ).should be false
117
+ PhpCond::equal?('0', 0 ).should be true
118
+ PhpCond::equal?('0', -1 ).should be false
119
+ PhpCond::equal?('0', '1' ).should be false
120
+ PhpCond::equal?('0', '0' ).should be true
121
+ PhpCond::equal?('0', '-1' ).should be false
122
+ PhpCond::equal?('0', nil ).should be false
123
+ PhpCond::equal?('0', [] ).should be false
124
+ PhpCond::equal?('0', 'php').should be false
125
+ PhpCond::equal?('0', '' ).should be false
126
+ end
127
+
128
+ it 'equal_with_string_-1' do
129
+ PhpCond::equal?('-1', true ).should be true
130
+ PhpCond::equal?('-1', false).should be false
131
+ PhpCond::equal?('-1', 1 ).should be false
132
+ PhpCond::equal?('-1', 0 ).should be false
133
+ PhpCond::equal?('-1', -1 ).should be true
134
+ PhpCond::equal?('-1', '1' ).should be false
135
+ PhpCond::equal?('-1', '0' ).should be false
136
+ PhpCond::equal?('-1', '-1' ).should be true
137
+ PhpCond::equal?('-1', nil ).should be false
138
+ PhpCond::equal?('-1', [] ).should be false
139
+ PhpCond::equal?('-1', 'php').should be false
140
+ PhpCond::equal?('-1', '' ).should be false
141
+ end
142
+
143
+ it 'equal_with_nil' do
144
+ PhpCond::equal?(nil, true ).should be false
145
+ PhpCond::equal?(nil, false).should be true
146
+ PhpCond::equal?(nil, 1 ).should be false
147
+ PhpCond::equal?(nil, 0 ).should be true
148
+ PhpCond::equal?(nil, -1 ).should be false
149
+ PhpCond::equal?(nil, '1' ).should be false
150
+ PhpCond::equal?(nil, '0' ).should be false
151
+ PhpCond::equal?(nil, '-1' ).should be false
152
+ PhpCond::equal?(nil, nil ).should be true
153
+ PhpCond::equal?(nil, [] ).should be true
154
+ PhpCond::equal?(nil, 'php').should be false
155
+ PhpCond::equal?(nil, '' ).should be true
156
+ end
157
+
158
+ it 'equal_with_empty_array' do
159
+ PhpCond::equal?([], true ).should be false
160
+ PhpCond::equal?([], false).should be true
161
+ PhpCond::equal?([], 1 ).should be false
162
+ PhpCond::equal?([], 0 ).should be false
163
+ PhpCond::equal?([], -1 ).should be false
164
+ PhpCond::equal?([], '1' ).should be false
165
+ PhpCond::equal?([], '0' ).should be false
166
+ PhpCond::equal?([], '-1' ).should be false
167
+ PhpCond::equal?([], nil ).should be true
168
+ PhpCond::equal?([], [] ).should be true
169
+ PhpCond::equal?([], 'php').should be false
170
+ PhpCond::equal?([], '' ).should be false
171
+ end
172
+
173
+ it 'equal_with_string' do
174
+ PhpCond::equal?('php', true ).should be true
175
+ PhpCond::equal?('php', false).should be false
176
+ PhpCond::equal?('php', 1 ).should be false
177
+ PhpCond::equal?('php', 0 ).should be true
178
+ PhpCond::equal?('php', -1 ).should be false
179
+ PhpCond::equal?('php', '1' ).should be false
180
+ PhpCond::equal?('php', '0' ).should be false
181
+ PhpCond::equal?('php', '-1' ).should be false
182
+ PhpCond::equal?('php', nil ).should be false
183
+ PhpCond::equal?('php', [] ).should be false
184
+ PhpCond::equal?('php', 'php').should be true
185
+ PhpCond::equal?('php', '' ).should be false
186
+ end
187
+
188
+ it 'equal_with_empty_string' do
189
+ PhpCond::equal?('', true ).should be false
190
+ PhpCond::equal?('', false).should be true
191
+ PhpCond::equal?('', 1 ).should be false
192
+ PhpCond::equal?('', 0 ).should be true
193
+ PhpCond::equal?('', -1 ).should be false
194
+ PhpCond::equal?('', '1' ).should be false
195
+ PhpCond::equal?('', '0' ).should be false
196
+ PhpCond::equal?('', '-1' ).should be false
197
+ PhpCond::equal?('', nil ).should be true
198
+ PhpCond::equal?('', [] ).should be false
199
+ PhpCond::equal?('', 'php').should be false
200
+ PhpCond::equal?('', '' ).should be true
201
+ end
202
+
203
+ it 'equal_with_others' do
204
+ PhpCond::equal?([], {}).should be true
205
+ PhpCond::equal?([1], true).should be true
206
+ PhpCond::equal?(1, 1.0).should be true
207
+ PhpCond::equal?([1], [2]).should be false
208
+ PhpCond::equal?([3], [3]).should be true
209
+
210
+ end
211
+
212
+ it 'identical' do
213
+ PhpCond::identical?(nil, nil).should be true
214
+ PhpCond::identical?(false, nil).should be false
215
+ PhpCond::identical?('', 0).should be false
216
+ end
217
+
218
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pebbles-php_cond
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - do-aki
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-01 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: emulate PHP condition
31
+ email:
32
+ - do.hiroaki@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/pebbles-php_cond.rb
43
+ - lib/pebbles-php_cond/version.rb
44
+ - pebbles-php_cond.gemspec
45
+ - spec/php_cond_spec.rb
46
+ homepage: https://github.com/do-aki/pebbles-php_cond
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.23
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: emulate PHP condition
70
+ test_files:
71
+ - spec/php_cond_spec.rb