Class: Card::Set::Pattern::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/card/set_pattern.rb

Direct Known Subclasses

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(card) ⇒ Abstract

Instance methods



74
75
76
77
78
79
80
# File 'lib/card/set_pattern.rb', line 74

def initialize card
  unless self.class.anchorless?
    @anchor_name = self.class.anchor_name(card).to_name
    @anchor_id = find_anchor_id card
  end
  self
end

Class Attribute Details

.anchor_parts_countObject



60
61
62
# File 'lib/card/set_pattern.rb', line 60

def anchor_parts_count
  @anchor_parts_count ||= (anchorless? ? 0 : 1)
end

.anchorlessObject

Returns the value of attribute anchorless.



25
26
27
# File 'lib/card/set_pattern.rb', line 25

def anchorless
  @anchorless
end

.assigns_typeObject

Returns the value of attribute assigns_type.



25
26
27
# File 'lib/card/set_pattern.rb', line 25

def assigns_type
  @assigns_type
end

.junction_onlyObject

Returns the value of attribute junction_only.



25
26
27
# File 'lib/card/set_pattern.rb', line 25

def junction_only
  @junction_only
end

.pattern_codeObject

Returns the value of attribute pattern_code.



25
26
27
# File 'lib/card/set_pattern.rb', line 25

def pattern_code
  @pattern_code
end

.pattern_idObject

Returns the value of attribute pattern_id.



25
26
27
# File 'lib/card/set_pattern.rb', line 25

def pattern_id
  @pattern_id
end

Class Method Details

.anchorless?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/card/set_pattern.rb', line 48

def anchorless?
  !!anchorless
end

.junction_only?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/card/set_pattern.rb', line 44

def junction_only?
  junction_only == true
end

.module_key(anchor_codes) ⇒ Object



64
65
66
67
68
69
# File 'lib/card/set_pattern.rb', line 64

def module_key anchor_codes
  return pattern_code.to_s.camelize if anchorless?
  return unless anchor_codes # is this not an error?

  ([pattern_code] + anchor_codes).map { |code| code.to_s.camelize }.join "::"
end

.new(card) ⇒ Object



29
30
31
# File 'lib/card/set_pattern.rb', line 29

def new card
  super if pattern_applies? card
end

.patternObject



52
53
54
# File 'lib/card/set_pattern.rb', line 52

def pattern
  Card.fetch(pattern_id, skip_modules: true).name
end

.pattern_applies?(card) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/card/set_pattern.rb', line 56

def pattern_applies? card
  junction_only? ? card.name.junction? : true
end

.register(pattern_code, opts = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/card/set_pattern.rb', line 33

def register pattern_code, opts={}
  if (self.pattern_id = Card::Codename.id(pattern_code))
    self.pattern_code = pattern_code
    Card.set_patterns.insert opts.delete(:index).to_i, self
    self.anchorless = !respond_to?(:anchor_name)
    opts.each { |key, val| send "#{key}=", val }
  else
    warn "no codename for pattern_code #{pattern_code}"
  end
end

Instance Method Details

#anchor_codenamesObject



111
112
113
114
115
116
# File 'lib/card/set_pattern.rb', line 111

def anchor_codenames
  anchor_parts.map do |part|
    part_id = Card.fetch_id part
    Card::Codename[part_id] || return
  end
end

#anchor_partsObject



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/card/set_pattern.rb', line 118

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



130
131
132
# File 'lib/card/set_pattern.rb', line 130

def anchor_parts_count
  self.class.anchor_parts_count
end

#find_anchor_id(card) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/card/set_pattern.rb', line 82

def find_anchor_id card
  if self.class.respond_to? :anchor_id
    self.class.anchor_id card
  else
    Card.fetch_id @anchor_name
  end
end

#format_module_list(klass) ⇒ Object



106
107
108
109
# File 'lib/card/set_pattern.rb', line 106

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

#inspectObject



142
143
144
# File 'lib/card/set_pattern.rb', line 142

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

#lookup_module_list(modules_hash) ⇒ Object



98
99
100
# File 'lib/card/set_pattern.rb', line 98

def lookup_module_list modules_hash
  module_key && modules_hash[module_key]
end

#module_keyObject



90
91
92
93
94
95
96
# File 'lib/card/set_pattern.rb', line 90

def module_key
  if defined? @module_key
    @module_key
  else
    @module_key = self.class.module_key(anchor_codenames)
  end
end

#module_listObject



102
103
104
# File 'lib/card/set_pattern.rb', line 102

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

#patternObject



134
135
136
# File 'lib/card/set_pattern.rb', line 134

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

#rule_set_keyObject



155
156
157
158
159
160
161
# File 'lib/card/set_pattern.rb', line 155

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



146
147
148
149
150
151
152
153
# File 'lib/card/set_pattern.rb', line 146

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

#to_sObject



138
139
140
# File 'lib/card/set_pattern.rb', line 138

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