Class: Card::SetPattern

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(card) ⇒ SetPattern

Instance methods



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

def initialize card
  unless self.class.anchorless?
    @anchor_name = self.class.anchor_name(card).to_name
    @anchor_id = if self.class.respond_to? :anchor_id
                   self.class.anchor_id card
                 else
                   Card.fetch_id @anchor_name
                 end
  end
  self
end

Class Attribute Details

.anchor_parts_countObject

Returns the value of attribute anchor_parts_count.



4
5
6
# File 'lib/card/set_pattern.rb', line 4

def anchor_parts_count
  @anchor_parts_count
end

.anchorlessObject

Returns the value of attribute anchorless.



4
5
6
# File 'lib/card/set_pattern.rb', line 4

def anchorless
  @anchorless
end

.assigns_typeObject

Returns the value of attribute assigns_type.



4
5
6
# File 'lib/card/set_pattern.rb', line 4

def assigns_type
  @assigns_type
end

.junction_onlyObject

Returns the value of attribute junction_only.



4
5
6
# File 'lib/card/set_pattern.rb', line 4

def junction_only
  @junction_only
end

.pattern_codeObject

Returns the value of attribute pattern_code.



4
5
6
# File 'lib/card/set_pattern.rb', line 4

def pattern_code
  @pattern_code
end

.pattern_idObject

Returns the value of attribute pattern_id.



4
5
6
# File 'lib/card/set_pattern.rb', line 4

def pattern_id
  @pattern_id
end

Class Method Details

.anchorless?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/card/set_pattern.rb', line 23

def anchorless?
  !!anchorless
end

.card_keysObject



7
8
9
10
11
12
13
# File 'lib/card/set_pattern.rb', line 7

def card_keys
  @@card_keys ||= Card.set_patterns.inject({}) do |hash, set_pattern|
    card_key = Card.quick_fetch(set_pattern.pattern_code.to_sym).key
    hash[card_key] = true
    hash
  end
end

.find(pattern_code) ⇒ Object



15
16
17
# File 'lib/card/set_pattern.rb', line 15

def find pattern_code
  Card.set_patterns.find { |sub| sub.pattern_code == pattern_code }
end

.junction_only?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/card/set_pattern.rb', line 19

def junction_only?
  !!junction_only
end

.new(card) ⇒ Object



27
28
29
# File 'lib/card/set_pattern.rb', line 27

def new card
  super if pattern_applies? card
end

.patternObject



31
32
33
# File 'lib/card/set_pattern.rb', line 31

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

.pattern_applies?(card) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/card/set_pattern.rb', line 46

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

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



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

def register pattern_code, opts={}
  if (self.pattern_id = Card::Codename[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

.write_tmp_file(pattern_code, from_file, seq) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/card/set_pattern.rb', line 54

def write_tmp_file pattern_code, from_file, seq
  to_file = "#{Card.paths['tmp/set_pattern'].first}/" \
            "#{seq}-#{pattern_code}.rb"
  klass = "Card::#{pattern_code.camelize}Set"
  file_content = <<EOF
# -*- encoding : utf-8 -*-
class #{klass} < Card::SetPattern
  cattr_accessor :options
  class << self
# ~~~~~~~~~~~ above autogenerated; below pulled from #{from_file} ~~~~~~~~~~~

#{File.read from_file}

# ~~~~~~~~~~~ below autogenerated; above pulled from #{from_file} ~~~~~~~~~~~
  end
  register "#{pattern_code}", (options || {})
end

EOF
  File.write to_file, file_content
  to_file
end

Instance Method Details

#anchor_codenamesObject



116
117
118
119
120
121
# File 'lib/card/set_pattern.rb', line 116

def anchor_codenames
  @anchor_name.parts.map do |part|
    part_id = Card.fetch_id part
    part_id && Card::Codename[part_id.to_i] || (return nil)
  end
end

#format_module_list(klass) ⇒ Object



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

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

#inspectObject



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

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

#lookup_module_list(modules_hash) ⇒ Object



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

def lookup_module_list modules_hash
  module_key && modules_hash[module_key]
end

#module_keyObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/card/set_pattern.rb', line 92

def module_key
  (defined? @module_key) ? @module_key : @module_key = begin
    if self.class.anchorless?
      self.class.pattern_code.camelize
    elsif anchor_codenames
      codenames = [self.class.pattern_code] + anchor_codenames.map(&:to_s)
      codenames.map(&:camelize).join '::'
    end
  end
end

#module_listObject



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

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

#patternObject



123
124
125
# File 'lib/card/set_pattern.rb', line 123

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

#rule_set_keyObject



144
145
146
147
148
149
150
# File 'lib/card/set_pattern.rb', line 144

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

#safe_keyObject



135
136
137
138
139
140
141
142
# File 'lib/card/set_pattern.rb', line 135

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

#to_sObject



127
128
129
# File 'lib/card/set_pattern.rb', line 127

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