Class: Punchout::Puncher

Inherits:
Object
  • Object
show all
Defined in:
lib/punchout/puncher.rb,
lib/punchout/puncher/matchable.rb,
lib/punchout/puncher/matchables.rb

Overview

Every Punchable has a Puncher, which keeps the details of implementation from littering the namespace of the Punchable

Defined Under Namespace

Classes: Matchable, Matchables

Instance Method Summary collapse

Constructor Details

#initializePuncher

Returns a new instance of Puncher.



8
9
10
# File 'lib/punchout/puncher.rb', line 8

def initialize
  @matchables = Matchables.new
end

Instance Method Details

#add(matchable) ⇒ Object

Adds passed Matchable to the list of things this Punchout::Puncher can punch

on behalf of

Parameters:

  • matchable (Matchable)

    a thing this puncher can match on and punch

Raises:

  • (StandardError)

    if matchable isn’t a Matchable



19
20
21
22
23
24
25
# File 'lib/punchout/puncher.rb', line 19

def add(matchable)
  if !matchable.kind_of?(Matchable)
    raise
  end

  @matchables.add(matchable)
end

#allArray

Returns all of the Classes that can be produced by this Punchout::Puncher.

Returns:



36
37
38
39
40
# File 'lib/punchout/puncher.rb', line 36

def all
  @matchables.all.map do |m|
    m.thing
  end
end

#can_punch?(type) ⇒ Boolean

Indicates whether passed Class is something this Punchout::Puncher would punch if stimulated appropriately

Returns:

  • (Boolean)


29
30
31
# File 'lib/punchout/puncher.rb', line 29

def can_punch?(type)
  @matchables.include?(type)
end

#punch(type) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/punchout/puncher.rb', line 42

def punch(type)

  match = @matchables.find(type)

  if match
    match.thing
  end
end

#punch!(type) ⇒ Object



51
52
53
# File 'lib/punchout/puncher.rb', line 51

def punch!(type)
  punch(type) or raise
end