Module: Cybertrain::Console

Defined in:
cybertrain/console.rb,
cybertrain/console/helpers.rb,
cybertrain/console/printer.rb

Overview

The expression console behind ./<app> console.

Defined Under Namespace

Classes: UnknownModel

Class Method Summary collapse

Class Method Details

.create_builder ⇒ Proc

Returns ->(name, attrs) { Model }: Model.create(attrs) for a model name, attrs a Hash{Symbol => Object}.

Returns:

  • (Proc) —

    ->(name, attrs) { Model }: Model.create(attrs) for a model name, attrs a Hash{Symbol => Object}



83
# File 'cybertrain/console.rb', line 83

def self.create_builder = @create_builder

.create_builder=(builder) ⇒ Object

Parameters:

  • builder (Proc)


87
88
89
# File 'cybertrain/console.rb', line 87

def self.create_builder=(builder)
  @create_builder = builder
end

.model_names ⇒ Array<String>

Returns the application’s model class names.

Returns:

  • (Array<String>) —

    the application’s model class names



93
# File 'cybertrain/console.rb', line 93

def self.model_names = @model_names

.model_names=(names) ⇒ Object

Parameters:

  • names (Array<String>)


97
98
99
# File 'cybertrain/console.rb', line 97

def self.model_names=(names)
  @model_names = names
end

.no_records(relation) ⇒ Array<Model>

An empty Array of records: what the default records_builder returns (no relation reaches it, since no model name resolves), and what a generated records table answers for a relation it does not know.

Parameters:

Returns:

  • (Array<Model>) —

    always empty



139
140
141
# File 'cybertrain/console.rb', line 139

def self.no_records(relation)
  Array.new(0) { Model.new }
end

.records_builder ⇒ Proc

Returns ->(relation) { Array<Model> }: the records of a relation, through its per-model to_a.

Returns:

  • (Proc) —

    ->(relation) { Array<Model> }: the records of a relation, through its per-model to_a



72
# File 'cybertrain/console.rb', line 72

def self.records_builder = @records_builder

.records_builder=(builder) ⇒ Object

Parameters:

  • builder (Proc)


76
77
78
# File 'cybertrain/console.rb', line 76

def self.records_builder=(builder)
  @records_builder = builder
end

.run(input, output, config) ⇒ Integer

Reads lines from input with gets until exit, quit or the end of input, evaluates each and writes its value (Console::Printer) to output. A line that does not parse prints its SyntaxError, a line that raises prints Class: message; both continue. _ is the last value. Production (config.production?) prints a warning first and otherwise behaves the same, as rails console does.

Parameters:

  • input (IO, StringIO) —

    anything with gets

  • output (IO, StringIO) —

    anything with write

  • config (Config) —

    Cybertrain.config

Returns:



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'cybertrain/console.rb', line 156

def self.run(input, output, config)
  helpers = Helpers.new
  interp = Template::Interpreter.new(helpers)
  env = Cast.empty_hash
  env["_"] = nil
  output.write(banner_text(config))
  # Flushed before every read: under Spinel `gets` on STDIN does not
  # flush a pending partial line on STDOUT, so the prompt would appear
  # only after the user's line (NOTES rule 83).
  output.flush
  lineno = 0
  running = true
  while running
    output.write(PROMPT)
    output.flush
    line = input.gets
    if line.nil?
      output.write("\n")
      running = false
    else
      lineno += 1
      text = line.to_s.strip
      if text == "exit" || text == "quit"
        running = false
      elsif !text.empty? && !text.start_with?("#")
        output.write(evaluate_line(text, lineno, helpers, interp, env))
      end
    end
  end
  0
end

.scope_builder ⇒ Proc

Returns ->(name) { Relation }: the relation a model name evaluates to; raises UnknownModel for any other name.

Returns:

  • (Proc) —

    ->(name) { Relation }: the relation a model name evaluates to; raises UnknownModel for any other name



61
# File 'cybertrain/console.rb', line 61

def self.scope_builder = @scope_builder

.scope_builder=(builder) ⇒ Object

Parameters:

  • builder (Proc)


65
66
67
# File 'cybertrain/console.rb', line 65

def self.scope_builder=(builder)
  @scope_builder = builder
end

.unknown_model(name) ⇒ Relation

Raises UnknownModel for name, naming the models there are: no model "Nope" (models: Article, Comment). The else of a generated scope / create table calls it. Typed as returning a Relation (never reached), so a lambda that ends in it has the type of the real builder: raise, then a typed value, so the method is never a body whose only expression is raise (NOTES rule 79).

Parameters:

  • name (String)

Returns:

  • (Relation) —

    never: it always raises

Raises:



111
112
113
114
# File 'cybertrain/console.rb', line 111

def self.unknown_model(name)
  raise UnknownModel, unknown_model_message(name)
  Relation.new("")
end

.unknown_record(name) ⇒ Model

unknown_model for create_builder: typed as returning a record (raise, then a typed value: NOTES rule 79).

Parameters:

  • name (String)

Returns:

  • (Model) —

    never: it always raises

Raises:



122
123
124
125
# File 'cybertrain/console.rb', line 122

def self.unknown_record(name)
  raise UnknownModel, unknown_model_message(name)
  Model.new
end