each_with_flag 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGES +0 -0
- data/MANIFEST +8 -0
- data/README.md +60 -0
- data/Rakefile +31 -0
- data/each_with_flag.gemspec +23 -0
- data/lib/each_with_flag.rb +6 -0
- data/lib/each_with_flag/enumerable/each_with_flag.rb +80 -0
- data/lib/each_with_flag/version.rb +3 -0
- data/test/test_each_with_flag.rb +228 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f5df8102224b53f67d378fd86feb93e935b473b1
|
4
|
+
data.tar.gz: 230851298d7a7399074e5c4c4e8931d467d7fb22
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 963fbff974d019c580cc4a05a15a252a7b0ededf889b911e2ce6958e89c095b3b6f10203a4fa9eabaaf110afae403d15aa5878e1eede5a20b71e609e615bbb9e
|
7
|
+
data.tar.gz: 3ee903da722b1faec1f409eb1c2de56a444a3907eab83712941eed89471048ec03cf8263b8e3ea9f6d86f1b2f2e324a0e801a2e601a254bc7a1ab0971f3147ac
|
data/CHANGES
ADDED
File without changes
|
data/MANIFEST
ADDED
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# Each with flags
|
2
|
+
|
3
|
+
Each with flags gem provides Enumerable methods"each_with_flag" and "each_with_index_with_flag" that expands standard methods "each" and "each_with_index" adding to yield block flags of first, has previous, surrounded, has next and has last element.
|
4
|
+
|
5
|
+
# Methods
|
6
|
+
|
7
|
+
## each_with_flag
|
8
|
+
|
9
|
+
Extend method each adding flags.
|
10
|
+
|
11
|
+
### Usage
|
12
|
+
```ruby
|
13
|
+
enumerable_obj.each_with_flag(flags) do |item, flag1, flag2| do
|
14
|
+
# Your code here
|
15
|
+
end
|
16
|
+
```
|
17
|
+
### Available flags
|
18
|
+
|
19
|
+
- :first - is item first
|
20
|
+
- :is_first - same as :first
|
21
|
+
- :has_previous - is item has previous item in list
|
22
|
+
- :has_prev - same as :has_previous
|
23
|
+
- :surrounded - is item has previous and next item in list
|
24
|
+
- :is_surrounded - same as :surrounded
|
25
|
+
- :has_next - is item has next item in list
|
26
|
+
- :last - is item last in list
|
27
|
+
- :is_last - same as :last
|
28
|
+
|
29
|
+
### Example
|
30
|
+
```ruby
|
31
|
+
['foo', 'bar', 'baz'].each_with_flag(:first, :surrounded, :last) do |item, is_first, is_surrounded, is_last| do
|
32
|
+
# is_first will be true only on first iteration
|
33
|
+
# is_surrounded will be true only on second iteration
|
34
|
+
# is_last will be true only on last iteration
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
## each_with_index_with_flag
|
39
|
+
The same as "each_with_flag" like "each" and "each_with_index"
|
40
|
+
|
41
|
+
## Additional method
|
42
|
+
Every flag has two additional metnods "each_with_{{flag_name}}" and "each_with_index_with_{{flag_name}}"
|
43
|
+
|
44
|
+
### Example
|
45
|
+
```ruby
|
46
|
+
['foo', 'bar', 'baz'].each_with_first_flag do |item, is_first,| do
|
47
|
+
# is_first will be true only on first iteration
|
48
|
+
end
|
49
|
+
|
50
|
+
['foo', 'bar', 'baz'].each_with_index_with_first_flag do |item, index, is_first,| do
|
51
|
+
# is_first will be true only on first iteration
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
## Version
|
56
|
+
0.1.0
|
57
|
+
|
58
|
+
License
|
59
|
+
----
|
60
|
+
MIT
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/clean'
|
4
|
+
|
5
|
+
CLEAN.include('*.gem', '*.rbc')
|
6
|
+
|
7
|
+
namespace :gem do
|
8
|
+
desc 'Build the each-with-flag gem'
|
9
|
+
task :create => [:clean] do
|
10
|
+
spec = eval(IO.read('each-with-flag.gemspec'))
|
11
|
+
if Gem::VERSION < '2.0'
|
12
|
+
Gem::Builder.new(spec).build
|
13
|
+
else
|
14
|
+
require 'rubygems/package'
|
15
|
+
Gem::Package.build(spec)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
desc 'Install the each_with_flag library as a gem'
|
20
|
+
task :install => [:create] do
|
21
|
+
file = Dir['*.gem'].first
|
22
|
+
sh "gem install -l #{file}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
Rake::TestTask.new do |t|
|
27
|
+
t.warning = true
|
28
|
+
t.verbose = true
|
29
|
+
end
|
30
|
+
|
31
|
+
task :default => :test
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.expand_path('../lib/each_with_flag/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'each_with_flag'
|
5
|
+
s.version = EachWithFlag::VERSION
|
6
|
+
s.date = '2015-10-18'
|
7
|
+
s.summary = 'Enhanced each methods (each with flags) for Enumerable objects'
|
8
|
+
s.authors = ['Oleh Birjukov']
|
9
|
+
s.email = 'ol.birjukov@gmail.com'
|
10
|
+
s.files = Dir['**/*'].reject { |f| f.include?('git') }
|
11
|
+
s.homepage = 'https://github.com/all-bear/rubygem.each_with_flag'
|
12
|
+
s.license = 'MIT'
|
13
|
+
s.test_files = s.files.grep(%r{^(test)/})
|
14
|
+
s.require_paths = ['lib']
|
15
|
+
s.description = <<-EOF
|
16
|
+
The each-with-flag library provides Enumerable methods
|
17
|
+
"each_with_flag" and "each_with_index_with_flag"
|
18
|
+
that expand standard methods "each" and "each_with_index"
|
19
|
+
adding to yield block flags of first, has previous, surrounded, has next and has last element.
|
20
|
+
EOF
|
21
|
+
|
22
|
+
s.add_development_dependency 'test-unit', '~> 3.0'
|
23
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module Enumerable
|
2
|
+
# The version of the each-with-flag library.
|
3
|
+
EACH_WITH_FLAG_VERSION = '0.1.0'
|
4
|
+
|
5
|
+
EACH_WITH_FLAG_FLAGS = {
|
6
|
+
:first => [:first, :is_first],
|
7
|
+
:has_previous => [:has_previous, :has_prev],
|
8
|
+
:surrounded => [:surrounded, :is_surrounded],
|
9
|
+
:has_next => :has_next,
|
10
|
+
:last => [:last, :is_last]
|
11
|
+
}
|
12
|
+
|
13
|
+
def each_with_flag(*params)
|
14
|
+
self.each_with_index do |data, index|
|
15
|
+
yield data, *flags(index, params)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def each_with_index_with_flag(*params)
|
20
|
+
self.each_with_index do |data, index|
|
21
|
+
yield data, index, *flags(index, params)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.all_user_flags
|
26
|
+
[EACH_WITH_FLAG_FLAGS.values].flatten
|
27
|
+
end
|
28
|
+
|
29
|
+
private_class_method :all_user_flags
|
30
|
+
|
31
|
+
all_user_flags.each do |flag|
|
32
|
+
define_method "each_with_#{flag}_flag" do |&b|
|
33
|
+
self.each_with_flag(flag) do |*params|
|
34
|
+
b.call *params
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
define_method "each_with_index_with_#{flag}_flag" do |&b|
|
39
|
+
self.each_with_index_with_flag(flag) do |*params|
|
40
|
+
b.call *params
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def user_flags_for(name)
|
48
|
+
[EACH_WITH_FLAG_FLAGS[name]].flatten
|
49
|
+
end
|
50
|
+
|
51
|
+
def slice_hash(hash, keys)
|
52
|
+
keys.each_with_object({}) do |k, h|
|
53
|
+
h[k] = hash[k] if hash.has_key?(k)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def define_flag(user_flags, flag_name, value)
|
58
|
+
user_flags_for(flag_name).each_with_object(user_flags) do |user_flag, flags|
|
59
|
+
flags[user_flag] = value
|
60
|
+
flags
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def last_index
|
65
|
+
self.size - 1
|
66
|
+
end
|
67
|
+
|
68
|
+
def flags(index, params)
|
69
|
+
flags = {}
|
70
|
+
|
71
|
+
define_flag(flags, :first, index == 0)
|
72
|
+
define_flag(flags, :has_previous, 0 < index)
|
73
|
+
define_flag(flags, :surrounded, 0 < index && index < last_index)
|
74
|
+
define_flag(flags, :has_next, index < last_index)
|
75
|
+
define_flag(flags, :last, index == last_index)
|
76
|
+
define_flag(flags, :first, index == 0)
|
77
|
+
|
78
|
+
slice_hash(flags, params).values
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,228 @@
|
|
1
|
+
########################################################################
|
2
|
+
# test_each_with_flag.rb
|
3
|
+
#
|
4
|
+
# Test case for the enumerable-extra library. You should run this
|
5
|
+
# test via the 'rake test' task.
|
6
|
+
########################################################################
|
7
|
+
require 'test-unit'
|
8
|
+
require 'enumerable/each_with_flag'
|
9
|
+
|
10
|
+
class Test_Enumerable_EachWithFlag < Test::Unit::TestCase
|
11
|
+
TEST_ITEM_SIZE = 3
|
12
|
+
|
13
|
+
def setup
|
14
|
+
@array = %w/foo bar baz/
|
15
|
+
@hash = {:a => 'foo', :b => 'bar', :c => 'baz'}
|
16
|
+
|
17
|
+
@expected_flags = {
|
18
|
+
:first => [true, false, false],
|
19
|
+
:has_previous => [false, true, true],
|
20
|
+
:surrounded => [false, true, false],
|
21
|
+
:has_next => [true, true, false],
|
22
|
+
:last => [false, false, true]
|
23
|
+
}
|
24
|
+
|
25
|
+
@expected_flag_mask = (0..(TEST_ITEM_SIZE - 1)).each_with_object([]) do |index, mask|
|
26
|
+
mask[index] = @expected_flags.each_with_object([]) do |data, mask|
|
27
|
+
mask << data[1][index]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
@flag_params = [:first, :has_previous, :surrounded, :has_next, :last]
|
32
|
+
|
33
|
+
@real_flag_masks = []
|
34
|
+
@yielded_flags = []
|
35
|
+
@yielded_items = []
|
36
|
+
@yielded_keys = []
|
37
|
+
@yielded_indexes = []
|
38
|
+
@yield_counter = 0
|
39
|
+
end
|
40
|
+
|
41
|
+
test 'version number is set properly' do
|
42
|
+
assert_equal('0.1.0', Enumerable::EACH_WITH_FLAG_VERSION)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_each_with_flag
|
46
|
+
check_respond_to :each_with_flag
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_each_with_flag_for_hash
|
50
|
+
@hash.send(:each_with_flag, *@flag_params) do |data, is_first, has_previous, surrounded, has_next, is_last|
|
51
|
+
add_key data[0]
|
52
|
+
add_item data[1]
|
53
|
+
add_flag_mask [is_first, has_previous, surrounded, has_next, is_last]
|
54
|
+
increase_yield_counter
|
55
|
+
end
|
56
|
+
|
57
|
+
check_flag_result_data @hash
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_each_with_flag_for_hash_reversed_params
|
61
|
+
@hash.send(:each_with_flag, *@flag_params.reverse) do |data, is_last, has_next, surrounded, has_previous, is_first|
|
62
|
+
add_key data[0]
|
63
|
+
add_item data[1]
|
64
|
+
add_flag_mask [is_first, has_previous, surrounded, has_next, is_last]
|
65
|
+
increase_yield_counter
|
66
|
+
end
|
67
|
+
|
68
|
+
check_flag_result_data @hash
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_each_with_flag_for_array
|
72
|
+
@array.send(:each_with_flag, *@flag_params) do |item, is_first, has_previous, surrounded, has_next, is_last|
|
73
|
+
add_item item
|
74
|
+
add_flag_mask [is_first, has_previous, surrounded, has_next, is_last]
|
75
|
+
increase_yield_counter
|
76
|
+
end
|
77
|
+
|
78
|
+
check_flag_result_data @array
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_each_with_flag_for_array_reversed_params
|
82
|
+
@array.send(:each_with_flag, *@flag_params.reverse) do |item, is_last, has_next, surrounded, has_previous, is_first|
|
83
|
+
add_item item
|
84
|
+
add_flag_mask [is_first, has_previous, surrounded, has_next, is_last]
|
85
|
+
increase_yield_counter
|
86
|
+
end
|
87
|
+
|
88
|
+
check_flag_result_data @array
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_each_with_first_flag
|
92
|
+
check_respond_to :each_with_first_flag
|
93
|
+
end
|
94
|
+
|
95
|
+
test 'each with special flags' do
|
96
|
+
@flag_params.each { |flag| check_each_with_special flag }
|
97
|
+
end
|
98
|
+
|
99
|
+
test 'each with index with special flags' do
|
100
|
+
@flag_params.each { |flag| check_each_with_index_with_special flag }
|
101
|
+
end
|
102
|
+
|
103
|
+
def teardown
|
104
|
+
@array = nil
|
105
|
+
@hash = nil
|
106
|
+
|
107
|
+
@real_flag_masks = nil
|
108
|
+
@yielded_flags = nil
|
109
|
+
@yielded_items = nil
|
110
|
+
@yielded_keys = nil
|
111
|
+
@yield_counter = nil
|
112
|
+
@yielded_indexes = nil
|
113
|
+
end
|
114
|
+
|
115
|
+
private
|
116
|
+
def restart
|
117
|
+
teardown
|
118
|
+
setup
|
119
|
+
end
|
120
|
+
|
121
|
+
def check_respond_to(method)
|
122
|
+
[@array, @hash].each do |item|
|
123
|
+
assert_respond_to(item, method)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def add_flag_mask(mask)
|
128
|
+
@real_flag_masks << mask
|
129
|
+
end
|
130
|
+
|
131
|
+
def increase_yield_counter
|
132
|
+
@yield_counter = @yield_counter + 1
|
133
|
+
end
|
134
|
+
|
135
|
+
def add_item(item)
|
136
|
+
@yielded_items << item
|
137
|
+
end
|
138
|
+
|
139
|
+
def add_key(key)
|
140
|
+
@yielded_keys << key
|
141
|
+
end
|
142
|
+
|
143
|
+
def add_index(index)
|
144
|
+
@yielded_indexes << index
|
145
|
+
end
|
146
|
+
|
147
|
+
def add_flag(flag)
|
148
|
+
@yielded_flags << flag
|
149
|
+
end
|
150
|
+
|
151
|
+
def check_general_result_data(subject, params)
|
152
|
+
assert_equal TEST_ITEM_SIZE, @yield_counter, 'Invalid count of block calls'
|
153
|
+
|
154
|
+
if subject.is_a? Hash
|
155
|
+
assert_equal @yielded_items, subject.values, 'Invalid items was passed to block'
|
156
|
+
elsif subject.is_a? Array
|
157
|
+
assert_equal @yielded_items, subject, 'Invalid items was passed to block'
|
158
|
+
end
|
159
|
+
|
160
|
+
assert_equal @yielded_keys, subject.keys, 'Invalid keys was passed to block' if subject.is_a? Hash
|
161
|
+
|
162
|
+
assert_equal @yielded_indexes, [0, 1, 2], 'Invalid indexes was passed to block' if params[:with_index]
|
163
|
+
end
|
164
|
+
|
165
|
+
def check_special_flag_result_data(subject, flag, params = {})
|
166
|
+
check_general_result_data(subject, params)
|
167
|
+
|
168
|
+
assert_equal @expected_flags[flag], @yielded_flags, 'Invalid flags'
|
169
|
+
end
|
170
|
+
|
171
|
+
def check_flag_result_data(subject, params = {})
|
172
|
+
check_general_result_data(subject, params)
|
173
|
+
|
174
|
+
assert_equal @expected_flag_mask, @real_flag_masks, 'Invalid flags'
|
175
|
+
end
|
176
|
+
|
177
|
+
def check_each_with_special(special)
|
178
|
+
method_name = "each_with_#{special}_flag"
|
179
|
+
|
180
|
+
@array.send(method_name) do |item, is_first|
|
181
|
+
add_item item
|
182
|
+
add_flag is_first
|
183
|
+
increase_yield_counter
|
184
|
+
end
|
185
|
+
|
186
|
+
check_special_flag_result_data @array, special
|
187
|
+
|
188
|
+
restart
|
189
|
+
|
190
|
+
@hash.send(method_name) do |data, is_first|
|
191
|
+
add_key data[0]
|
192
|
+
add_item data[1]
|
193
|
+
add_flag is_first
|
194
|
+
increase_yield_counter
|
195
|
+
end
|
196
|
+
|
197
|
+
check_special_flag_result_data @hash, special
|
198
|
+
|
199
|
+
restart
|
200
|
+
end
|
201
|
+
|
202
|
+
def check_each_with_index_with_special(special)
|
203
|
+
method_name = "each_with_index_with_#{special}_flag"
|
204
|
+
|
205
|
+
@array.send(method_name) do |item, index, is_first|
|
206
|
+
add_item item
|
207
|
+
add_index index
|
208
|
+
add_flag is_first
|
209
|
+
increase_yield_counter
|
210
|
+
end
|
211
|
+
|
212
|
+
check_special_flag_result_data @array, special, :with_index => true
|
213
|
+
|
214
|
+
restart
|
215
|
+
|
216
|
+
@hash.send(method_name) do |data, index, is_first|
|
217
|
+
add_key data[0]
|
218
|
+
add_item data[1]
|
219
|
+
add_index index
|
220
|
+
add_flag is_first
|
221
|
+
increase_yield_counter
|
222
|
+
end
|
223
|
+
|
224
|
+
check_special_flag_result_data @hash, special, :with_index => true
|
225
|
+
|
226
|
+
restart
|
227
|
+
end
|
228
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: each_with_flag
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Oleh Birjukov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: test-unit
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
description: |2
|
28
|
+
The each-with-flag library provides Enumerable methods
|
29
|
+
"each_with_flag" and "each_with_index_with_flag"
|
30
|
+
that expand standard methods "each" and "each_with_index"
|
31
|
+
adding to yield block flags of first, has previous, surrounded, has next and has last element.
|
32
|
+
email: ol.birjukov@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- CHANGES
|
38
|
+
- MANIFEST
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- each_with_flag.gemspec
|
42
|
+
- lib/each_with_flag.rb
|
43
|
+
- lib/each_with_flag/enumerable/each_with_flag.rb
|
44
|
+
- lib/each_with_flag/version.rb
|
45
|
+
- test/test_each_with_flag.rb
|
46
|
+
homepage: https://github.com/all-bear/rubygem.each_with_flag
|
47
|
+
licenses:
|
48
|
+
- MIT
|
49
|
+
metadata: {}
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 2.4.6
|
67
|
+
signing_key:
|
68
|
+
specification_version: 4
|
69
|
+
summary: Enhanced each methods (each with flags) for Enumerable objects
|
70
|
+
test_files:
|
71
|
+
- test/test_each_with_flag.rb
|