Class: MarkdownServer::CsvBrowser::RowContext
- Inherits:
-
Object
- Object
- MarkdownServer::CsvBrowser::RowContext
- Defined in:
- lib/markdown_server/csv_browser/row_context.rb
Overview
Context object passed to add-on action blocks. Exposes the active row/table/options/input/state and provides table read/write primitives that all flow through TableReader (so validation and CSV writes behave exactly like the row-editor path).
A RowContext represents one HTTP invocation. For a multi-round prompt, a fresh RowContext is created per POST; state is what carries data across rounds (round-tripping through the client).
Defined Under Namespace
Classes: StateHash
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#row ⇒ Object
readonly
Returns the value of attribute row.
-
#row_index ⇒ Object
readonly
Returns the value of attribute row_index.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #delete_row(table_name, row_index) ⇒ Object
- #done(reload: true) ⇒ Object
- #error(message, fields: nil) ⇒ Object
-
#initialize(database:, table:, row_index:, row:, options: {}, input: nil, state: nil) ⇒ RowContext
constructor
A new instance of RowContext.
- #insert_row(table_name, values, at: nil) ⇒ Object
-
#prompt(title:, fields:, state: {}) ⇒ Object
Return-value helpers — each produces the shape the HTTP layer will serialize back to the client.
-
#read_table(table_name, where: {}) ⇒ Object
List rows from any table in this database.
- #update_row(table_name, row_index, changes) ⇒ Object
Constructor Details
#initialize(database:, table:, row_index:, row:, options: {}, input: nil, state: nil) ⇒ RowContext
Returns a new instance of RowContext.
18 19 20 21 22 23 24 25 26 |
# File 'lib/markdown_server/csv_browser/row_context.rb', line 18 def initialize(database:, table:, row_index:, row:, options: {}, input: nil, state: nil) @database = database @table = table @row_index = row_index @row = row || {} @options = || {} @input = symbolize_keys(input) @state = StateHash.wrap(state) end |
Instance Attribute Details
#database ⇒ Object (readonly)
Returns the value of attribute database.
16 17 18 |
# File 'lib/markdown_server/csv_browser/row_context.rb', line 16 def database @database end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
16 17 18 |
# File 'lib/markdown_server/csv_browser/row_context.rb', line 16 def input @input end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/markdown_server/csv_browser/row_context.rb', line 16 def @options end |
#row ⇒ Object (readonly)
Returns the value of attribute row.
16 17 18 |
# File 'lib/markdown_server/csv_browser/row_context.rb', line 16 def row @row end |
#row_index ⇒ Object (readonly)
Returns the value of attribute row_index.
16 17 18 |
# File 'lib/markdown_server/csv_browser/row_context.rb', line 16 def row_index @row_index end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
16 17 18 |
# File 'lib/markdown_server/csv_browser/row_context.rb', line 16 def state @state end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
16 17 18 |
# File 'lib/markdown_server/csv_browser/row_context.rb', line 16 def table @table end |
Instance Method Details
#delete_row(table_name, row_index) ⇒ Object
54 55 56 57 |
# File 'lib/markdown_server/csv_browser/row_context.rb', line 54 def delete_row(table_name, row_index) reader = TableReader.new(resolve_table!(table_name)) reader.delete_row(row_index) end |
#done(reload: true) ⇒ Object
65 66 67 |
# File 'lib/markdown_server/csv_browser/row_context.rb', line 65 def done(reload: true) { kind: "done", reload: reload } end |
#error(message, fields: nil) ⇒ Object
69 70 71 72 73 |
# File 'lib/markdown_server/csv_browser/row_context.rb', line 69 def error(, fields: nil) h = { kind: "error", message: } h[:fields] = fields if fields h end |
#insert_row(table_name, values, at: nil) ⇒ Object
44 45 46 47 |
# File 'lib/markdown_server/csv_browser/row_context.rb', line 44 def insert_row(table_name, values, at: nil) reader = TableReader.new(resolve_table!(table_name)) reader.insert_row(stringify_values(values), at: at) end |
#prompt(title:, fields:, state: {}) ⇒ Object
Return-value helpers — each produces the shape the HTTP layer will serialize back to the client.
61 62 63 |
# File 'lib/markdown_server/csv_browser/row_context.rb', line 61 def prompt(title:, fields:, state: {}) { kind: "prompt", title: title, fields: fields, state: stringify_state(state) } end |
#read_table(table_name, where: {}) ⇒ Object
List rows from any table in this database. Returns an array of plain hashes keyed by column name (strings).
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/markdown_server/csv_browser/row_context.rb', line 30 def read_table(table_name, where: {}) t = resolve_table(table_name) return [] unless t && File.exist?(t.csv_path) rows = [] CSV.foreach(t.csv_path, headers: true) do |row| h = row.to_h next unless where.empty? || where.all? { |k, v| h[k.to_s].to_s == v.to_s } rows << h end rows end |
#update_row(table_name, row_index, changes) ⇒ Object
49 50 51 52 |
# File 'lib/markdown_server/csv_browser/row_context.rb', line 49 def update_row(table_name, row_index, changes) reader = TableReader.new(resolve_table!(table_name)) reader.update_row(row_index, stringify_values(changes)) end |