Module: Card::Set::Helpers

Included in:
Card::Set
Defined in:
lib/card/set/helpers.rb

Constant Summary collapse

SET_PATTERN_TEST_REGEXP =
/^(?<pattern>\w+)_set\?$/

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

handles all_set?, abstract_set?, type_set?, etc.



45
46
47
48
49
50
51
# File 'lib/card/set/helpers.rb', line 45

def method_missing method_name, *args
  if (matches = method_name.match SET_PATTERN_TEST_REGEXP)
    pattern_code == matches[:pattern].to_sym
  else
    super
  end
end

Instance Method Details

#format_module(format_sym) ⇒ Object



92
93
94
# File 'lib/card/set/helpers.rb', line 92

def format_module format_sym
  const_get Card::Format.format_class_name(format_sym)
end

#format_modules(format_sym, test: true) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/card/set/helpers.rb', line 82

def format_modules format_sym, test: true
  if base_format_modules?
    [format_module(format_sym)]
  elsif abstract_set?
    abstract_format_modules format_sym, test
  else
    nonbase_format_modules format_sym
  end
end

#modulesObject



57
58
59
60
61
62
63
64
65
# File 'lib/card/set/helpers.rb', line 57

def modules
  if all_set?
    [self]
  elsif abstract_set?
    [test_set]
  else
    Set.modules[:nonbase][shortname] || []
  end
end

#num_set_parts(pattern_code) ⇒ Object



16
17
18
19
20
# File 'lib/card/set/helpers.rb', line 16

def num_set_parts pattern_code
  return 1 if pattern_code == :abstract

  Pattern.find(pattern_code).anchor_parts_count
end

#pattern_codeObject



40
41
42
# File 'lib/card/set/helpers.rb', line 40

def pattern_code
  @pattern_code ||= set_name_parts[2].underscore.to_sym
end

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/card/set/helpers.rb', line 53

def respond_to_missing? method_name, _include_private=false
  method_name.match? SET_PATTERN_TEST_REGEXP
end

#set_format_type_keyObject



22
23
24
# File 'lib/card/set/helpers.rb', line 22

def set_format_type_key
  @set_format_type_key ||= :"#{set_type_key}_format"
end

#set_name_partsObject



36
37
38
# File 'lib/card/set/helpers.rb', line 36

def set_name_parts
  @set_name_parts ||= name.split "::"
end

#set_type_keyObject



26
27
28
29
30
31
32
33
34
# File 'lib/card/set/helpers.rb', line 26

def set_type_key
  if all_set?
    :base
  elsif abstract_set?
    :abstract
  else
    :nonbase
  end
end

#shortnameObject



6
7
8
9
10
# File 'lib/card/set/helpers.rb', line 6

def shortname
  first = 2 # shortname eliminates Card::Set
  last = first + num_set_parts(pattern_code)
  set_name_parts[first..last].join "::"
end

#test_setObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/card/set/helpers.rb', line 67

def test_set
  # rubocop:disable Lint/Eval
  ::Card::Set::Self.const_remove_if_defined :TestSet
  eval <<-RUBY, binding, __FILE__, __LINE__ + 1
    class ::Card::Set::Self
      module TestSet
        extend Card::Set
        include_set #{name}
      end
    end
  RUBY
  ::Card::Set::Self::TestSet
  # rubocop:enable Lint/Eval
end

#underscoreObject



12
13
14
# File 'lib/card/set/helpers.rb', line 12

def underscore
  shortname.tr(":", "_").underscore
end