Class: Chimpactions::Action

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

Overview

Defines a single step to take given a set of conditions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Action

Returns a new instance of Action.



14
15
16
17
18
19
20
21
22
# File 'lib/chimpactions/action.rb', line 14

def initialize(params)
  if params.class.name == "Hash"
    params.each_pair do |key,val|
      self.send(key.to_s.dup << "=", val)
    end
  elsif params.class.name == "Chimpaction"
    ini_ar(params)
  end
end

Instance Attribute Details

#actionObject

The Chimpaction::Subscriber method to execute for this action.



6
7
8
# File 'lib/chimpactions/action.rb', line 6

def action
  @action
end

#isObject

Returns the value of attribute is.



10
11
12
# File 'lib/chimpactions/action.rb', line 10

def is
  @is
end

#listObject

The Chimpactions::List object to excute the action on.



8
9
10
# File 'lib/chimpactions/action.rb', line 8

def list
  @list
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

#whennObject

Returns the value of attribute whenn.



9
10
11
# File 'lib/chimpactions/action.rb', line 9

def whenn
  @whenn
end

Instance Method Details

#cast_value(val) ⇒ String, ...

Utility for casting a String to a ‘best guess’ Type

Parameters:

  • (Object)

Returns:

  • (String, Integer, TrueClass, FalseClass)

Raises:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/chimpactions/action.rb', line 56

def cast_value(val)
  raise ArgumentError.new("Your value is not a String! (it's a #{val.class})") if val.class != String && val.class != TrueClass && val.class != FalseClass
  
  if (Float val rescue false)
    Float val
  elsif (Integer val rescue false)
    Integer val
  elsif val =~ /^true$/i || val == true
    true
  elsif val =~ /^false$/i || val == false
    false
  else
    val
  end
end

#execute(subscriber) ⇒ Object



32
33
34
35
36
37
# File 'lib/chimpactions/action.rb', line 32

def execute(subscriber)
  if perform?(subscriber)
    puts "subscriber.#{action}(\"#{list}\")"
    eval "subscriber.#{action}(\"#{list}\")"
  end
end

#ini_ar(ar) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/chimpactions/action.rb', line 24

def ini_ar(ar)
    attribs = %W[action list whenn is value]
    attribs.each do |attrib|
      val = ar.send(attrib)
      self.send(attrib.to_s.dup << "=", val)
    end
end

#perform?(subscriber) ⇒ Boolean

Check the Object attributes against the specified logic.

Parameters:

  • Subscriber (String, String, Subscriber, String)

    method name, = || != || < || >, Subscriber Object, value to test against

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/chimpactions/action.rb', line 41

def perform?(subscriber)
#  puts "#{is}"
  if  is.eql?("=")
    is = '==' 
  end
 # puts "#{is}"
  value = cast_value(self.value)
  value = "\"#{value}\"" if value.class.name == "String"
#  puts "*** CHECKING : subscriber.#{whenn} #{is} #{value}"
  eval "subscriber.#{whenn} #{is} #{value}"
end