about_pos 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d68d749d2f08127f80633e1e9f9ee604ac87b087
4
+ data.tar.gz: 63038ab28ff4774823a34d6e184a9d8fc98f14f9
5
+ SHA512:
6
+ metadata.gz: d09008e484f098deb5b3f4f790d82f16fef25fa886cdd4f3a12f1071a2021cfe933e84a2781c9fc0f97683e75737f01a2c9bd8fdd75f4229cbc4ebf489b030e2
7
+ data.tar.gz: f2e5401988650543456e323a64d093b7c13eeeb57c97b3829958b63d54b3872241f6f1a70e55c9fb1f93245666e656b037993dccb42b13e982d4066354a12d33
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 on_a_ladder.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+
2
+ Copyright (c) 2014 da99
3
+
4
+ MIT License
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+
2
+ # About\_Pos
3
+
4
+ Loop through an array and keep track of previous, next item, current item, etc
5
+
6
+ ## Installation
7
+
8
+ gem 'about_pos'
9
+
10
+ ## Usage
11
+
12
+ No coming any time soon.
13
+
14
+ ## The Essence of the Implementation
15
+
16
+ * `meta.next`, `meta.prev`.
17
+ * saving data (`[]`, `[]=`) and keep sure
18
+ the data is carried over to the other items.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/about_pos.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "about_pos"
7
+ spec.version = `cat VERSION`
8
+ spec.authors = ["da99"]
9
+ spec.email = ["i-hate-spam-1234567@mailinator.com"]
10
+ spec.summary = %q{Provide more meta info while you loop back/forth on your arrays.}
11
+ spec.description = %q{
12
+ Whenever I loop through an array, there are times I wish I could
13
+ know what comes before or after the item I am currently one.
14
+ Including prev/next index calculations. This gem helps you with
15
+ that. However, it would be better for you to create your own
16
+ since you will probably not like my way of doing it.
17
+ }
18
+ spec.homepage = "https://github.com/da99/about_pos"
19
+ spec.license = "MIT"
20
+
21
+ spec.files = `git ls-files -z`.split("\x0")
22
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
23
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.5"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "bacon"
29
+ spec.add_development_dependency "Bacon_Colored"
30
+ spec.add_development_dependency "pry"
31
+ end
data/bin/test ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env bash
2
+ # -*- bash -*-
3
+ #
4
+ #
5
+ set -u -e -o pipefail
6
+
7
+ files="$(find specs/ -maxdepth 1 -type f -iname "*.rb" -and -not -iname "helpers.rb")"
8
+ if [[ -z "$files" ]]; then
9
+ colorize yellow "No tests found." 1>&2
10
+ exit 0
11
+ else
12
+ bundle exec bacon specs/helpers.rb $files "$@"
13
+ fi
data/lib/about_pos.rb ADDED
@@ -0,0 +1,154 @@
1
+
2
+ class About_Pos
3
+
4
+ No_Next = Class.new(RuntimeError)
5
+ No_Prev = Class.new(RuntimeError)
6
+
7
+ class << self
8
+
9
+ def Back arr, &blok
10
+ Move(:back, arr, &blok)
11
+ end
12
+
13
+ def Forward arr, &blok
14
+ Move(:forward, arr, &blok)
15
+ end
16
+
17
+ private
18
+ def Move dir, arr
19
+ size = arr.size
20
+
21
+ if dir == :forward
22
+ real_index = 0
23
+ seq = arr
24
+ else
25
+ real_index = (size - 1) - 0
26
+ seq = arr.reverse
27
+ end
28
+
29
+ meta = Meta.new(dir, real_index, 0, arr)
30
+
31
+ seq.each_with_index { |v, i|
32
+ yield meta.value, meta.real_index, meta
33
+ (meta = meta.next) if meta.next?
34
+ }
35
+ end
36
+
37
+ end # === class self ===
38
+
39
+ class Meta
40
+
41
+ def initialize dir, real_index, i, arr, prev = nil
42
+ @arr = arr
43
+ @data = {}
44
+ @dir = dir
45
+ @last_index = arr.size - 1
46
+
47
+ @real_index = real_index
48
+ @value = arr[real_index]
49
+ @i = i
50
+
51
+ @next = nil
52
+ @prev = prev
53
+ end
54
+
55
+ [
56
+ :arr,
57
+ :real_index,
58
+ :last_index,
59
+ :value, :i
60
+ ].each { |v|
61
+ eval %~
62
+ def #{v}
63
+ raise "Value not set for: #{v}" if @#{v}.nil?
64
+ @#{v}
65
+ end
66
+ ~
67
+ }
68
+
69
+ def next
70
+ @msg ||= if forward?
71
+ "This is the last position."
72
+ else
73
+ "This is the first position."
74
+ end
75
+ raise No_Next, @msg if !next?
76
+
77
+ @next ||= begin
78
+ if forward?
79
+ Meta.new(dir, real_index + 1, self.i + 1, arr, self)
80
+ else
81
+ Meta.new(dir, real_index - 1, self.i - 1, arr, self)
82
+ end
83
+ end
84
+ end
85
+
86
+ def prev
87
+ @msg ||= if forward?
88
+ "This is the first position."
89
+ else
90
+ "This is the last position."
91
+ end
92
+ raise No_Prev, @msg if !prev?
93
+
94
+ @prev ||= begin
95
+ if forward?
96
+ Meta.new(dir, real_index - 1, self.i - 1, arr)
97
+ else
98
+ Meta.new(dir, real_index + 1, self.i + 1, arr)
99
+ end
100
+ end
101
+ end
102
+
103
+ def dir
104
+ @dir
105
+ end
106
+
107
+ def back?
108
+ dir == :back
109
+ end
110
+
111
+ def forward?
112
+ dir == :forward
113
+ end
114
+
115
+ def next?
116
+ if forward?
117
+ real_index != last_index
118
+ else
119
+ real_index != 0
120
+ end
121
+ end
122
+
123
+ def prev?
124
+ if forward?
125
+ real_index != 0
126
+ else
127
+ real_index != last_index
128
+ end
129
+ end
130
+
131
+ def top?
132
+ real_index == 0
133
+ end
134
+
135
+ def middle?
136
+ real_index != 0 && real_index != last_index
137
+ end
138
+
139
+ def bottom?
140
+ real_index == last_index
141
+ end
142
+
143
+ def [] k
144
+ @data[k]
145
+ end
146
+
147
+ def []= k, v
148
+ @data[k] = v
149
+ end
150
+
151
+ end # === class Meta ===
152
+
153
+
154
+ end # === class About_Pos ===
data/specs/Back.rb ADDED
@@ -0,0 +1,96 @@
1
+
2
+ describe "Back" do
3
+
4
+ it "runs items in reverse" do
5
+ track = []
6
+ About_Pos.Back([1,2,3]) do |v, i, m|
7
+ track.push v
8
+ end
9
+ track.should == [3,2,1]
10
+ end
11
+
12
+ it "provides the real index" do
13
+ track = []
14
+ About_Pos.Back([1,2,3]) do |v, i, m|
15
+ track.push i
16
+ end
17
+ track.should == [2, 1, 0]
18
+ end
19
+
20
+ describe "Meta" do
21
+
22
+ describe "prev?" do
23
+
24
+ it "is false when at first item (real last item)" do
25
+ track = []
26
+ About_Pos.Back([1,2,3]) do |v, i, m|
27
+ track.push m.prev?
28
+ end
29
+ track.should == [false, true, true]
30
+ end
31
+
32
+ end # === describe prev? ===
33
+
34
+ describe "next?" do
35
+
36
+ it "is first if it reaches the last item (real first item)" do
37
+ track = []
38
+ About_Pos.Back([4,5,6]) do |v,i,m|
39
+ track.push m.next?
40
+ end
41
+ track.should == [true, true, false]
42
+ end
43
+
44
+ end # === describe next? ===
45
+
46
+ describe "prev" do
47
+
48
+ it "has value of previous item" do
49
+ track = []
50
+ About_Pos.Back([8,9,1,2]) do |v,i,m|
51
+ track.push(m.prev? ? m.prev.value : nil)
52
+ end
53
+ track.should == [nil, 2, 1, 9]
54
+ end
55
+
56
+ it "holds data from previous iterations" do
57
+ track = []
58
+ vals = [:a, :b, :c, :d]
59
+ About_Pos.Back([8,9,1,2]) do |v,i,m|
60
+ if m.prev?
61
+ track.push m.prev[:test_val]
62
+ else
63
+ track.push nil
64
+ end
65
+ m[:test_val] = vals.shift
66
+ end
67
+ track.should == [nil, :a, :b, :c]
68
+ end
69
+
70
+ end # === describe prev ===
71
+
72
+ describe "next" do
73
+
74
+ it "has value of next item" do
75
+ track = []
76
+ About_Pos.Back([1,2,3,4]) do |v,i,m|
77
+ track.push(m.next? ? m.next.value : nil)
78
+ end
79
+ track.should == [3, 2, 1, nil]
80
+ end
81
+
82
+ it "saves data that is accesible in the next iteration" do
83
+ track = []
84
+ vals = [:a, :b, :c]
85
+ About_Pos.Back([1,2,3]) { | v, i, m |
86
+ (m.next[:test_val] = vals.shift) if m.next?
87
+ track.push(m[:test_val])
88
+ }
89
+ track.should == [nil, :a, :b]
90
+ end
91
+
92
+ end # === describe next ===
93
+
94
+ end # === describe Meta ===
95
+
96
+ end # === describe about_pos ===
data/specs/Forward.rb ADDED
@@ -0,0 +1,122 @@
1
+
2
+ describe "Forward" do
3
+
4
+ it "runs items in forward fashion" do
5
+ track = []
6
+ About_Pos.Forward([1,2,3]) do |v, i, m|
7
+ track.push v
8
+ end
9
+ track.should == [1,2,3]
10
+ end
11
+
12
+ it "provides the real index" do
13
+ track = []
14
+ About_Pos.Forward([1,2,3,4]) do | v, i, m |
15
+ track.push i
16
+ end
17
+ track.should == [0,1,2,3]
18
+ end
19
+
20
+ describe "Meta" do
21
+
22
+ describe "prev?" do
23
+ it "is false when index == 0" do
24
+ track = []
25
+ About_Pos.Forward([1,2,3,4]) do | v, i, m |
26
+ track.push m.prev?
27
+ end
28
+ track.should == [false, true, true, true]
29
+ end
30
+ end # === describe prev? ===
31
+
32
+ describe "next?" do
33
+ it "is false when index == last index" do
34
+ track = []
35
+ About_Pos.Forward([1,2,3,4]) do | v, i, m |
36
+ track.push m.next?
37
+ end
38
+ track.should == [true, true, true, false]
39
+ end
40
+ end # === describe next? ===
41
+
42
+ describe "next" do
43
+
44
+ it "contains .value for next" do
45
+ track = []
46
+ About_Pos.Forward([1,2,3,4]) do | v, i, m |
47
+ track.push(m.next.value) if m.next?
48
+ end
49
+ track.should == [2,3,4]
50
+ end
51
+
52
+ it "raises an error if there is no next value" do
53
+ lambda {
54
+ About_Pos.Forward([1,2,3,4]) do | v, i, m |
55
+ m.next
56
+ end
57
+ }.should.raise(About_Pos::No_Next)
58
+ .message.should.match /This is the last position/i
59
+ end
60
+
61
+ end # === describe next ===
62
+
63
+ describe "prev" do
64
+
65
+ it "contains a .value for prev" do
66
+ track = []
67
+ About_Pos.Forward([1,2,3,4]) do | v, i, m |
68
+ track.push(m.prev.value) if m.prev?
69
+ end
70
+ track.should == [1,2,3]
71
+ end
72
+
73
+ it "raises an error if there is no prev value" do
74
+ lambda {
75
+ About_Pos.Forward([1,2,3,4]) do | v, i, m |
76
+ m.prev
77
+ end
78
+ }.should.raise(About_Pos::No_Prev)
79
+ .message.should.match /This is the first position/i
80
+ end
81
+
82
+ end # === describe prev ===
83
+
84
+ describe "saving/reading data ([], []=)" do
85
+
86
+ it "saves a value to be used on .prev meta" do
87
+ vals = [:a, :b, :c]
88
+ track = []
89
+ About_Pos.Forward([1,2,3,4]) do | v, i, m |
90
+ m[:test_val] = vals.shift
91
+
92
+ if m.prev?
93
+ track.push( m.prev[:test_val] )
94
+ else
95
+ track.push nil
96
+ end
97
+ end
98
+ track.should == [nil,:a,:b,:c]
99
+ end
100
+
101
+ it "saves a value to be used on .next meta" do
102
+ vals = [:d, :e, :f]
103
+ track = []
104
+ About_Pos.Forward([1,2,3,4]) do |v,i,m|
105
+ if m.next?
106
+ m.next[:test_val] = vals.shift
107
+ end
108
+ track.push m[:test_val]
109
+ end
110
+ track.should == [nil, :d, :e, :f]
111
+ end
112
+
113
+ end # === describe []/[]= ===
114
+
115
+ end # === describe Meta ===
116
+
117
+ end # === describe about_pos ===
118
+
119
+
120
+
121
+
122
+
data/specs/helpers.rb ADDED
@@ -0,0 +1,4 @@
1
+
2
+ require 'Bacon_Colored'
3
+ require 'about_pos'
4
+ require 'pry'
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: about_pos
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - da99
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bacon
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: Bacon_Colored
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: "\n Whenever I loop through an array, there are times I wish I could\n
84
+ \ know what comes before or after the item I am currently one.\n Including
85
+ prev/next index calculations. This gem helps you with\n that. However, it would
86
+ be better for you to create your own\n since you will probably not like my way
87
+ of doing it.\n "
88
+ email:
89
+ - i-hate-spam-1234567@mailinator.com
90
+ executables:
91
+ - test
92
+ extensions: []
93
+ extra_rdoc_files: []
94
+ files:
95
+ - ".gitignore"
96
+ - Gemfile
97
+ - LICENSE.txt
98
+ - README.md
99
+ - VERSION
100
+ - about_pos.gemspec
101
+ - bin/test
102
+ - lib/about_pos.rb
103
+ - specs/Back.rb
104
+ - specs/Forward.rb
105
+ - specs/helpers.rb
106
+ homepage: https://github.com/da99/about_pos
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.2.2
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Provide more meta info while you loop back/forth on your arrays.
130
+ test_files: []