Class: CommandDeck::Executor
- Inherits:
-
Object
- Object
- CommandDeck::Executor
- Defined in:
- lib/command_deck/executor.rb
Overview
Executor for running actions
Class Method Summary collapse
- .boolean_true?(value) ⇒ Boolean
- .call(key:, params:, request: nil) ⇒ Object
- .coerce(schema, raw) ⇒ Object
-
.coerce_boolean(value) ⇒ Object
rubocop:disable Naming/PredicateMethod.
- .coerce_integer(value) ⇒ Object
- .coerce_selector(value) ⇒ Object
- .coerce_string(value) ⇒ Object
- .coerce_value(type, value) ⇒ Object
- .integer_or_nil(value) ⇒ Object
- .symbolize_keys_shallow(hash) ⇒ Object
Class Method Details
.boolean_true?(value) ⇒ Boolean
59 60 61 62 63 |
# File 'lib/command_deck/executor.rb', line 59 def self.boolean_true?(value) return false if value.nil? value == true || %w[true 1 on].include?(value.to_s.strip.downcase) end |
.call(key:, params:, request: nil) ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/command_deck/executor.rb', line 6 def self.call(key:, params:, request: nil) action = Registry.find_action(key) raise ArgumentError, "Unknown action #{key}" unless action coerced = coerce(action.params, params || {}) ctx = request ? CommandDeck.configuration.build_context(request) : {} action.block.call(coerced, ctx) end |
.coerce(schema, raw) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/command_deck/executor.rb', line 15 def self.coerce(schema, raw) return {} if schema.nil? || schema.empty? schema.each_with_object({}) do |param, out| name = param[:name] value = raw[name] || raw[name.to_s] out[name] = coerce_value(param[:type], value) end end |
.coerce_boolean(value) ⇒ Object
rubocop:disable Naming/PredicateMethod
34 35 36 |
# File 'lib/command_deck/executor.rb', line 34 def self.coerce_boolean(value) # rubocop:disable Naming/PredicateMethod boolean_true?(value) end |
.coerce_integer(value) ⇒ Object
38 39 40 |
# File 'lib/command_deck/executor.rb', line 38 def self.coerce_integer(value) integer_or_nil(value) end |
.coerce_selector(value) ⇒ Object
42 43 44 45 46 |
# File 'lib/command_deck/executor.rb', line 42 def self.coerce_selector(value) return symbolize_keys_shallow(value) if value.is_a?(Hash) value end |
.coerce_string(value) ⇒ Object
48 49 50 |
# File 'lib/command_deck/executor.rb', line 48 def self.coerce_string(value) value&.to_s end |
.coerce_value(type, value) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/command_deck/executor.rb', line 25 def self.coerce_value(type, value) case type when :boolean then coerce_boolean(value) when :integer then coerce_integer(value) when :selector then coerce_selector(value) else coerce_string(value) end end |
.integer_or_nil(value) ⇒ Object
65 66 67 68 69 |
# File 'lib/command_deck/executor.rb', line 65 def self.integer_or_nil(value) return nil if value.nil? || value == "" value.to_i end |
.symbolize_keys_shallow(hash) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/command_deck/executor.rb', line 52 def self.symbolize_keys_shallow(hash) hash.each_with_object({}) do |(k, v), out| key = k.respond_to?(:to_sym) ? k.to_sym : k out[key] = v end end |