vedeu 0.6.7 → 0.6.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/docs/dsl.md +4 -4
  4. data/lib/vedeu/all.rb +6 -18
  5. data/lib/vedeu/api.rb +14 -13
  6. data/lib/vedeu/bindings/system.rb +5 -4
  7. data/lib/vedeu/buffers/virtual_buffer.rb +13 -11
  8. data/lib/vedeu/configuration/api.rb +4 -3
  9. data/lib/vedeu/cursors/cursor.rb +23 -18
  10. data/lib/vedeu/distributed/client.rb +11 -6
  11. data/lib/vedeu/distributed/server.rb +4 -3
  12. data/lib/vedeu/dsl/keymap.rb +6 -6
  13. data/lib/vedeu/editor/all.rb +19 -0
  14. data/lib/vedeu/{input/editor → editor}/capture.rb +1 -1
  15. data/lib/vedeu/{input/editor → editor}/cropper.rb +0 -0
  16. data/lib/vedeu/{input/editor → editor}/cursor.rb +0 -0
  17. data/lib/vedeu/{input/editor → editor}/document.rb +0 -0
  18. data/lib/vedeu/{input/editor → editor}/documents.rb +0 -0
  19. data/lib/vedeu/{input/editor → editor}/editor.rb +0 -0
  20. data/lib/vedeu/{input/editor → editor}/insert.rb +0 -0
  21. data/lib/vedeu/{input/editor → editor}/line.rb +0 -0
  22. data/lib/vedeu/{input/editor → editor}/lines.rb +0 -0
  23. data/lib/vedeu/error.rb +21 -18
  24. data/lib/vedeu/esc/borders.rb +6 -4
  25. data/lib/vedeu/esc/colours.rb +13 -12
  26. data/lib/vedeu/esc/esc.rb +11 -10
  27. data/lib/vedeu/input/all.rb +15 -0
  28. data/lib/vedeu/input/input.rb +55 -52
  29. data/lib/vedeu/input/key.rb +36 -30
  30. data/lib/vedeu/input/keymap.rb +101 -92
  31. data/lib/vedeu/input/keymaps.rb +10 -6
  32. data/lib/vedeu/input/keys.rb +8 -4
  33. data/lib/vedeu/input/mapper.rb +125 -121
  34. data/lib/vedeu/input/translator.rb +159 -0
  35. data/lib/vedeu/internal_api.rb +16 -15
  36. data/lib/vedeu/logging/all.rb +13 -0
  37. data/lib/vedeu/logging/debug.rb +84 -0
  38. data/lib/vedeu/logging/lockless_log_device.rb +65 -0
  39. data/lib/vedeu/logging/log.rb +179 -0
  40. data/lib/vedeu/logging/mono_logger.rb +26 -0
  41. data/lib/vedeu/logging/timer.rb +68 -0
  42. data/lib/vedeu/menus/all.rb +12 -0
  43. data/lib/vedeu/{dsl/menu.rb → menus/dsl.rb} +19 -18
  44. data/lib/vedeu/menus/menu.rb +234 -0
  45. data/lib/vedeu/{null/menu.rb → menus/null.rb} +6 -6
  46. data/lib/vedeu/menus/repository.rb +18 -0
  47. data/lib/vedeu/models/cell.rb +62 -55
  48. data/lib/vedeu/models/escape.rb +55 -50
  49. data/lib/vedeu/models/page.rb +74 -69
  50. data/lib/vedeu/models/row.rb +55 -51
  51. data/lib/vedeu/models/views/all.rb +1 -0
  52. data/lib/vedeu/models/views/char.rb +1 -1
  53. data/lib/vedeu/models/views/html_char.rb +165 -0
  54. data/lib/vedeu/output/renderers/html.rb +5 -4
  55. data/lib/vedeu/repositories/model.rb +2 -0
  56. data/lib/vedeu/runtime/application.rb +110 -103
  57. data/lib/vedeu/runtime/bootstrap.rb +109 -103
  58. data/lib/vedeu/runtime/flags.rb +45 -41
  59. data/lib/vedeu/runtime/launcher.rb +1 -1
  60. data/lib/vedeu/runtime/main_loop.rb +51 -46
  61. data/lib/vedeu/runtime/traps.rb +20 -16
  62. data/lib/vedeu/version.rb +1 -1
  63. data/lib/vedeu.rb +2 -2
  64. data/test/lib/vedeu/buffers/virtual_buffer_test.rb +1 -1
  65. data/test/lib/vedeu/cursors/cursor_test.rb +11 -5
  66. data/test/lib/vedeu/dsl/interface_test.rb +1 -1
  67. data/test/lib/vedeu/dsl/keymap_test.rb +2 -2
  68. data/test/lib/vedeu/{input/editor → editor}/capture_test.rb +0 -0
  69. data/test/lib/vedeu/{input/editor → editor}/cropper_test.rb +0 -0
  70. data/test/lib/vedeu/{input/editor → editor}/cursor_test.rb +0 -0
  71. data/test/lib/vedeu/{input/editor → editor}/document_test.rb +0 -0
  72. data/test/lib/vedeu/{input/editor → editor}/documents_test.rb +0 -0
  73. data/test/lib/vedeu/{input/editor → editor}/editor_test.rb +0 -0
  74. data/test/lib/vedeu/{input/editor → editor}/insert_test.rb +0 -0
  75. data/test/lib/vedeu/{input/editor → editor}/line_test.rb +0 -0
  76. data/test/lib/vedeu/{input/editor → editor}/lines_test.rb +0 -0
  77. data/test/lib/vedeu/input/input_test.rb +58 -54
  78. data/test/lib/vedeu/input/key_test.rb +29 -25
  79. data/test/lib/vedeu/input/keymap_test.rb +74 -70
  80. data/test/lib/vedeu/input/keymaps_test.rb +8 -4
  81. data/test/lib/vedeu/input/keys_test.rb +12 -8
  82. data/test/lib/vedeu/input/mapper_test.rb +64 -56
  83. data/test/lib/vedeu/input/translator_test.rb +36 -0
  84. data/test/lib/vedeu/logging/debug_test.rb +39 -0
  85. data/test/lib/vedeu/logging/lockless_log_device_test.rb +42 -0
  86. data/test/lib/vedeu/logging/log_test.rb +52 -0
  87. data/test/lib/vedeu/logging/mono_logger_test.rb +43 -0
  88. data/test/lib/vedeu/logging/timer_test.rb +44 -0
  89. data/test/lib/vedeu/{dsl/menu_test.rb → menus/dsl_test.rb} +6 -6
  90. data/test/lib/vedeu/menus/menu_test.rb +305 -0
  91. data/test/lib/vedeu/{null/menu_test.rb → menus/null_test.rb} +5 -5
  92. data/test/lib/vedeu/menus/repository_test.rb +17 -0
  93. data/test/lib/vedeu/models/cell_test.rb +54 -50
  94. data/test/lib/vedeu/models/escape_test.rb +49 -45
  95. data/test/lib/vedeu/models/page_test.rb +167 -160
  96. data/test/lib/vedeu/models/row_test.rb +71 -67
  97. data/test/lib/vedeu/models/views/html_char_test.rb +110 -0
  98. data/test/lib/vedeu/output/compressor_test.rb +44 -22
  99. data/test/lib/vedeu/output/renderers_test.rb +4 -2
  100. data/test/lib/vedeu/runtime/application_test.rb +36 -32
  101. data/test/lib/vedeu/runtime/bootstrap_test.rb +31 -26
  102. data/test/lib/vedeu/runtime/flags_test.rb +33 -29
  103. data/test/lib/vedeu/runtime/launcher_test.rb +4 -2
  104. data/test/lib/vedeu/runtime/main_loop_test.rb +28 -24
  105. data/test/lib/vedeu/runtime/traps_test.rb +8 -4
  106. data/test/test_helper.rb +4 -2
  107. metadata +66 -63
  108. data/lib/vedeu/input/editor/all.rb +0 -19
  109. data/lib/vedeu/input/input_translator.rb +0 -155
  110. data/lib/vedeu/log/debug.rb +0 -79
  111. data/lib/vedeu/log/lockless_log_device.rb +0 -61
  112. data/lib/vedeu/log/log.rb +0 -172
  113. data/lib/vedeu/log/mono_logger.rb +0 -21
  114. data/lib/vedeu/log/timer.rb +0 -63
  115. data/lib/vedeu/models/menu.rb +0 -217
  116. data/lib/vedeu/models/menus.rb +0 -14
  117. data/lib/vedeu/output/html_char.rb +0 -161
  118. data/test/lib/vedeu/input/input_translator_test.rb +0 -32
  119. data/test/lib/vedeu/log/debug_test.rb +0 -35
  120. data/test/lib/vedeu/log/lockless_log_device_test.rb +0 -29
  121. data/test/lib/vedeu/log/log_test.rb +0 -48
  122. data/test/lib/vedeu/log/mono_logger_test.rb +0 -31
  123. data/test/lib/vedeu/log/timer_test.rb +0 -40
  124. data/test/lib/vedeu/models/menu_test.rb +0 -301
  125. data/test/lib/vedeu/models/menus_test.rb +0 -13
  126. data/test/lib/vedeu/output/html_char_test.rb +0 -106
