vedeu 0.0.38 → 0.0.39

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/deps.md +24 -11
  3. data/lib/vedeu/input/input.rb +2 -2
  4. data/lib/vedeu/launcher.rb +2 -0
  5. data/lib/vedeu/models/builders/builder.rb +41 -0
  6. data/lib/vedeu/models/builders/command_builder.rb +4 -25
  7. data/lib/vedeu/models/builders/interface_builder.rb +6 -26
  8. data/lib/vedeu/models/interface.rb +2 -2
  9. data/lib/vedeu/output/clear_interface.rb +10 -2
  10. data/lib/vedeu/parsing/text_adaptor.rb +7 -3
  11. data/lib/vedeu/repository/command_repository.rb +6 -6
  12. data/lib/vedeu/repository/interface_repository.rb +2 -18
  13. data/lib/vedeu/repository/repository.rb +25 -9
  14. data/lib/vedeu/repository/storage.rb +15 -20
  15. data/lib/vedeu/support/events.rb +58 -0
  16. data/lib/vedeu/support/exit.rb +2 -2
  17. data/lib/vedeu/support/{coordinate.rb → geometry.rb} +1 -1
  18. data/lib/vedeu/support/menu.rb +34 -6
  19. data/lib/vedeu/support/terminal.rb +1 -1
  20. data/lib/vedeu/support/translator.rb +2 -1
  21. data/lib/vedeu.rb +21 -7
  22. data/test/lib/vedeu/models/builders/builder_test.rb +11 -0
  23. data/test/lib/vedeu/models/interface_test.rb +3 -3
  24. data/test/lib/vedeu/parsing/text_adaptor_test.rb +24 -0
  25. data/test/lib/vedeu/repository/command_repository_test.rb +3 -11
  26. data/test/lib/vedeu/repository/interface_repository_test.rb +0 -23
  27. data/test/lib/vedeu/repository/repository_test.rb +45 -22
  28. data/test/lib/vedeu/repository/storage_test.rb +44 -19
  29. data/test/lib/vedeu/support/compositor_test.rb +2 -2
  30. data/test/lib/vedeu/support/events_test.rb +28 -0
  31. data/test/lib/vedeu/support/{coordinate_test.rb → geometry_test.rb} +110 -110
  32. data/test/lib/vedeu/support/menu_test.rb +69 -31
  33. data/vedeu.gemspec +1 -1
  34. metadata +11 -8
  35. data/lib/vedeu/repository/event_repository.rb +0 -35
  36. data/test/lib/vedeu/repository/event_repository_test.rb +0 -31
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 661482d026960734c10b12e030206f9570fe369a
4
- data.tar.gz: 021507e23ec529f132e3da33e03ab7e537fa9e76
3
+ metadata.gz: 3f1858b317f4c614c4171d5d1032e7f512f093a6
4
+ data.tar.gz: b33301029f63c79baeef259eac052cd109634415
5
5
  SHA512:
6
- metadata.gz: fe71eb666f8f5a6353e6b76b945498c6407c8d83a797e5bbcaf2ceb4b7e9f698e85db442afdfde87c04b151af13b187fd3f8ec4e3aa7adb9daa9b60a202bc1bf
7
- data.tar.gz: c14259a37b32d99a84dda5d989cff126b3e9365f1dfd3d1e30abb725d9b1c58f7b07232243b811600ccdfadf810d6e3cef032c6b93132e478cd6dc22b771c4af
6
+ metadata.gz: cd006607dbf4753ca9a488298d6054e68db1425a4807ad058cbbc4c3d779b45ae030f9e4ca7d4b1db611c12ac70abfb44cc6b81d2f523ba09a916649a64181e5
7
+ data.tar.gz: be1a3ab07377b3385f7d0b2611989edc59c6cb84b6ecfeecf40e2b843c2dd4a344a67e3ab153bfc9b6e9dd2a3de75dae407ecdd3b11cf52dde47240660f82e59
data/deps.md CHANGED
@@ -8,6 +8,8 @@ Application
8
8
  Process
9
9
  Terminal
10
10
 
11
+ Builder
12
+
11
13
  ClearInterface
12
14
 
13
15
  Collection
@@ -17,6 +19,10 @@ Colour
17
19
 
18
20
  Command
19
21
 
22
+ CommandBuilder
23
+ Builder
24
+ CommandRepository
25
+
20
26
  CommandRepository
21
27
  Command
22
28
  Repository
@@ -29,29 +35,27 @@ Compositor
29
35
 
30
36
  Configuration
31
37
 
