Class: WAB::IO::Shell

Inherits:
Object
  • Object
show all
Includes:
ShellLogger
Defined in:
lib/wab/io/shell.rb

Overview

A Shell that uses STDIN and STDOUT for all interactions with the View and Model. Since the View and Model APIs are asynchronous and Controller calls are synchronous for simplicity some effort is required to block where needed to achieve the difference in behavior.

Instance Attribute Summary collapse

Attributes included from ShellLogger

#logger

Instance Method Summary collapse

Constructor Details

#initialize(tcnt, type_key = 'kind', path_pos = 0) ⇒ Shell

Sets up the shell with the designated number of processing threads and the type_key.

tcnt

processing thread count

type_key

key to use for the record type



24
25
26
27
28
29
30
31
32
# File 'lib/wab/io/shell.rb', line 24

def initialize(tcnt, type_key='kind', path_pos=0)
  @controllers = {}
  @type_key = type_key
  @path_pos = path_pos
  @engine = Engine.new(self, tcnt)
  @timeout = 2.0
  @logger = Logger.new(STDERR)
  logger.level = Logger::WARN
end

Instance Attribute Details

#path_posObject (readonly)

Returns the value of attribute path_pos.



15
16
17
# File 'lib/wab/io/shell.rb', line 15

def path_pos
  @path_pos
end

#timeoutObject

Returns the value of attribute timeout.



17
18
19
# File 'lib/wab/io/shell.rb', line 17

def timeout
  @timeout
end

#type_keyObject (readonly)

Returns the value of attribute type_key.



16
17
18
# File 'lib/wab/io/shell.rb', line 16

def type_key
  @type_key
end

Instance Method Details

#changed(_data) ⇒ Object

Push changed data to the view if it matches one of the subscription filters.

data: Wab::Data to push to the view if subscribed

Raises:

  • (NotImplementedError)


95
96
97
# File 'lib/wab/io/shell.rb', line 95

def changed(_data)
  raise NotImplementedError.new
end

#controller(data) ⇒ Object

Returns the controller associated with the type key found in the data. If a controller has not be registered under that key the default controller is returned if there is one.

data

data to extract the type from for lookup in the controllers



56
57
58
59
60
61
62
63
64
65
# File 'lib/wab/io/shell.rb', line 56

def controller(data)
  path = data.get(:path)
  path = path.native if path.is_a?(WAB::Data)
  return path_controller(path) unless path.nil? || (path.length <= @path_pos)

  content = data.get(:content)
  return @controllers[content.get(@type_key)] || @controllers[nil] unless content.nil?

  @controllers[nil]
end

#data(value = {}, repair = false) ⇒ Object

Create and return a new data instance with the provided initial value. The value must be a Hash or Array. The members of the Hash or Array must be nil, boolean, String, Integer, Float, BigDecimal, Array, Hash, Time, URI::HTTP, or WAB::UUID. Keys to Hashes must be Symbols.

If the repair flag is true then an attempt will be made to fix the value by replacing String keys with Symbols and calling to_h or to_s on unsupported Objects.

value

initial value

repair

flag indicating invalid value should be repaired if possible



85
86
87
# File 'lib/wab/io/shell.rb', line 85

def data(value={}, repair=false)
  WAB::Impl::Data.new(value, repair)
end

#get(ref) ⇒ Object

Returns a WAB::Data that matches the object reference or nil if there is no match.

ref

object reference

Raises:



105
106
107
108
109
110
111
112
113
# File 'lib/wab/io/shell.rb', line 105

def get(ref)
  result = query(where: ref.to_i, select: '$')
  raise WAB::Error.new("nil result get of #{ref}.") if result.nil?
  raise WAB::Error.new("error on get of #{ref}. #{result[:error]}") if 0 != result[:code]

  ra = result[:results]
  return nil if (ra.nil? || 0 == ra.length)
  ra[0]
end

#path_controller(path) ⇒ Object

Returns the controller according to the type in the path.

path: path Array such as from a URL



70
71
72
# File 'lib/wab/io/shell.rb', line 70

def path_controller(path)
  @controllers[path[@path_pos]] || @controllers[nil]
end

#query(tql) ⇒ Object

Evaluates the JSON TQL query. The TQL should be native Ruby objects that correspond to the TQL JSON format but using Symbol keys instead of strings.

tql

query to evaluate



120
121
122
# File 'lib/wab/io/shell.rb', line 120

def query(tql)
  @engine.request(tql, @timeout)
end

#register_controller(type, controller) ⇒ Object

Register a controller for a named type.

If a request is received for an unregistered type the default controller will be used. The default controller is registered with a nil key.

type

type name

controller

Controller instance for handling requests for the identified type



46
47
48
49
# File 'lib/wab/io/shell.rb', line 46

def register_controller(type, controller)
  controller.shell = self
  @controllers[type] = controller
end

#startObject

Starts listening and processing.



35
36
37
# File 'lib/wab/io/shell.rb', line 35

def start()
  @engine.start
end

#subscribe(_controller, _filter) ⇒ Object

Subscribe to changes in stored data and push changes to the controller if it passes the supplied filter.

The controller#changed method is called when changes in data cause the associated object to pass the provided filter.

controller

the controller to notify of changed

filter

the filter to apply to the data. Syntax is that TQL uses for the FILTER clause.

Raises:

  • (NotImplementedError)


132
133
134
# File 'lib/wab/io/shell.rb', line 132

def subscribe(_controller, _filter)
  raise NotImplementedError.new
end