vedeu 0.1.19 → 0.2.0

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.
@@ -36,6 +36,7 @@ module Vedeu
36
36
 
37
37
  private
38
38
 
39
+ # @return [Interface]
39
40
  attr_reader :interface
40
41
 
41
42
  # @api private
@@ -31,6 +31,7 @@ module Vedeu
31
31
 
32
32
  private
33
33
 
34
+ # @return [String]
34
35
  attr_reader :name
35
36
 
36
37
  # @api private
@@ -45,6 +45,7 @@ module Vedeu
45
45
 
46
46
  private
47
47
 
48
+ # @return [Interface]
48
49
  attr_reader :interface
49
50
 
50
51
  # The client application may have created a line that us too long for the
@@ -52,7 +52,15 @@ module Vedeu
52
52
 
53
53
  # @see Vedeu::API#trigger
54
54
  def trigger(name, *args)
55
- handlers[name][:events].each { |event| event.trigger(*args) }
55
+ results = handlers[name][:events].map { |event| event.trigger(*args) }
56
+
57
+ if results.one?
58
+ results.first
59
+
60
+ else
61
+ results
62
+
63
+ end
56
64
  end
57
65
 
58
66
  # Remove all registered events. Used for testing purposes.
@@ -64,6 +72,7 @@ module Vedeu
64
72
 
65
73
  private
66
74
 
75
+ # @return [Hash]
67
76
  attr_reader :handlers
68
77
 
69
78
  # @api private
@@ -10,8 +10,10 @@ module Vedeu
10
10
  # Stores the interface attributes defined by the API.
11
11
  #
12
12
  # @param attributes [Hash]
13
- # @return [Hash]
13
+ # @return [Hash|FalseClass]
14
14
  def add(attributes)
15
+ return false if attributes[:name].empty?
16
+
15
17
  storage.store(attributes[:name], attributes)
16
18
  end
17
19
 
@@ -0,0 +1,111 @@
1
+ module Vedeu
2
+
3
+ # Repository for storing and retrieving defined menus.
4
+ #
5
+ # @api private
6
+ module Menus
7
+
8
+ extend self
9
+
10
+ # System events which when called with the appropriate menu name will
11
+ # update the menu accordingly.
12
+ Vedeu.event(:_menu_current_) { |name| Menus.use(name).current_item }
13
+ Vedeu.event(:_menu_selected_) { |name| Menus.use(name).selected_item }
14
+ Vedeu.event(:_menu_next_) { |name| Menus.use(name).next_item }
15
+ Vedeu.event(:_menu_prev_) { |name| Menus.use(name).prev_item }
16
+ Vedeu.event(:_menu_top_) { |name| Menus.use(name).top_item }
17
+ Vedeu.event(:_menu_bottom_) { |name| Menus.use(name).bottom_item }
18
+ Vedeu.event(:_menu_select_) { |name| Menus.use(name).select_item }
19
+ Vedeu.event(:_menu_deselect_) { |name| Menus.use(name).deselect_item }
20
+ Vedeu.event(:_menu_items_) { |name| Menus.use(name).items }
21
+ Vedeu.event(:_menu_view_) { |name| Menus.use(name).view }
22
+
23
+ # Stores the menu attributes defined by the API.
24
+ #
25
+ # @param attributes [Hash]
26
+ # @return [Hash|FalseClass]
27
+ def add(attributes)
28
+ return false if attributes[:name].empty?
29
+
30
+ storage.store(attributes[:name], attributes)
31
+ end
32
+
33
+ # Return the whole repository of menus.
34
+ #
35
+ # @return [Hash]
36
+ def all
37
+ storage
38
+ end
39
+
40
+ # Find a menu by name.
41
+ #
42
+ # @param name [String]
43
+ # @return [Hash]
44
+ def find(name)
45
+ storage.fetch(name) do
46
+ fail MenuNotFound,
47
+ "Menu was not found with this name: #{name.to_s}."
48
+ end
49
+ end
50
+
51
+ # Returns a collection of the names of all the registered menus.
52
+ #
53
+ # @return [Array]
54
+ def registered
55
+ storage.keys
56
+ end
57
+
58
+ # Returns a boolean indicating whether the named menu is registered.
59
+ #
60
+ # @return [TrueClass|FalseClass]
61
+ def registered?(name)
62
+ storage.key?(name)
63
+ end
64
+
65
+ # Removes the menu from the repository and associated events.
66
+ #
67
+ # @param name [String]
68
+ # @return [TrueClass|FalseClass]
69
+ def remove(name)
70
+ return false unless registered?(name)
71
+
72
+ storage.delete(name) { false }
73
+
74
+ true
75
+ end
76
+
77
+ # Reset the menus repository; removing all registered menus.
78
+ # This will delete the menus themselves, and the client application
79
+ # will need to either redefine menus before using them, or restart.
80
+ #
81
+ # @return [Hash]
82
+ def reset
83
+ @_storage = in_memory
84
+ end
85
+
86
+ # Access a menu by name.
87
+ #
88
+ # @param name [String]
89
+ # @return [Vedeu::Menu|MenuNotFound]
90
+ def use(name)
91
+ find(name).fetch(:items) do
92
+ fail MenuNotFound, "This menu '#{name}' has no items."
93
+ end
94
+ end
95
+
96
+ private
97
+
98
+ # @api private
99
+ # @return [Hash]
100
+ def storage
101
+ @_storage ||= in_memory
102
+ end
103
+
104
+ # @api private
105
+ # @return [Hash]
106
+ def in_memory
107
+ {}
108
+ end
109
+
110
+ end
111
+ end
@@ -5,7 +5,23 @@ module Vedeu
5
5
  # @api private
