class Card::Content

Content objects support the parsing of content strings into arrays that contain semantically meaningful “chunks” like nests, links, urls, etc.

Each chunk has an object whose class inherits from {Card::Content::Chunk::Abstract}

Attributes

chunks[R]
format[R]
opts[R]
revision[R]

Public Class Methods

new(content, format_or_card, opts={}) click to toggle source

initialization parses String, detects chunks @param content [String] @param format_or_card [Card::Format or Card] @param opts [Hash] @option opts [Symbol] :chunk_list - name of registered list of chunk classes to be used in parsing

Calls superclass method
# File lib/card/content.rb, line 21
def initialize content, format_or_card, opts={}
  @format = resolve_format format_or_card
  opts ||= {}
  chunk_list = opts[:chunk_list] || @format.chunk_list
  @chunks = Parser.new(chunk_list, self).parse(content)
  super(@chunks.any? ? @chunks : content)
end

Public Instance Methods

card() click to toggle source

Content must be associated with a Format object, which in turn must be associated with a Card @return [Card]

# File lib/card/content.rb, line 32
def card
  @format.card
end
custom_process_chunks() { |chunk| ... } click to toggle source
# File lib/card/content.rb, line 55
def custom_process_chunks
  each_chunk do |chunk|
    chunk.burn_after_reading yield(chunk)
  end
end
each_chunk() { |item| ... } click to toggle source
# File lib/card/content.rb, line 65
def each_chunk
  return enum_for(:each_chunk) unless block_given?

  iterator =
    case __getobj__
    when Hash   then :each_value
    when Array  then :each
    when String then return # no chunks
    else
      Rails.logger.warn "unrecognized type for #each_chunk: " \
                        " #{self.class} #{__getobj__.class}"
      return
    end
  send(iterator) { |item| yield item if item.is_a?(Chunk::Abstract) }
end
find_chunks(chunk_type=nil) click to toggle source

Find all chunks of a given type @param chunk_type [Chunk Class or Symbol] @return [Array of Chunk instances]

# File lib/card/content.rb, line 39
def find_chunks chunk_type=nil
  chunk_type = interpret_chunk_type chunk_type
  each_chunk.select { |chunk| chunk.is_a?(chunk_type) }
end
has_chunk?(chunk_type) click to toggle source
# File lib/card/content.rb, line 44
def has_chunk? chunk_type
  each_chunk.any { |chunk| chunk.is_a?(chunk_type) }
end
inspect() click to toggle source
# File lib/card/content.rb, line 96
def inspect
  "<#{__getobj__.class}:#{card}:#{self}>"
end
pieces() click to toggle source
# File lib/card/content.rb, line 61
def pieces
  Array.wrap(__getobj__)
end
process_chunks(&block) click to toggle source

sends &block to process_chunk on each Chunk object

# File lib/card/content.rb, line 49
def process_chunks &block
  return custom_process_chunks(&block) if block_given?

  each_chunk(&:process_chunk)
end
strip_chunks(chunk_type) click to toggle source
# File lib/card/content.rb, line 119
def strip_chunks chunk_type
  chunks.reject! { |c| c.is_a? chunk_type }
end
strip_nests() click to toggle source
# File lib/card/content.rb, line 115
def strip_nests
  strip_chunks Chunk::Nest
end
to_s() click to toggle source

convert content to String. the common cases here are that either

  • (a) content is already a String, or

  • (b) it’s an Array that needs to be iterated over and converted into a

a string by running to_s on each item.

# File lib/card/content.rb, line 87
def to_s
  case __getobj__
  when Array    then map(&:to_s) * ""
  when String   then __getobj__
  when NilClass then "" # raise "Nil Card::Content"
  else               __getobj__.to_s
  end
end
without_chunks(*chunk_classes) { |to_s| ... } click to toggle source
# File lib/card/content.rb, line 108
def without_chunks *chunk_classes
  chunk_classes = ::Set.new Array.wrap(chunk_classes)
  stash = stash_chunks chunk_classes
  processed = yield to_s
  unstash_chunks processed, stash
end
without_nests(&block) click to toggle source
# File lib/card/content.rb, line 100
def without_nests &block
  without_chunks Chunk::Nest, &block
end
without_references(&block) click to toggle source
# File lib/card/content.rb, line 104
def without_references &block
  without_chunks Chunk::Nest, Chunk::Link, Chunk::EscapedLiteral, &block
end

Private Instance Methods

interpret_chunk_type(chunk_type) click to toggle source
# File lib/card/content.rb, line 125
def interpret_chunk_type chunk_type
  case chunk_type
  when nil
    Chunk
  when Symbol, String
    Chunk.const_get chunk_type
  when Class
    chunk_type
  else
    raise ArgumentError, "unknown chunk type: #{chunk_type}"
  end
end
resolve_format(format_or_card) click to toggle source
# File lib/card/content.rb, line 155
def resolve_format format_or_card
  if format_or_card.is_a?(Card)
    Format.new format_or_card, format: nil
  else
    format_or_card
  end
end
stash_chunks(chunk_classes) click to toggle source
# File lib/card/content.rb, line 138
def stash_chunks chunk_classes
  chunks = []
  each_chunk do |chunk|
    next unless chunk_classes.include? chunk.class

    chunk.burn_after_reading "{{#{chunks.size}}}"
    chunks << chunk.text
  end
  chunks
end
unstash_chunks(content, stashed_chunks) click to toggle source
# File lib/card/content.rb, line 149
def unstash_chunks content, stashed_chunks
  Chunk::Nest.gsub content do |nest_content|
    nest_content.number? ? stashed_chunks[nest_content.to_i] : "{{#{nest_content}}}"
  end
end