Class: Bridge::Trick

Inherits:
Object
  • Object
show all
Defined in:
lib/bridge/trick.rb

Constant Summary collapse

REQUIRED_CARDS =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Trick

Returns a new instance of Trick.



6
7
8
9
# File 'lib/bridge/trick.rb', line 6

def initialize(params = {})
  params.map { |k,v| self.send(:"#{k}=",v) }
  self.cards = [] if self.cards.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/bridge/trick.rb', line 19

def method_missing(method, *args, &block)
  begin
    self.cards = self.cards.to_s.split(' ').map { |c| Card.from_string(c) } unless self.cards.class == Array
    self.cards.send(method, *args, &block)
  rescue Exception => e
    super
  end
end

Instance Attribute Details

#cardsObject

Returns the value of attribute cards.



4
5
6
# File 'lib/bridge/trick.rb', line 4

def cards
  @cards
end

#leaderObject

Returns the value of attribute leader.



5
6
7
# File 'lib/bridge/trick.rb', line 5

def leader
  @leader
end

Instance Method Details

#done?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/bridge/trick.rb', line 11

def done?
  self.cards.compact.size >= REQUIRED_CARDS
end

#leader_cardObject



15
16
17
# File 'lib/bridge/trick.rb', line 15

def leader_card
  self.cards[self.leader]
end