@@ -0,0 +1,179 @@
1
+ module Vedeu
2
+
3
+ module Logging
4
+
5
+ # Provides the ability to log anything to the Vedeu log file.
6
+ #
7
+ class Log
8
+
9
+ class << self
10
+
11
+ # Write a message to the Vedeu log file.
12
+ #
13
+ # @example
14
+ # Vedeu.log(type: :debug,
15
+ # message: 'A useful debugging message: Error!')
16
+ #
17
+ # @param message [String] The message you wish to emit to the
18
+ # log file, useful for debugging.
19
+ # @param force [Boolean] When evaluates to true will attempt
20
+ # to write to the log file regardless of the Configuration
21
+ # setting.
22
+ # @param type [Symbol] Colour code messages in the log file
23
+ # depending on their source. See {message_types}
24
+ #
25
+ # @return [TrueClass]
26
+ def log(message:, force: false, type: :info)
27
+ output = log_entry(type, message)
28
+
29
+ if (enabled? || force) && (Vedeu::Configuration.log_only.empty? ||
30
+ Vedeu::Configuration.log_only.include?(type))
31
+ logger.debug(output)
32
+ end
33
+
34
+ output
35
+ end
36
+
37
+ # Write a message to STDOUT.
38
+ #
39
+ # @example
40
+ # Vedeu.log_stdout
41
+ #
42
+ # @return [TrueClass]
43
+ def log_stdout(type: :info, message:)
44
+ $stdout.puts log_entry(type, message)
45
+ end
46
+
47
+ # Write a message to STDERR.
48
+ #
49
+ # @example
50
+ # Vedeu.log_stderr
51
+ #
52
+ # @return [TrueClass]
53
+ def log_stderr(type: :info, message:)
54
+ $stderr.puts log_entry(type, message)
55
+ end
56
+
57
+ private
58
+
59
+ # @return [TrueClass]
60
+ def logger
61
+ MonoLogger.new(log_file).tap do |log|
62
+ log.formatter = proc do |_, _, _, message|
63
+ formatted_message(message)
64
+ end
65
+ end
66
+ end
67
+
68
+ # Returns the message with timestamp.
69
+ #
70
+ # [ 0.0987] [debug] Something happened.
71
+ #
72
+ # @param message [String] The message type and message
73
+ # coloured and combined.
74
+ # @return [String]
75
+ def formatted_message(message)
76
+ "#{timestamp}#{message}\n" if message
77
+ end
78
+
79
+ # Returns the message:
80
+ #
81
+ # [type] message
82
+ #
83
+ # @param type [Symbol] The type of log message.
84
+ # @param message [String] The message you wish to emit to the
85
+ # log file, useful for debugging.
86
+ # @return [String]
87
+ def log_entry(type, message)
88
+ "#{message_type(type)}#{message_body(type, message)}"
89
+ end
90
+
91
+ # Fetches the filename from the configuration.
92
+ #
93
+ # @return [String]
94
+ def log_file
95
+ Vedeu::Configuration.log
96
+ end
97
+ alias_method :enabled?, :log_file
98
+
99
+ # Displays the message body using the colour specified in the
100
+ # last element of {message_types}.
101
+ #
102
+ # @param type [Symbol] The type of log message.
103
+ # @param body [String] The log message itself.
104
+ # @return [String]
105
+ def message_body(type, body)
106
+ Vedeu::Esc.send(message_types.fetch(type, :default)[-1]) do
107
+ body
108
+ end
109
+ end
110
+
111
+ # Displays the message type using the colour specified in the
112
+ # first element of {message_types}.
113
+ #
114
+ # @param type [Symbol] The type of log message.
115
+ # @return [String]
116
+ def message_type(type)
117
+ Vedeu::Esc.send(message_types.fetch(type, :default)[0]) do
118
+ "[#{type}]".ljust(9)
119
+ end
120
+ end
121
+
122
+ # The defined message types for Vedeu with their respective
123
+ # colours. When used, produces a log entry of the format:
124
+ #
125
+ # [type] message
126
+ #
127
+ # The 'type' will be shown as the first colour defined in the
128
+ # value array, whilst the 'message' will be shown using the
129
+ # last colour.
130
+ #
131
+ # Valid types available:
132
+ #
133
+ # :config, :create, :debug, :error, :drb, :event, :info,
134
+ # :input, :output, :reset, :store, :test, :timer, :update
135
+ #
136
+ # @return [Hash<Symbol => Array<Symbol>>]
137
+ def message_types
138
+ {
139
+ config: [:light_yellow, :yellow],
140
+ create: [:light_green, :green],
141
+ debug: [:light_red, :red],
142
+ error: [:light_red, :red],
143
+ drb: [:light_blue, :blue],
144
+ event: [:light_magenta, :magenta],
145
+ info: [:white, :default],
146
+ input: [:light_yellow, :yellow],
147
+ output: [:light_green, :green],
148
+ reset: [:light_cyan, :cyan],
149
+ store: [:light_cyan, :cyan],
150
+ test: [:light_white, :white],
151
+ timer: [:light_yellow, :yellow],
152
+ update: [:light_cyan, :cyan],
153
+ }
154
+ end
155
+
156
+ # Returns a formatted timestamp.
157
+ # eg. [137.7824]
158
+ #
159
+ # @return [String]
160
+ def timestamp
161
+ @now = Time.now.to_f
162
+ @time = 0.0 unless @time
163
+ @last = @now unless @last
164
+
165
+ unless @last == @time
166
+ @time += (@now - @last).round(4)
167
+ @last = @now
168
+ end
169
+
170
+ "[#{format('%7.4f', @time.to_s)}] ".rjust(7)
171
+ end
172
+
173
+ end # Eigenclass
174
+
175
+ end # Log
176
+
177
+ end # Logging
178
+
179
+ end # Vedeu
@@ -0,0 +1,26 @@
1
+ module Vedeu
2
+
3
+ module Logging
4
+
5
+ # Allows the creation of a lock-less log device.
6
+ #
7
+ class MonoLogger < ::Logger
8
+
9
+ # Create a trappable Logger instance.
10
+ #
11
+ # @param logdev [String|IO] The filename (String) or IO object
12
+ # (typically STDOUT, STDERR or an open file).
13
+ # @return [Vedeu::Logging::MonoLogger]
14
+ def initialize(logdev)
15
+ @level = Logger::DEBUG
16
+ @default_formatter = Logger::Formatter.new
17
+ @formatter = nil
18
+
19
+ @logdev = Vedeu::Logging::LocklessLogDevice.new(logdev) if logdev
20
+ end
21
+
22
+ end # MonoLogger
23
+
24
+ end # Logging
25
+
26
+ end # Vedeu
@@ -0,0 +1,68 @@
1
+ module Vedeu
2
+
3
+ module Logging
4
+
5
+ # Measure the duration. Used for debugging.
6
+ #
7
+ class Timer
8
+
9
+ class << self
10
+
11
+ # @example
12
+ # Vedeu.timer 'message' do
13
+ # # ... code to be measured
14
+ # end
15
+ #
16
+ # @param message [String]
17
+ # @param block [Proc]
18
+ # @return [void] The return value of the executed block.
19
+ def timer(message = '', &block)
20
+ new(message).measure(&block)
21
+ end
22
+
23
+ end # Eigenclass
24
+
25
+ # Returns a new instance of Vedeu::Logging::Timer.
26
+ #
27
+ # @param message [String]
28
+ # @return [Vedeu::Logging::Timer]
29
+ def initialize(message = '')
30
+ @message = message
31
+ @started = Time.now.to_f
32
+ end
33
+
34
+ # Write an entry to the log file stating how long a section of
35
+ # code took in milliseconds. Useful for debugging performance.
36
+ #
37
+ # @return [void] The return value of the executed block.
38
+ def measure
39
+ work = yield
40
+
41
+ Vedeu.log(type: :timer, message: "#{message} took #{elapsed}ms.")
42
+
43
+ work
44
+ end
45
+
46
+ protected
47
+
48
+ # @!attribute [r] started
49
+ # @return [Time]
50
+ attr_reader :started
51
+
52
+ # @!attribute [r] message
53
+ # @return [String]
54
+ attr_reader :message
55
+
56
+ # Returns the elapsed time in milliseconds with 3 decimal
57
+ # places.
58
+ #
59
+ # @return [Float]
60
+ def elapsed
61
+ ((Time.now.to_f - started) * 1000).round(3)
62
+ end
63
+
64
+ end # Timer
65
+
66
+ end # Logging
67
+
68
+ end # Vedeu
@@ -0,0 +1,12 @@
1
+ module Vedeu
2
+
3
+ module Menus
4
+
5
+ end # Menus
6
+
7
+ end # Vedeu
8
+
9
+ require 'vedeu/menus/dsl'
10
+ require 'vedeu/menus/menu'
11
+ require 'vedeu/menus/null'
12
+ require 'vedeu/menus/repository'
@@ -1,19 +1,19 @@
1
1
  module Vedeu
