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

Class Method Summary collapse

Class Method Details

.find_class_by_prefix(prefix) ⇒ Object



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

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/chunk.rb', line 39

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

.register_class(klass, hash) ⇒ Object



23
24
25
26
27
# 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



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

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