Module: Card::Chunk

Defined in:
lib/card/chunk.rb

Overview

A chunk is a pattern of text that can be protected and interrogated by a format. Each Chunk class has a pattern that states what sort of text it matches. Chunks are initalized by passing in the result of a match by its pattern.

Defined Under Namespace

Classes: Abstract

Constant Summary collapse

@@raw_list =
{}
@@prefix_regexp_by_list =
{}
@@prefix_map =
{}

Class Method Summary collapse

Class Method Details

.find_class_by_prefix(prefix) ⇒ Object



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

def find_class_by_prefix prefix
  config = prefix_map[prefix[0, 1]] ||
           prefix_map[prefix[-1, 1]] ||
           prefix_map[:default]
  # prefix identified by first character, last character, or default.
  # a little ugly...
  config[:class]
end

.get_prefix_regexp(chunk_list_key) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/card/chunk.rb', line 43

def get_prefix_regexp chunk_list_key
  prefix_regexp_by_list[chunk_list_key] ||= begin
    prefix_res = raw_list[chunk_list_key].map do |chunkname|
      chunk_class = const_get chunkname
      chunk_class.config[:prefix_re]
    end
    /(?:#{ prefix_res * '|' })/m
  end
end

.register_class(klass, hash) ⇒ Object



23
24
25
26
27
28
# File 'lib/card/chunk.rb', line 23

def register_class klass, hash
  klass.config = hash.merge class: klass
  prefix_index = hash[:idx_char] || :default
  # ^ this is gross and needs to be moved out.
  prefix_map[prefix_index] = klass.config
end

.register_list(key, list) ⇒ Object



30
31
32
# File 'lib/card/chunk.rb', line 30

def register_list key, list
  raw_list[key] = list
end