2
2
 
3
- module DSL
3
+ module Menus
4
4
 
5
- # Provides the mechanism to create menus within client applications and use
6
- # events to drive them.
5
+ # Provides the mechanism to create menus within client
6
+ # applications and use events to drive them.
7
7
  #
8
- class Menu
8
+ class DSL
9
9
 
10
10
  include Vedeu::DSL
11
11
 
12
- # Return a new instance of DSL::Menu.
12
+ # Return a new instance of Vedeu::Menus::DSL.
13
13
  #
14
- # @param model [Vedeu::Menu]
14
+ # @param model [Vedeu::Menus::Menu]
15
15
  # @param client [Object]
16
- # @return [Vedeu::DSL::Menu]
16
+ # @return [Vedeu::Menus::DSL]
17
17
  def initialize(model, client = nil)
18
18
  @model = model
19
19
  @client = client
@@ -21,7 +21,8 @@ module Vedeu
21
21
 
22
22
  # Add an individual item to the menu.
23
23
  #
24
- # @param element [Object] An object you wish to add to the collection.
24
+ # @param element [Object] An object you wish to add to the
25
+ # collection.
25
26
  #
26
27
  # Vedeu.menu 'my_menu' do
27
28
  # item SomeClass.new
@@ -34,11 +35,11 @@ module Vedeu
34
35
  end
