class WAB::IO::Shell

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.

Attributes

path_pos[R]
timeout[RW]
type_key[R]

Public Class Methods

new(tcnt, type_key='kind', path_pos=0) click to toggle source

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

# 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

Public Instance Methods

changed(_data) click to toggle source

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

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

# File lib/wab/io/shell.rb, line 95
def changed(_data)
  raise NotImplementedError.new
end
controller(data) click to toggle source

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

# 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)] || default_controller unless content.nil?

  default_controller
end
data(value={}, repair=false) click to toggle source

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

# File lib/wab/io/shell.rb, line 85
def data(value={}, repair=false)
  WAB::Impl::Data.new(value, repair)
end
get(ref) click to toggle source

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

ref

object reference

# 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) click to toggle source

Returns the controller according to the type in the path.

path: path Array such as from a URL

# File lib/wab/io/shell.rb, line 70
def path_controller(path)
  @controllers[path[@path_pos]] || default_controller
end
query(tql) click to toggle source

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

# File lib/wab/io/shell.rb, line 120
def query(tql)
  @engine.request(tql, @timeout)
end
register_controller(type, controller) click to toggle source

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

# File lib/wab/io/shell.rb, line 46
def register_controller(type, controller)
  controller.shell = self
  @controllers[type] = controller
end
start() click to toggle source

Starts listening and processing.

# File lib/wab/io/shell.rb, line 35
def start()
  @engine.start
end
subscribe(_controller, _filter) click to toggle source

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.

# File lib/wab/io/shell.rb, line 132
def subscribe(_controller, _filter)
  raise NotImplementedError.new
end

Private Instance Methods

default_controller() click to toggle source
# File lib/wab/io/shell.rb, line 138
def default_controller
  @default_controller ||= @controllers[nil]
end