32
- Coordinate
38
+ Geometry
33
39
  Esc
34
40
  Terminal
35
41
 
36
42
  Esc
37
43
  Translator
38
44
 
39
- EventRepository
45
+ Events
40
46
 
41
47
  Exit
42
- EventRepository
43
48
 
44
49
  HashParser
45
50
  TextAdaptor
46
51
 
47
52
  Input
48
- EventRepository
49
53
  Queue
50
54
  Terminal
51
55
 
52
56
  Interface
53
57
  ClearInterface
54
- Coordinate
58
+ Geometry
55
59
  LineCollection
56
60
  Presentation
57
61
  Queue
@@ -59,6 +63,11 @@ Interface
59
63
  Style
60
64
  Terminal
61
65
 
66
+ InterfaceBuilder
67
+ Builder
68
+ Geometry
69
+ InterfaceRepository
70
+
62
71
  InterfaceCollection
63
72
  InterfaceRepository
64
73
 
@@ -141,19 +150,23 @@ Orphans
141
150
  Template - orphaned
142
151
  Wordwrap - orphaned
143
152
  Menu - orphaned
144
-
145
153
  Exit - orphaned
146
- EventRepository
147
-
148
154
 
149
155
  ----------------------------------------------------------------------
150
156
  Grouped
151
157
  ----------------------------------------------------------------------
152
158
 
159
+ CommandBuilder
160
+ Builder
161
+
162
+ InterfaceBuilder
163
+ Builder
164
+ Geometry
165
+ InterfaceRepository
166
+
153
167
  Launcher
154
168
  Application
155
169
  Input
156
- EventRepository
157
170
  Queue
158
171
  Terminal
159
172
  Esc
@@ -162,7 +175,7 @@ Launcher
162
175
  InterfaceRepository
163
176
  Interface
164
177
  ClearInterface
165
- Coordinate
178
+ Geometry
166
179
  Esc
167
180
  Translator
168
181
  Terminal
@@ -218,7 +231,7 @@ Launcher
218
231
  InterfaceRepository
219
232
  Interface
220
233
  ClearInterface
221
- Coordinate
234
+ Geometry
222
235
  Esc
223
236
  Translator
224
237
  Terminal
@@ -1,6 +1,6 @@
1
1
  require_relative '../support/queue'
2
2
  require_relative '../support/terminal'
3
- require_relative '../repository/event_repository'
3
+ require_relative '../../vedeu'
4
4
 
5
5
  module Vedeu
6
6
  class Input
@@ -9,7 +9,7 @@ module Vedeu
9
9
  end
10
10
 
11
11
  def capture
12
- EventRepository.trigger(:_keypress_, keypress)
12
+ Vedeu.trigger(:_keypress_, keypress)
13
13
 
14
14
  Queue.enqueue(keypress)
15
15
  end
@@ -19,6 +19,8 @@ module Vedeu
19
19
  def execute!
20
20
  $stdin, $stdout, $stderr = @stdin, @stdout, @stderr
21
21
 
22
+ $stderr.reopen("logs/vedeu.log", "w")
23
+
22
24
  Application.start(configuration)
23
25
 
24
26
  @exit_code = 0
@@ -0,0 +1,41 @@
1
+ module Vedeu
2
+ class Builder
3
+ def self.build(name, &block)
4
+ new(name).build(&block)
5
+ end
6
+
7
+ def initialize(name)
8
+ @name = name.to_s
9
+ end
10
+
11
+ def build(&block)
12
+ self.instance_eval(&block)
13
+
14
+ repository.create(attributes)
15
+ end
16
+
17
+ def repository
18
+ fail StandardError, 'Subclasses implement this.'
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :name
24
+
25
+ def attributes
26
+ user_attributes.merge!(overrides)
27
+ end
28
+
29
+ def overrides
30
+ {}
31
+ end
32
+
33
+ def user_attributes
34
+ @attributes ||= { name: name }
35
+ end
36
+
37
+ def method_missing(method_name, arg, &block)
38
+ user_attributes[method_name] = arg
39
+ end
40
+ end
41
+ end
@@ -1,31 +1,10 @@
1
+ require_relative 'builder'
1
2
  require_relative '../../repository/command_repository'
2
3
 
3
4
  module Vedeu
