Module: Patience::Processable

Included in:
EventHandler::Click, EventHandler::Drag, EventHandler::Drop
Defined in:
lib/patience/processable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#areaObject (readonly)

Returns the value of attribute area.



3
4
5
# File 'lib/patience/processable.rb', line 3

def area
  @area
end

#cardObject (readonly)

Returns the value of attribute card.



3
4
5
# File 'lib/patience/processable.rb', line 3

def card
  @card
end

#pileObject (readonly)

Returns the value of attribute pile.



3
4
5
# File 'lib/patience/processable.rb', line 3

def pile
  @pile
end

Instance Method Details

#detect_in(areas, option, &block) ⇒ Object

Finds area or pile, or card (depends on the option) in the areas. If the option is wrong, raises ArgumentError.



7
8
9
10
11
12
13
14
15
# File 'lib/patience/processable.rb', line 7

def detect_in(areas, option, &block)
  case option
    when :area then find_area_in(areas, &block)
    when :pile then find_pile_in(areas, &block)
    when :card then find_card_in(areas, &block)
  else
    raise ArgumentError, "Unknown option: #{option}"
  end
end

#find_area_in(areas) ⇒ Object

Returns area, which is being clicked.



18
19
20
# File 'lib/patience/processable.rb', line 18

def find_area_in(areas)
  areas.values.find { |area| yield(area) }
end

#find_card_in(areas) ⇒ Object

Returns card, which is being clicked.



32
33
34
35
36
37
38
# File 'lib/patience/processable.rb', line 32

def find_card_in(areas)
  find_pile_in(areas) do |pile|
    card = pile.cards.reverse.find { |card| yield(card) }
    return card unless card.nil?
  end
  nil
end

#find_pile_in(areas) ⇒ Object

Returns pile, which is being clicked.



23
24
25
26
27
28
29
# File 'lib/patience/processable.rb', line 23

def find_pile_in(areas)
  find_area_in(areas) do |area|
    pile = area.piles.find { |pile| yield(pile) }
    return pile unless pile.nil?
  end
  nil
end

#foundation?Boolean

Returns true, if the clicked area is Foundation.

Returns:

  • (Boolean)


82
83
84
# File 'lib/patience/processable.rb', line 82

def foundation?
  area.instance_of? Foundation
end

#nothing?Boolean

Returns true, if there hasn’t been found something.

Returns:

  • (Boolean)


51
52
53
# File 'lib/patience/processable.rb', line 51

def nothing?
  to_a.compact.size.zero?
end

#pick_up(card, mouse_pos) ⇒ Object

Returns subtraction between card and mouse position at the moment of click, only if cursor clicked the card.



62
63
64
# File 'lib/patience/processable.rb', line 62

def pick_up(card, mouse_pos)
  card.pos - mouse_pos
end

#something?Boolean

Returns true, if there’s been found something. Opposite of #nothing?.

Returns:

  • (Boolean)


56
57
58
# File 'lib/patience/processable.rb', line 56

def something?
  not nothing?
end

#stock?Boolean

Returns true, if the clicked area is Stock.

Returns:

  • (Boolean)


67
68
69
# File 'lib/patience/processable.rb', line 67

def stock?
  area.instance_of? Stock
end

#tableau?Boolean

Returns true, if the clicked area is Tableau.

Returns:

  • (Boolean)


77
78
79
# File 'lib/patience/processable.rb', line 77

def tableau?
  area.instance_of? Tableau
end

#to_aObject

Returns array, containing gathered hit elements.



41
42
43
# File 'lib/patience/processable.rb', line 41

def to_a
  [area, pile, card]
end

#to_hObject

Returns hash, containing gathered hit elements.



46
47
48
# File 'lib/patience/processable.rb', line 46

def to_h
  { :area => area, :pile => pile, :card => card }
end

#waste?Boolean

Returns true, if the clicked area is Waste.

Returns:

  • (Boolean)


72
73
74
# File 'lib/patience/processable.rb', line 72

def waste?
  area.instance_of? Waste
end