6
6
  class Cursor
7
7
 
8
- attr_reader :top, :bottom, :left, :right, :cursor_x, :cursor_y
8
+ # @return [Fixnum]
9
+ attr_reader :top
10
+
11
+ # @return [Fixnum]
12
+ attr_reader :bottom
13
+
14
+ # @return [Fixnum]
15
+ attr_reader :left
16
+
17
+ # @return [Fixnum]
18
+ attr_reader :right
19
+
20
+ # @return [Fixnum]
21
+ attr_reader :cursor_x
22
+
23
+ # @return [Fixnum]
24
+ attr_reader :cursor_y
9
25
 
10
26
  # Provides a new instance of Cursor.
11
27
  #
@@ -32,8 +32,17 @@ module Vedeu
32
32
 
33
33
  private
34
34
 
35
+ # @return [Proc]
35
36
  attr_reader :closure
36
- attr_accessor :deadline, :executed_at, :now
37
+
38
+ # @return []
39
+ attr_accessor :deadline
40
+
41
+ # @return []
42
+ attr_accessor :executed_at
43
+
44
+ # @return []
45
+ attr_accessor :now
37
46
 
38
47
  # Execute the code stored in the event closure.
39
48
  #
@@ -47,6 +47,7 @@ module Vedeu
47
47
 
48
48
  private
49
49
 
50
+ # @return [Fixnum]
50
51
  attr_reader :value
51
52
 
52
53
  # @api private
@@ -7,31 +7,12 @@ module Vedeu
7
7
 
8
8
  # Returns a new instance of Menu.
9
9
  #
10
- # @param collection []
10
+ # @param collection [Array]
11
11
  # @return [Menu]
12
12
  def initialize(collection)
13
13
  @collection = collection
14
14
  @current = 0
15
15
  @selected = nil
16
- @events = events
17
- end
18
-
19
- # Creates events to manipulate this menu.
20
- #
21
- # @return []
22
- def events
23
- @_events ||= Vedeu.events.add(self) do
24
- event(:menu_next) { next_item }
25
- event(:menu_prev) { prev_item }
26
- event(:menu_top) { top_item }
27
- event(:menu_bottom) { bottom_item }
28
- event(:menu_select) { select_item }
29
- event(:menu_deselect) { deselect_item }
30
-
31
- event(:menu_selected) { selected_item }
32
- event(:menu_current) { current_item }
33
- event(:menu_items) { items }
34
- end
35
16
  end
36
17
 
37
18
  # Returns the index of the value in the collection which is current.
@@ -66,8 +47,14 @@ module Vedeu
66
47
  @collection[@selected]
67
48
  end
68
49
 
69
- # Returns a new collection of items, which includes the `current` and
70
- # `selected` states.
50
+ # Returns a new collection of items.
51
+ # Each element of the collection is of the format:
52
+ #
53
+ # [ selected, current, item ]
54
+ #
55
+ # `selected` is a boolean indicating whether the item is selected.
56
+ # `current` is a boolean indicating whether the item is current.
57
+ # `item` is the item itself.
71
58
  #
72
59
  # @return [Array]
73
60
  def items
@@ -90,6 +77,8 @@ module Vedeu
90
77
  items
91
78
  end
92
79
 
80
+ # Returns a subset of all the items.
81
+ #
93
82
  # @return [Array]
94
83
  def view
95
84
  items[@current, @collection.size]
@@ -22,13 +22,6 @@ module Vedeu
22
22
  end
23
23
  end
24
24
 
25
- describe '.unevent' do
26
- it 'unregister the event by name' do
27
- Vedeu.event(:calcium) { proc { |x| x } }
28
- Vedeu.unevent(:calcium).wont_include(:calcium)
29
- end
30
- end
31
-
32
25
  describe '.events' do
