Class: Card::Content
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Card::Content
- Defined in:
- lib/card/content.rb,
lib/card/content/all.rb,
lib/card/content/diff.rb,
lib/card/content/chunk.rb,
lib/card/content/clean.rb,
lib/card/content/parser.rb,
lib/card/content/truncate.rb,
lib/card/content/diff/l_c_s.rb,
lib/card/content/diff/result.rb,
lib/card/content/diff/summary.rb,
lib/card/content/chunk/abstract.rb,
lib/card/content/diff/l_c_s/processor.rb
Overview
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 Chunk::Abstract
Defined Under Namespace
Modules: All, Chunk, Clean, Truncate Classes: Diff, Parser
Constant Summary
Constants included from Clean
Clean::ALLOWED_TAGS, Clean::ATTR_VALUE_RE
Constants included from Truncate
Instance Attribute Summary collapse
-
#chunks ⇒ Object
readonly
Returns the value of attribute chunks.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#revision ⇒ Object
readonly
Returns the value of attribute revision.
Instance Method Summary collapse
-
#card ⇒ Card
Content must be associated with a Format object, which in turn must be associated with a Card.
- #custom_process_chunks ⇒ Object
- #each_chunk ⇒ Object
-
#find_chunks(chunk_type = nil) ⇒ Array of Chunk instances
Find all chunks of a given type.
- #has_chunk?(chunk_type) ⇒ Boolean
-
#initialize(content, format_or_card, opts = {}) ⇒ Content
constructor
initialization parses String, detects chunks classes to be used in parsing.
- #inspect ⇒ Object
- #pieces ⇒ Object
-
#process_chunks(&block) ⇒ Object
sends &block to #process_chunk on each Chunk object.
- #strip_chunks(chunk_type) ⇒ Object
- #strip_nests ⇒ Object
-
#to_s ⇒ Object
convert content to String.
- #without_chunks(*chunk_classes) ⇒ Object
- #without_nests(&block) ⇒ Object
- #without_references(&block) ⇒ Object
Methods included from Clean
Methods included from Truncate
Constructor Details
#initialize(content, format_or_card, opts = {}) ⇒ Content
initialization parses String, detects chunks classes to be used in parsing
23 24 25 26 27 28 29 |
# File 'lib/card/content.rb', line 23 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 |
Instance Attribute Details
#chunks ⇒ Object (readonly)
Returns the value of attribute chunks.
15 16 17 |
# File 'lib/card/content.rb', line 15 def chunks @chunks end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
15 16 17 |
# File 'lib/card/content.rb', line 15 def format @format end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
15 16 17 |
# File 'lib/card/content.rb', line 15 def opts @opts end |
#revision ⇒ Object (readonly)
Returns the value of attribute revision.
15 16 17 |
# File 'lib/card/content.rb', line 15 def revision @revision end |
Instance Method Details
#card ⇒ Card
Content must be associated with a Format object, which in turn must be associated with a Card
34 35 36 |
# File 'lib/card/content.rb', line 34 def card @format.card end |
#custom_process_chunks ⇒ Object
57 58 59 60 61 |
# File 'lib/card/content.rb', line 57 def custom_process_chunks each_chunk do |chunk| chunk.burn_after_reading yield(chunk) end end |
#each_chunk ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/card/content.rb', line 67 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) ⇒ Array of Chunk instances
Find all chunks of a given type
41 42 43 44 |
# File 'lib/card/content.rb', line 41 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) ⇒ Boolean
46 47 48 |
# File 'lib/card/content.rb', line 46 def has_chunk? chunk_type each_chunk.any { |chunk| chunk.is_a?(chunk_type) } end |
#inspect ⇒ Object
98 99 100 |
# File 'lib/card/content.rb', line 98 def inspect "<#{__getobj__.class}:#{card}:#{self}>" end |
#pieces ⇒ Object
63 64 65 |
# File 'lib/card/content.rb', line 63 def pieces Array.wrap(__getobj__) end |
#process_chunks(&block) ⇒ Object
sends &block to #process_chunk on each Chunk object
51 52 53 54 55 |
# File 'lib/card/content.rb', line 51 def process_chunks &block return custom_process_chunks(&block) if block_given? each_chunk(&:process_chunk) end |
#strip_chunks(chunk_type) ⇒ Object
121 122 123 |
# File 'lib/card/content.rb', line 121 def strip_chunks chunk_type chunks.reject! { |c| c.is_a? chunk_type } end |
#strip_nests ⇒ Object
117 118 119 |
# File 'lib/card/content.rb', line 117 def strip_nests strip_chunks Chunk::Nest end |
#to_s ⇒ Object
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.
89 90 91 92 93 94 95 96 |
# File 'lib/card/content.rb', line 89 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) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/card/content.rb', line 110 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) ⇒ Object
102 103 104 |
# File 'lib/card/content.rb', line 102 def without_nests &block without_chunks Chunk::Nest, &block end |