35
36
  alias_method :item=, :item
36
37
 
37
- # Define the items for the menu. Most powerful when used with one of your
38
- # model classes.
38
+ # Define the items for the menu. Most powerful when used with
39
+ # one of your model classes.
39
40
  #
40
- # In the 'my_playlist' example below, your `Track` model may return a
41
- # collection of tracks to populate the menu.
41
+ # In the 'my_playlist' example below, your `Track` model may
42
+ # return a collection of tracks to populate the menu.
42
43
  #
43
44
  # Vedeu.menu 'my_menu' do
44
45
  # items [:item_1, :item_2, :item_3]
@@ -48,16 +49,16 @@ module Vedeu
48
49
  # items Track.all_my_favourites
49
50
  # end
50
51
  #
51
- # @param collection [Array<Object>] A collection of objects which make up
52
- # the menu items.
52
+ # @param collection [Array<Object>] A collection of objects
53
+ # which make up the menu items.
53
54
  # @return [Array]
54
55
  def items(collection = [])
55
56
  model.collection = collection
56
57
  end
57
58
  alias_method :items=, :items
58
59
 
59
- # The name of the menu. Used to reference the menu throughout your
60
- # application's execution lifetime.
60
+ # The name of the menu. Used to reference the menu throughout
61
+ # your application's execution lifetime.
61
62
  #
