Class: RapidTransit::Action::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rapid_transit/action/base.rb

Direct Known Subclasses

ClassMethod, InstanceMethod, Setter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action_name, *args, &block) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
10
11
12
13
# File 'lib/rapid_transit/action/base.rb', line 5

def initialize(action_name, *args, &block)
  @action_name = action_name
  options = args.extract_options!
  @condition = options.delete(:if)
  @options = options
  @key = args.first
  @block = block
  @validations = :action_name, :key
end

Instance Attribute Details

#action_nameObject

Returns the value of attribute action_name.



3
4
5
# File 'lib/rapid_transit/action/base.rb', line 3

def action_name
  @action_name
end

#blockObject

Returns the value of attribute block.



3
4
5
# File 'lib/rapid_transit/action/base.rb', line 3

def block
  @block
end

#conditionObject

Returns the value of attribute condition.



3
4
5
# File 'lib/rapid_transit/action/base.rb', line 3

def condition
  @condition
end

#keyObject

Returns the value of attribute key.



3
4
5
# File 'lib/rapid_transit/action/base.rb', line 3

def key
  @key
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/rapid_transit/action/base.rb', line 3

def options
  @options
end

Instance Method Details

#apply?(row) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
# File 'lib/rapid_transit/action/base.rb', line 28

def apply?(row)
  if condition.is_a? Hash
    result = condition.all? { |k, v| row[k] == v }
    result
  elsif condition.is_a? Proc
    condition.call(row)
  else
    true
  end
end

#result(row) ⇒ Object



15
16
17
# File 'lib/rapid_transit/action/base.rb', line 15

def result(row)
  receivers(row).zip(methods, params(row)).map { |r, m, p| r.send m, *p }.last
end

#result_saved?Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/rapid_transit/action/base.rb', line 23

def result_saved?
  # Override in your subclass if the action returns a saved active record
  false
end

#valid?(actions) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/rapid_transit/action/base.rb', line 19

def valid?(actions)
  @validations.each { |v| send "validate_#{v}", actions }
end