Class: Sod::Action

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sod/action.rb

Overview

A generic action (and DSL) from which to inherit and build custom actions from. :reek:TooManyInstanceVariables

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context: Context::EMPTY, model: Models::Action) ⇒ Action

Returns a new instance of Action.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sod/action.rb', line 38

def initialize context: Context::EMPTY, model: Models::Action
  klass = self.class

  @context = context

  @record = model[
    **klass.instance_variable_get(:@attributes),
    description: load(:description),
    ancillary: Array(load(:ancillary)).compact,
    default: load_default
  ]

  verify_aliases
  verify_argument
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



36
37
38
# File 'lib/sod/action.rb', line 36

def record
  @record
end

Class Method Details

.ancillary(*lines) ⇒ Object



20
21
22
# File 'lib/sod/action.rb', line 20

def self.ancillary(*lines)
  @ancillary ? fail(Error, "Ancillary can only be defined once.") : @ancillary = lines
end

.default(&block) ⇒ Object



30
31
32
# File 'lib/sod/action.rb', line 30

def self.default &block
  @default ? fail(Error, "Default can only be defined once.") : @default = block
end

.description(text) ⇒ Object



16
17
18
# File 'lib/sod/action.rb', line 16

def self.description text
  @description ? fail(Error, "Description can only be defined once.") : @description = text
end

.inherited(descendant) ⇒ Object



11
12
13
14
# File 'lib/sod/action.rb', line 11

def self.inherited descendant
  super
  descendant.class_eval { @attributes = {} }
end

.on(aliases, **keywords) ⇒ Object



24
25
26
27
28
# File 'lib/sod/action.rb', line 24

def self.on(aliases, **keywords)
  fail Error, "On can only be defined once." if @attributes.any?

  @attributes.merge! keywords, aliases: Array(aliases)
end

Instance Method Details

#callObject



54
55
56
57
# File 'lib/sod/action.rb', line 54

def call(*)
  fail NoMethodError,
       "`#{self.class}##{__method__} #{method(__method__).parameters}` must be implemented."
end

#inspectObject



59
60
61
62
# File 'lib/sod/action.rb', line 59

def inspect
  attributes = record.to_h.map { |key, value| "#{key}=#{value.inspect}" }
  %(#<#{self.class}:#{object_id} @context=#{context.inspect} #{attributes.join ", "}>)
end

#to_procObject



64
# File 'lib/sod/action.rb', line 64

def to_proc = method(:call).to_proc