Class: Card::Set::Pattern::Base

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Defined in:
card/lib/card/set/pattern/base.rb

Overview

class from which set patterns inherit

Direct Known Subclasses

Abstract, All, AllPlus, Right, Rstar, Rule, Self, Star, Type, TypePlusRight

Instance Attribute Summary

Attributes included from ClassMethods

#anchorless, #assigns_type, #junction_only, #pattern_code, #pattern_id

Instance Method Summary collapse

Methods included from ClassMethods

anchorless?, generic_label, junction_only?, new, pattern_applies?, register

Constructor Details

#initialize(card) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
# File 'card/lib/card/set/pattern/base.rb', line 8

def initialize card
  return if self.class.anchorless?

  @anchor_name = self.class.anchor_name(card).to_name
  @anchor_id = find_anchor_id card
end

Instance Method Details

#anchor_codenamesObject



38
39
40
41
42
# File 'card/lib/card/set/pattern/base.rb', line 38

def anchor_codenames
  anchor_parts.map do |part|
    part.cardname&.code || break
  end
end

#anchor_partsObject



44
45
46
47
48
49
50
51
52
53
54
# File 'card/lib/card/set/pattern/base.rb', line 44

def anchor_parts
  return [@anchor_name] unless anchor_parts_count > 1

  parts = @anchor_name.parts
  if parts.size <= anchor_parts_count
    parts
  else
    # handles cases where anchor is a compound card, eg A+B+*self
    [@anchor_name[0..-anchor_parts_count]] + parts[(-anchor_parts_count + 1)..-1]
  end
end

#anchor_parts_countObject



56
57
58
# File 'card/lib/card/set/pattern/base.rb', line 56

def anchor_parts_count
  self.class.anchor_parts_count
end

#find_anchor_id(card) ⇒ Object



15
16
17
# File 'card/lib/card/set/pattern/base.rb', line 15

def find_anchor_id card
  self.class.try(:anchor_id, card) || Card.fetch_id(@anchor_name)
end

#format_module_list(klass) ⇒ Object



33
34
35
36
# File 'card/lib/card/set/pattern/base.rb', line 33

def format_module_list klass
  hash = Card::Set.modules[:nonbase_format][klass]
  hash && lookup_module_list(hash)
end

#inspectObject



68
69
70
# File 'card/lib/card/set/pattern/base.rb', line 68

def inspect
  "<#{self.class} #{to_s.to_name.inspect}>"
end

#lookup_module_list(modules_hash) ⇒ Object



25
26
27
# File 'card/lib/card/set/pattern/base.rb', line 25

def lookup_module_list modules_hash
  module_key && modules_hash[module_key]
end

#module_keyObject



19
20
21
22
23
# File 'card/lib/card/set/pattern/base.rb', line 19

def module_key
  return @module_key if defined? @module_key

  @module_key = self.class.module_key anchor_codenames
end

#module_listObject



29
30
31
# File 'card/lib/card/set/pattern/base.rb', line 29

def module_list
  lookup_module_list Card::Set.modules[:nonbase]
end

#patternObject



60
61
62
# File 'card/lib/card/set/pattern/base.rb', line 60

def pattern
  @pattern ||= self.class.pattern
end

#rule_set_keyObject



77
78
79
80
81
82
83
# File 'card/lib/card/set/pattern/base.rb', line 77

def rule_set_key
  if self.class.anchorless?
    self.class.pattern_code.to_s
  elsif @anchor_id
    "#{@anchor_id}+#{self.class.pattern_code}"
  end
end

#safe_keyObject



72
73
74
75
# File 'card/lib/card/set/pattern/base.rb', line 72

def safe_key
  caps_part = self.class.pattern_code.to_s.tr(" ", "_").upcase
  self.class.anchorless? ? caps_part : "#{caps_part}-#{@anchor_name.safe_key}"
end

#to_sObject



64
65
66
# File 'card/lib/card/set/pattern/base.rb', line 64

def to_s
  self.class.anchorless? ? pattern.s : "#{@anchor_name}+#{pattern}"
end