62
63
  # Vedeu.menu do
63
64
  # name 'my_menu'
@@ -71,8 +72,8 @@ module Vedeu
71
72
  end
72
73
  alias_method :name=, :name
73
74
 
74
- end # Menu
75
+ end # DSL
75
76
 
76
- end # DSL
77
+ end # Menus
77
78
 
78
79
  end # Vedeu
@@ -0,0 +1,234 @@
1
+ module Vedeu
2
+
3
+ module Menus
4
+
5
+ # Converts the collection passed into a list of menu items which
6
+ # can be navigated using the instance methods or events provided.
7
+ #
8
+ class Menu
9
+
10
+ include Vedeu::Model
11
+
12
+ # @!attribute [rw] collection
13
+ # @return [Array]
14
+ attr_accessor :collection
15
+
16
+ # Returns the index of the value in the collection which is
17
+ # current.
18
+ #
19
+ # @!attribute [rw] current
20
+ # @return [Fixnum]
21
+ attr_accessor :current
22
+
23
+ # The name of the menu. Used to reference the menu throughout
24
+ # the application's execution lifetime.
25
+ #
26
+ # @!attribute [rw] name
27
+ # @return [String]
28
+ attr_accessor :name
29
+
30
+ # Returns the index of the value in the collection which is
31
+ # selected.
32
+ #
33
+ # @!attribute [rw] selected
34
+ # @return [Fixnum]
35
+ attr_accessor :selected
36
+
37
+ # Register a menu by name which will display a collection of
38
+ # items for your users to select; and provide interactivity
39
+ # within your application.
40
+ #
41
+ # @param name [String]
42
+ # @param block [Proc] A set of attributes which define the
43
+ # features of the menu. See {Vedeu::Menus::DSL#items} and
44
+ # {Vedeu::Menus::DSL#name}.
45
+ #
46
+ # @example
47
+ # Vedeu.menu 'my_interface' do
48
+ # items [:item_1, :item_2, :item_3]
49
+ # # ...
50
+ # end
51
+ #
52
+ # Vedeu.menu do
53
+ # name 'menus_must_have_a_name'
54
+ # items Track.all_my_favourites
55
+ # # ...
56
+ # end
57
+ #
58
+ # @raise [Vedeu::Error::InvalidSyntax] The required block was
59
+ # not given.
60
+ # @return [API::Menu]
61
+ def self.menu(name = '', &block)
62
+ fail Vedeu::Error::InvalidSyntax, 'block not given' unless block_given?
63
+
64
+ build(name: name, &block).store
65
+ end
66
+
67
+ # Returns a new instance of Vedeu::Menus::Menu.
68
+ #
69
+ # @param attributes [Hash]
70
+ # @option attributes collection [Array]
71
+ # @option attributes name [String]
72
+ # @option attributes current [Fixnum]
73
+ # @option attributes selected [Fixnum|NilClass]
74
+ # @return [Vedeu::Menus::Menu]
75
+ def initialize(attributes = {})
76
+ @attributes = defaults.merge!(attributes)
77
+
78
+ @attributes.each do |key, value|
79
+ instance_variable_set("@#{key}", value)
80
+ end
81
+ end
82
+
83
+ # Returns the item from the collection which shares the same
84
+ # index as the value of {Vedeu::Menus::Menu#current}.
85
+ #
86
+ # @return [void]
87
+ def current_item
88
+ @collection[@current]
89
+ end
90
+
91
+ # Returns the item from the collection which shares the same
92
+ # index as the value of {Vedeu::Menus::Menu#selected}.
93
+ #
94
+ # @return [|NilClass]
95
+ def selected_item
96
+ return nil unless @selected
97
+
98
+ @collection[@selected]
99
+ end
100
+
101
+ # Returns a new collection of items.
102
+ # Each element of the collection is of the format:
103
+ #
104
+ # [selected, current, item]
105
+ #
106
+ # `selected` is a boolean indicating whether the item is
107
+ # selected.
108
+ # `current` is a boolean indicating whether the item is
109
+ # current.
110
+ # `item` is the item itself.
111
+ #
112
+ # @return [Array]
113
+ def items
114
+ items = []
115
+ @collection.each_with_index do |item, index|
116
+ if index == @current && index == @selected
117
+ items << [true, true, item]
118
+
119
+ elsif index == @current
120
+ items << [false, true, item]
121
+
122
+ elsif index == @selected
123
+ items << [true, false, item]
124
+
125
+ else
126
+ items << [false, false, item]
127
+
128
+ end
129
+ end
130
+ items
131
+ end
132
+
133
+ # Returns a subset of all the items.
134
+ #
135
+ # @return [Array]
136
+ def view
137
+ items[@current, @collection.size]
138
+ end
139
+
140
+ # Sets the value of current to be the first item of the
141
+ # collection.
142
+ #
143
+ # @return [Array]
144
+ def top_item
145
+ @current = 0
146
+
147
+ items
148
+ end
149
+
150
+ # Sets the value of current to be the last item of the
151
+ # collection.
152
+ #
153
+ # @return [Array]
154
+ def bottom_item
155
+ @current = last
156
+
157
+ items
158
+ end
159
+
160
+ # Sets the value of current to be the next item in the
161
+ # collection until we reach the last.
162
+ #
163
+ # @return [Array]
164
+ def next_item
165
+ @current += 1 if @current < last
166
+
167
+ items
168
+ end
169
+
170
+ # Sets the value of current to be the previous item in the
171
+ # collection until we reach the first.
172
+ #
173
+ # @return [Array]
174
+ def prev_item
175
+ @current -= 1 if @current > 0
176
+
177
+ items
178
+ end
179
+
180
+ # Sets the selected item to be the same value as the current
181
+ # item.
182
+ #
183
+ # @return [Array]
184
+ def select_item
185
+ @selected = @current
186
+
187
+ items
188
+ end
189
+
190
+ # Removes the value of `selected`, meaning no items are
191
+ # selected.
192
+ #
193
+ # @return [Array]
194
+ def deselect_item
195
+ @selected = nil
196
+
197
+ items
198
+ end
199
+
200
+ # Returns the last index of the collection.
201
+ #
202
+ # @return [Fixnum]
203
+ def last
204
+ @collection.size - 1
205
+ end
206
+
207
+ # Returns the size of the collection.
208
+ #
209
+ # @return [Fixnum]
210
+ def size
211
+ @collection.size
212
+ end
213
+
214
+ private
215
+
216
+ # The default values for a new instance of this class.
217
+ #
218
+ # @return [Hash]
219
+ def defaults
220
+ {
221
+ client: nil,
222
+ collection: [],
223
+ current: 0,
224
+ name: '',
225
+ repository: Vedeu.menus,
226
+ selected: nil,
227
+ }
228
+ end
229
+
230
+ end # Menu
231
+
232
+ end # Menus
233
+
234
+ end # Vedeu
@@ -1,23 +1,23 @@
1
1
  module Vedeu