4
- class CommandBuilder
5
- def self.build(name, &block)
6
- new(name).build(&block)
7
- end
8
-
9
- def initialize(name)
10
- @name = name.to_s
11
- end
12
-
13
- def build(&block)
14
- self.instance_eval(&block)
15
-
16
- CommandRepository.create(attributes)
17
- end
18
-
19
- private
20
-
21
- attr_reader :name
22
-
23
- def attributes
24
- @attributes ||= { name: name }
25
- end
26
-
27
- def method_missing(method_name, arg, &block)
28
- attributes[method_name] = arg
5
+ class CommandBuilder < Builder
6
+ def repository
7
+ CommandRepository
29
8
  end
30
9
  end
31
10
  end
@@ -1,31 +1,15 @@
1
- require_relative '../colour'
2
- require_relative '../../support/coordinate'
1
+ require_relative 'builder'
2
+ require_relative '../../support/geometry'
3
3
  require_relative '../../repository/interface_repository'
4
4
 
5
5
  module Vedeu
6
- class InterfaceBuilder
7
- def self.build(name, &block)
8
- new(name).build(&block)
9
- end
10
-
11
- def initialize(name)
12
- @name = name.to_s
13
- end
14
-
15
- def build(&block)
16
- self.instance_eval(&block)
17
-
18
- InterfaceRepository.create(attributes)
6
+ class InterfaceBuilder < Builder
7
+ def repository
8
+ InterfaceRepository
19
9
  end
20
10
 
21
11
  private
22
12
 
23
- attr_reader :name
24
-
25
- def attributes
26
- user_attributes.merge!(overrides)
27
- end
28
-
29
13
  def overrides
30
14
  @overrides = if user_attributes[:centred] == true
31
15
  { x: geometry.left, y: geometry.top }
@@ -35,16 +19,12 @@ module Vedeu
35
19
  end
36
20
 
37
21
  def geometry
