Class: About_Pos::Meta
- Inherits:
-
Object
- Object
- About_Pos::Meta
- Defined in:
- lib/about_pos.rb
Overview
class self ===
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
- #back? ⇒ Boolean
- #bottom? ⇒ Boolean
- #dir ⇒ Object
- #forward? ⇒ Boolean
- #grab ⇒ Object
-
#initialize(dir, real_index, arr, prev = nil) ⇒ Meta
constructor
A new instance of Meta.
- #middle? ⇒ Boolean
- #next ⇒ Object
- #next? ⇒ Boolean
- #prev ⇒ Object
- #prev? ⇒ Boolean
- #top? ⇒ Boolean
- #value ⇒ Object
Constructor Details
#initialize(dir, real_index, arr, prev = nil) ⇒ Meta
Returns a new instance of Meta.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/about_pos.rb', line 45 def initialize dir, real_index, arr, prev = nil @arr = arr @data = {} @dir = dir @last_index = arr.size - 1 @real_index = real_index @next = nil @prev = prev end |
Instance Method Details
#[](k) ⇒ Object
158 159 160 |
# File 'lib/about_pos.rb', line 158 def [] k @data[k] end |
#[]=(k, v) ⇒ Object
162 163 164 |
# File 'lib/about_pos.rb', line 162 def []= k, v @data[k] = v end |
#back? ⇒ Boolean
122 123 124 |
# File 'lib/about_pos.rb', line 122 def back? dir == :back end |
#bottom? ⇒ Boolean
154 155 156 |
# File 'lib/about_pos.rb', line 154 def bottom? real_index == last_index end |
#dir ⇒ Object
118 119 120 |
# File 'lib/about_pos.rb', line 118 def dir @dir end |
#forward? ⇒ Boolean
126 127 128 |
# File 'lib/about_pos.rb', line 126 def forward? dir == :forward end |
#grab ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/about_pos.rb', line 74 def grab raise No_Next, "No more values to grab" unless next? if forward? @real_index += 1 else @real_index -= 1 end value end |
#middle? ⇒ Boolean
150 151 152 |
# File 'lib/about_pos.rb', line 150 def middle? real_index != 0 && real_index != last_index end |
#next ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/about_pos.rb', line 84 def next @msg ||= if forward? "This is the last position." else "This is the first position." end raise No_Next, @msg if !next? @next ||= begin if forward? Meta.new(dir, real_index + 1, arr, self) else Meta.new(dir, real_index - 1, arr, self) end end end |
#next? ⇒ Boolean
130 131 132 133 134 135 136 |
# File 'lib/about_pos.rb', line 130 def next? if forward? real_index < last_index else real_index > 0 end end |
#prev ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/about_pos.rb', line 101 def prev @msg ||= if forward? "This is the first position." else "This is the last position." end raise No_Prev, @msg if !prev? @prev ||= begin if forward? Meta.new(dir, real_index - 1, arr) else Meta.new(dir, real_index + 1, arr) end end end |
#prev? ⇒ Boolean
138 139 140 141 142 143 144 |
# File 'lib/about_pos.rb', line 138 def prev? if forward? real_index > 0 else real_index < last_index end end |
#top? ⇒ Boolean
146 147 148 |
# File 'lib/about_pos.rb', line 146 def top? real_index == 0 end |
#value ⇒ Object
57 58 59 |
# File 'lib/about_pos.rb', line 57 def value arr[real_index] end |