Module: Card::Set::Event::Options

Included in:
Card::Set::Event
Defined in:
lib/card/set/event/options.rb

Instance Method Summary collapse

Instance Method Details

#callback_name(stage, after_subcards = false) ⇒ Object



75
76
77
78
# File 'lib/card/set/event/options.rb', line 75

def callback_name stage, after_subcards=false
  name = after_subcards ? "#{stage}_final_stage" : "#{stage}_stage"
  name.to_sym
end

#event_opts(stage_or_opts, opts) ⇒ Object



47
48
49
50
51
52
# File 'lib/card/set/event/options.rb', line 47

def event_opts stage_or_opts, opts
  opts = normalize_opts stage_or_opts, opts
  process_stage_opts opts
  process_action_opts opts
  opts
end

#normalize_opts(stage_or_opts, opts) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/card/set/event/options.rb', line 54

def normalize_opts stage_or_opts, opts
  if stage_or_opts.is_a? Symbol
    opts[:in] = stage_or_opts
  else
    opts = stage_or_opts
  end
  opts
end

#process_action_opts(opts) ⇒ Object



63
64
65
# File 'lib/card/set/event/options.rb', line 63

def process_action_opts opts
  opts[:on] = %i[create update] if opts[:on] == :save
end

#process_stage_opts(opts) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/card/set/event/options.rb', line 67

def process_stage_opts opts
  if opts[:after] || opts[:before]
    # ignore :in options
  elsif (in_opt = opts.delete :in)
    opts[:after] = callback_name in_opt, opts.delete(:after_subcards)
  end
end

#valid_values(condition) ⇒ Object



43
44
45
# File 'lib/card/set/event/options.rb', line 43

def valid_values condition
  Card::Set::Event::CONDITION_OPTIONS[condition]
end

#validate_condition_name(condition) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
# File 'lib/card/set/event/options.rb', line 14

def validate_condition_name condition
  return if Card::Set::Event::CONDITIONS.include? condition

  raise ArgumentError,
        "invalid condition key '#{condition}' in event '#{@event}'\n" \
        "valid conditions are #{Card::Set::Event::CONDITIONS.to_a.join ', '}"
end

#validate_condition_value(condition, val) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/card/set/event/options.rb', line 22

def validate_condition_value condition, val
  if condition == :when
    validate_when_value val
  else
    invalid = Array.wrap(val) - Array.wrap(valid_values(condition))
    return if invalid.empty?

    raise ArgumentError,
          "invalid option#{'s' if invalid.size > 1} '#{invalid}' "\
          "for condition '#{condition}' in event '#{@event}'"
  end
end

#validate_conditionsObject



5
6
7
8
9
10
11
12
# File 'lib/card/set/event/options.rb', line 5

def validate_conditions
  @opts.each do |key, val|
    next if key.in? %i[in before after around]

    validate_condition_name key
    validate_condition_value key, val
  end
end

#validate_when_value(val) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
# File 'lib/card/set/event/options.rb', line 35

def validate_when_value val
  return if val.is_a?(Symbol) || val.is_a?(Proc)

  raise ArgumentError,
        "invalid value for condition 'when' in event '#{@event}'\n" \
        "must be a symbol or a proc"
end