38
- @_geometry ||= Coordinate.new({
22
+ @_geometry ||= Geometry.new({
39
23
  height: user_attributes[:height],
40
24
  width: user_attributes[:width],
41
25
  })
42
26
  end
43
27
 
44
- def user_attributes
45
- @attributes ||= { name: name, centred: false }
46
- end
47
-
48
28
  def method_missing(method_name, arg, &block)
49
29
  user_attributes[method_name] = arg
50
30
  end
@@ -6,7 +6,7 @@ require_relative 'presentation'
6
6
  require_relative 'style'
7
7
  require_relative '../output/clear_interface'
8
8
  require_relative '../output/render_interface'
9
- require_relative '../support/coordinate'
9
+ require_relative '../support/geometry'
10
10
  require_relative '../support/queue'
11
11
  require_relative '../support/terminal'
12
12
 
@@ -33,7 +33,7 @@ module Vedeu
33
33
  end
34
34
 
35
35
  def geometry
36
- @_geometry ||= Coordinate.new(attributes)
36
+ @_geometry ||= Geometry.new(attributes)
37
37
  end
38
38
 
39
39
  def origin(index = 0)
@@ -9,9 +9,9 @@ module Vedeu
9
9
  end
10
10
 
11
11
  def clear
12
- interface.height.times.inject([interface.colour.to_s]) do |line, index|
12
+ interface_lines.inject([colours]) do |line, index|
13
13
  line << interface.origin(index)
14
- line << (' ' * interface.width)
14
+ line << ' ' * interface.width
15
15
  line << interface.origin(index)
16
16
  end.join
17
17
  end
@@ -19,5 +19,13 @@ module Vedeu
19
19
  private
20
20
 
21
21
  attr_reader :interface
22
+
23
+ def colours
24
+ interface.colour.to_s
25
+ end
26
+
27
+ def interface_lines
28
+ interface.height.times
29
+ end
22
30
  end
23
31
  end
@@ -2,8 +2,8 @@ module Vedeu
2
2
  class TextAdaptor
3
3
  # Convert a block of text into a collection of lines.
4
4
  #
5
- # @param text [String] a block of text containing new line (\n)
6
- # characters.
5
+ # @param text [String|Array] a block of text containing new line (\n)
6
+ # characters, or a collection of strings.
7
7
  #
8
8
  # @return [Array]
9
9
  def self.adapt(text)
@@ -25,7 +25,11 @@ module Vedeu
25
25
  attr_reader :text
26
26
 
27
27
  def lines
28
- text.split(/\n/)
28
+ if text.is_a?(::Array)
29
+ text
30
+ else
31
+ text.split(/\n/)
32
+ end
29
33
  end
30
34
 
31
35
  def no_content?
@@ -10,10 +10,6 @@ module Vedeu
10
10
  by_keypress(input) || by_keyword(input)
11
11
  end
12
12
 
13
- def create(attributes)
14
- super(entity.new(attributes))
15
- end
16
-
17
13
  def entity
18
14
  Command
19
15
  end
@@ -21,11 +17,15 @@ module Vedeu
21
17
  private
22
18
 
23
19
  def by_keypress(input)
24
- query(entity, :keypress, input)
20
+ query(:keypress, input)
21
+ rescue EntityNotFound
22
+ false
25
23
  end
26
24
 
27
25
  def by_keyword(input)
28
- query(entity, :keyword, input)
26
+ query(:keyword, input)
27
+ rescue EntityNotFound
28
+ false
29
29
  end
30
30
  end
31
31
  end
@@ -2,31 +2,15 @@ require_relative '../models/interface'
2
2
  require_relative 'repository'
3
3
 
4
4
  module Vedeu
5
- class UndefinedInterface < StandardError; end
6
-
7
5
  module InterfaceRepository
8
6
  extend Repository
9
7
  extend self
10
8
 
11
- def create(attributes = {})
12
- super(entity.new(attributes))
13
- end
14
-
15
- def find(name)
16
- if result = query(entity, :name, name)
17
- result
18
-
19
- else
20
- fail UndefinedInterface, "#{name.to_s} could not be found."
21
-
22
- end
23
- end
24
-
25
9
  def update(name, attributes = {})
26
- interface = find(name)
10
+ interface = query(:name, name)
27
11
  interface.attributes = attributes
28
12
  interface
29
- rescue UndefinedInterface
13
+ rescue EntityNotFound
30
14
  create(attributes)
31
15
  end
32
16
 
@@ -1,25 +1,41 @@
1
1
  require_relative 'storage'
2
2
 
3
3
  module Vedeu
4
+ EntityNotFound = Class.new(StandardError)
5
+
4
6
  module Repository
5
- def adaptor
6
- @adaptor ||= Storage.new
7
+ extend self
8
+
9
+ def create(attributes)
10
+ storage.create(entity, attributes)
7
11
  end
8
12
 
9
13
  def all
10
- adaptor.all(entity)
14
+ storage.all(entity)
11
15
  end
12
16
 
13
- def query(entity, attribute, value)
14
- adaptor.query(entity, attribute, value)
15
- end
17
+ def query(attribute, value)
18
+ if result = storage.query(entity, attribute, value)
19
+ result
16
20
 
17
- def create(model)
18
- adaptor.create(model)
21
+ else
22
+ fail EntityNotFound, "#{entity.to_s} could not be found."
23
+
24
+ end
19
25
  end
20
26
 
21
27
  def reset
22
- adaptor.reset(entity)
28
+ storage.reset(entity)
29
+ end
30
+
31
+ def entity
32
+ fail StandardError, 'The extending module implements this.'
33
+ end
34
+
35
+ private
36
+
37
+ def storage
38
+ @storage ||= Storage.new
23
39
  end
24
40
  end
25
41
  end
@@ -1,45 +1,40 @@
1
1
  module Vedeu
2
2
  class Storage
3
3
  def initialize
4
- @map = {}
4
+ @store = {}
5
5
  end
6
6
 
7
- def create(record)
8
- map_for(record)[record.name] = record
7
+ def create(entity, attributes)
8
+ entities(entity)
9
+ .store(attributes[:name], entity.new(attributes))
9
10
  end
10
11
 
11
- def delete(record)
12
- map_for(record).delete(record.name)
13
- end
12
+ def all(entity = nil)
13
+ return entities unless entity
14
14
 
15
- def reset(entity)
16
- all(entity).map { |record| delete(record) }
17
- end
18
-
19
- def all(entity)
20
- map_for_class(entity).values
15
+ entities(entity).values
21
16
  end
22
17
 
23
18
  def query(entity, attribute, value)
24
19
  return false if value.nil? || value.empty?
25
20
 
26
- map_for_class(entity).select do |_, result|
21
+ entities(entity).select do |name, result|
27
22
  return result if result.send(attribute) == value
28
23
  end
29
24
 
30
25
  false
31
26
  end
32
27
 
33
- private
28
+ def reset(entity)
29
+ store[entity.to_s] = {}
30
+ end
34
31
 
35
- attr_reader :map
32
+ private
36
33
 
37
- def map_for_class(entity)
38
- map[entity.to_s.to_sym] ||= {}
39
- end
34
+ attr_reader :store
40
35
 
41
- def map_for(record)
42
- map_for_class(record.class)
36
+ def entities(entity)
37
+ store[entity.to_s] ||= {}
43
38
  end
44
39
  end
45
40
  end
@@ -0,0 +1,58 @@
1
+ module Vedeu
2
+ class Events
3
+ def initialize(&block)
4
+ @handlers = Hash.new { |h, k| h[k] = [] }
5
+
6
+ log("Events#initialize " \
7
+ "self: #{self.object_id}")
8
+
9
+ self.instance_eval(&block) if block_given?
10
+
11
+ self
12
+ end
13
+
14
+ def add(object, &block)
15
+ log("Events#add " \
16
+ "self: #{self.object_id}" \
17
+ "menu: #{object.object_id}")
18
+
19
+ @self_before_instance_eval = eval "self", block.binding
20
+
21
+ self.instance_eval(&block)
22
+ end
23
+
24
+ def on(event, &block)
25
+ log("Events#on " \
26
+ "self: #{self.object_id} " \
27
+ "block: #{block.object_id} " \
28
+ "event: #{event.inspect}")
29
+ handlers[event] << block
30
+ end
31
+
32
+ def trigger(event, *args)
33
+ handlers[event].each do |handler|
34
+ log("Events#trigger " \
35
+ "self: #{self.object_id} " \
36
+ "handler: #{handler.object_id} " \
37
+ "event: #{event.inspect}")
38
+ handler.call(*args)
39
+ end
40
+ end
41
+
42
+ def method_missing(method, *args, &block)
43
+ @self_before_instance_eval.send method, *args, &block
44
+ end
45
+
46
+ private
47
+
48
+ attr_reader :handlers
49
+
50
+ def log(message)
51
+ Vedeu.trigger(:_log_, message) if debug?
52
+ end
53
+
54
+ def debug?
55
+ false
56
+ end
57
+ end
58
+ end
@@ -1,9 +1,9 @@
1
- require_relative '../repository/event_repository'
1
+ require_relative '../../vedeu'
2
2
 
3
3
  module Vedeu
4
4
  class Exit
5
5
  def self.dispatch
6
- EventRepository.trigger(:_exit_)
6
+ Vedeu.trigger(:_exit_)
7
7
  end
8
8
  end
9
9
  end
@@ -4,7 +4,7 @@ require_relative 'terminal'
4
4
  module Vedeu
5
5
  class OutOfBoundsError < StandardError; end
6
6
 
7
- class Coordinate
7
+ class Geometry
8
8
  def initialize(attrs = {})
9
9
  @attrs = attrs
10
10
  @height = attrs.fetch(:height)
@@ -1,9 +1,29 @@
1
+ require_relative '../../vedeu'
2
+ require_relative 'events'
3
+
1
4
  module Vedeu
2
5
  class Menu
3
6
  def initialize(collection)
4
7
  @collection = collection
5
8
  @current = 0
6
9
  @selected = nil
10
+ @events = events
11
+ end
12
+
13
+ def events
14
+ @_events ||= Vedeu.events.add(self) do
15
+ on(:menu_next) { next_item }
16
+ on(:menu_prev) { prev_item }
17
+ on(:menu_top) { top_item }
18
+ on(:menu_bottom) { bottom_item }
19
+ on(:menu_select) { select_item }
20
+ on(:menu_deselect) { deselect_item }
21
+
22
+ on(:menu_selected) { selected_item }
23
+ on(:menu_current) { current_item }
24
+ on(:menu_items) { items }
25
+ on(:menu_render) { render }
26
+ end
7
27
  end
8
28
 
9
29
  def current
@@ -64,32 +84,40 @@ module Vedeu
64
84
  lines
65
85
  end
66
86
 
67
- def top
87
+ def top_item
68
88
  @current = 0
89
+
90
+ self
69
91
  end
70
92
 
71
- def bottom
93
+ def bottom_item
72
94
  @current = last
95
+
96
+ self
73
97
  end
74
98
 
75
- def next
99
+ def next_item
76
100
  @current += 1 if @current < last
77
101
 
78
102
  self
79
103
  end
80
104
 
81
- def prev
105
+ def prev_item
82
106
  @current -= 1 if @current > 0
83
107
 
84
108
  self
85
109
  end
86
110
 
87
- def select
111
+ def select_item
88
112
  @selected = @current
113
+
114
+ self
89
115
  end
90
116
 
91
- def deselect
117
+ def deselect_item
92
118
  @selected = nil
119
+
120
+ self
93
121
  end
94
122
 
95
123
  def last
@@ -1,6 +1,6 @@
1
1
  require 'io/console'
2
2
 
3
- require_relative '../support/esc'
3
+ require_relative 'esc'
4
4
 
5
5
  module Vedeu
6
6
  module Terminal