Class: Card::Content
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Card::Content
- Defined in:
- lib/card/content.rb,
lib/card/content/diff.rb,
lib/card/content/chunk.rb,
lib/card/content/clean.rb,
lib/card/content/parser.rb,
lib/card/content/diff/lcs.rb,
lib/card/content/truncate.rb,
lib/card/content/diff/result.rb,
lib/card/content/diff/processor.rb,
mod/core/chunk/include.rb,
mod/core/chunk/literal.rb,
mod/core/chunk/reference.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 Card::Chunk::Abstract
Defined Under Namespace
Modules: 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.
- #each_chunk ⇒ Object
-
#find_chunks(chunk_type) ⇒ Array of Chunk instances
Find all chunks of a given type.
-
#initialize(content, format_or_card, opts = {}) ⇒ Content
constructor
initialization parses String, detects chunks classes to be used in parsing.
- #inspect ⇒ Object
-
#process_each_chunk(&block) ⇒ Object
sends &block to #process_chunk on each Chunk object.
-
#to_s ⇒ Object
convert content to String.
Methods included from Clean
clean!, clean_with_space_last!, process_attribute, process_attribute_match
Methods included from Truncate
close_tags, find_tags, polish, smart_truncate, truncate
Constructor Details
#initialize(content, format_or_card, opts = {}) ⇒ Content
initialization parses String, detects chunks classes to be used in parsing
26 27 28 29 30 31 32 |
# File 'lib/card/content.rb', line 26 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.
18 19 20 |
# File 'lib/card/content.rb', line 18 def chunks @chunks end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
18 19 20 |
# File 'lib/card/content.rb', line 18 def format @format end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
18 19 20 |
# File 'lib/card/content.rb', line 18 def opts @opts end |
#revision ⇒ Object (readonly)
Returns the value of attribute revision.
18 19 20 |
# File 'lib/card/content.rb', line 18 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
37 38 39 |
# File 'lib/card/content.rb', line 37 def card @format.card end |
#each_chunk ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/card/content.rb', line 53 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) ⇒ Array of Chunk instances
Find all chunks of a given type
44 45 46 |
# File 'lib/card/content.rb', line 44 def find_chunks chunk_type each_chunk.select { |chunk| chunk.is_a?(chunk_type) } end |
#inspect ⇒ Object
84 85 86 |
# File 'lib/card/content.rb', line 84 def inspect "<#{__getobj__.class}:#{card}:#{self}>" end |
#process_each_chunk(&block) ⇒ Object
sends &block to #process_chunk on each Chunk object
49 50 51 |
# File 'lib/card/content.rb', line 49 def process_each_chunk &block each_chunk { |chunk| chunk.process_chunk(&block) } 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.
75 76 77 78 79 80 81 82 |
# File 'lib/card/content.rb', line 75 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 |