Module: Card::Content::Chunk

Defined in:
lib/card/content/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



30
31
32
33
34
35
36
37
# File 'lib/card/content/chunk.rb', line 30

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



39
40
41
42
43
44
45
46
47
# File 'lib/card/content/chunk.rb', line 39

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



19
20
21
22
23
24
# File 'lib/card/content/chunk.rb', line 19

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



26
27
28
# File 'lib/card/content/chunk.rb', line 26

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