2
2
 
3
- module Null
3
+ module Menus
4
4
 
5
5
  # Provides a non-existent model to swallow messages.
6
6
  #
7
- class Menu < Vedeu::Null::Generic
7
+ class Null < Vedeu::Null::Generic
8
8
 
9
- # Returns an instance of the Vedeu::Null::Menu class.
9
+ # Returns an instance of the Vedeu::Menus::Null class.
10
10
  #
11
11
  # @param attributes [Hash<Symbol => void>]
12
12
  # @option attributes name [String|NilClass]
13
- # @return [Vedeu::Null::Menu]
13
+ # @return [Vedeu::Menus::Null]
14
14
  def initialize(attributes = {})
15
15
  @attributes = attributes
16
16
  @name = @attributes[:name]
17
17
  end
18
18
 
19
- end # Menu
19
+ end # Null
20
20
 
21
- end # Null
21
+ end # Menus
22
22
 
23
23
  end # Vedeu
@@ -0,0 +1,18 @@
1
+ module Vedeu
2
+
3
+ module Menus
4
+
5
+ # Allows the storing of menus by name.
6
+ #
7
+ class Repository < Vedeu::Repository
8
+
9
+ singleton_class.send(:alias_method, :menus, :repository)
10
+
11
+ real Vedeu::Menus::Menu
12
+ null Vedeu::Menus::Null
13
+
14
+ end # Repository
15
+
16
+ end # Menus
17
+
18
+ end # Vedeu