33
26
  it 'returns the Events singleton' do
34
27
  Vedeu.events.must_be_instance_of(Vedeu::Events)
@@ -71,6 +64,27 @@ module Vedeu
71
64
  Vedeu.log('some message...').must_equal(nil)
72
65
  end
73
66
  end
67
+
68
+ it 'write the message to the log file when the `force` argument ' \
69
+ 'evaluates to true' do
70
+ Configuration.stub(:debug?, false) do
71
+ Vedeu.log('some message...', true).must_equal(true)
72
+ end
73
+ end
74
+ end
75
+
76
+ describe '.resize' do
77
+ it 'triggers the :_clear_ and :_refresh_ events' do
78
+ skip
79
+ end
80
+ end
81
+
82
+ describe '.menu' do
83
+ it 'creates and stores a new menu' do
84
+ Vedeu.menu('Vedeu.menu') do
85
+ # ...
86
+ end.must_be_instance_of(API::Menu)
87
+ end
74
88
  end
75
89
 
76
90
  describe '.trigger' do
@@ -80,6 +94,13 @@ module Vedeu
80
94
  end
81
95
  end
82
96
 
97
+ describe '.unevent' do
98
+ it 'unregister the event by name' do
99
+ Vedeu.event(:calcium) { proc { |x| x } }
100
+ Vedeu.unevent(:calcium).wont_include(:calcium)
101
+ end
102
+ end
103
+
83
104
  describe '.use' do
84
105
  before { Vedeu::Interfaces.reset }
85
106
 
@@ -140,10 +161,5 @@ module Vedeu
140
161
  end
141
162
  end
142
163
 
143
- describe '.resize' do
144
- it 'triggers the :_clear_ and :_refresh_ events' do
145
- skip
146
- end
147
- end
148
164
  end
149
165
  end
@@ -4,16 +4,16 @@ module Vedeu
4
4
  module API
5
5
  describe Defined do
6
6
  describe '#events' do
7
- before { Vedeu.events.reset }
8
-
9
7
  it 'returns no events when none currently registered' do
10
- Defined.events.must_equal([])
8
+ Vedeu.stub(:events, Events.new) do
9
+ Defined.events.must_equal([])
10
+ end
11
11
  end
12
12
 
13
13
  it 'returns all events currently registered' do
14
14
  Vedeu.event(:birthday) { :eat_too_much_cake }
15
15
 
16
- Defined.events.must_equal([:birthday])
16
+ Defined.events.must_include(:birthday)
17
17
  end
18
18
  end
19
19
 
@@ -0,0 +1,68 @@
1
+ require 'test_helper'
2
+
3
+ module Vedeu
4
+ module API
5
+
6
+ describe Menu do
7
+ describe '.define' do
8
+ before { Menus.reset }
9
+
10
+ it 'raises an exception if no block was given' do
11
+ proc { Menu.define }.must_raise(InvalidSyntax)
12
+ end
13
+
14
+ it 'raises an exception if no name was specified for the menu' do
15
+ proc { Menu.define { } }.must_raise(InvalidSyntax)
16
+ end
17
+
18
+ it 'adds the menu to the menus repository' do
19
+ Menu.define do
20
+ name 'elements'
21
+ items [:sodium, :magnesium, :aluminium, :silicon]
22
+ end
23
+
24
+ Vedeu::Menus.registered.must_equal(['elements'])
25
+ end
26
+
27
+ it 'returns the API::Menu instance' do
28
+ Menu.define do
29
+ name 'elements'
30
+ end.must_be_instance_of(API::Menu)
31
+ end
32
+ end
33
+
34
+ describe '#initialize' do
35
+ it 'returns an instance of itself' do
36
+ Menu.new.must_be_instance_of(Menu)
37
+ end
38
+ end
39
+
40
+ describe '#items' do
41
+ it 'returns an instance of Vedeu::Menu' do
42
+ menu = Menu.new
43
+ menu.items.must_be_instance_of(Vedeu::Menu)
44
+ end
45
+
46
+ it 'assigns the instance of Vedeu::Menu to the attributes' do
47
+ menu = Menu.new
48
+ menu.items([:sodium, :magnesium, :aluminium, :silicon])
49
+ menu.attributes[:items].must_be_instance_of(Vedeu::Menu)
50
+ end
51
+ end
52
+
53
+ describe '#name' do
54
+ it 'returns the name of the menu' do
55
+ menu = Menu.new
56
+ menu.name('elements').must_equal('elements')
57
+ end
58
+
59
+ it 'assigns the name to the attributes' do
60
+ menu = Menu.new
61
+ menu.name('elements')
62
+ menu.attributes[:name].must_equal('elements')
63
+ end
64
+ end
65
+ end
66
+
67
+ end
68
+ end