Class: BBCoder::BufferTags

Inherits:
Object
  • Object
show all
Defined in:
lib/bbcoder/buffer_tags.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer) ⇒ BufferTags

Returns a new instance of BufferTags.



5
6
7
8
9
# File 'lib/bbcoder/buffer_tags.rb', line 5

def initialize(buffer)
  @buffer = buffer
  @_internal = []
  @_meta = {}
end

Instance Attribute Details

#_internalObject

Returns the value of attribute _internal.



3
4
5
# File 'lib/bbcoder/buffer_tags.rb', line 3

def _internal
  @_internal
end

#_metaObject

Returns the value of attribute _meta.



3
4
5
# File 'lib/bbcoder/buffer_tags.rb', line 3

def _meta
  @_meta
end

#bufferObject

Returns the value of attribute buffer.



3
4
5
# File 'lib/bbcoder/buffer_tags.rb', line 3

def buffer
  @buffer
end

Instance Method Details

#criteria_met?(tag) ⇒ Boolean

helper methods

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/bbcoder/buffer_tags.rb', line 67

def criteria_met?(tag)
  return false if BBCoder.configuration[tag].nil?

  parent_criteria_met?(BBCoder.configuration[tag].parents)
end

#empty?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/bbcoder/buffer_tags.rb', line 54

def empty?
  _internal.empty?
end

#include?(tag) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/bbcoder/buffer_tags.rb', line 62

def include?(tag)
  _internal.include?(tag)
end

#join(limit = nil) ⇒ Object

orphaned open tags are combined



41
42
43
44
45
46
47
# File 'lib/bbcoder/buffer_tags.rb', line 41

def join(limit = nil)
  limit = size if limit.nil?

  1.upto(limit).to_a.collect do
    [BBCoder::Tag.reform(_internal.pop, _meta.delete(size+1)), buffer.pop(+1)].join  # +1 depth modifier for the buffer
  end.reverse.join
end

#lastObject



58
59
60
# File 'lib/bbcoder/buffer_tags.rb', line 58

def last
  _internal.last
end

#parent_criteria_met?(parents) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/bbcoder/buffer_tags.rb', line 73

def parent_criteria_met?(parents)
  parents.empty? || !(_internal & parents).empty?
end

#pop(tag) ⇒ Object

logic when popping specific tag



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bbcoder/buffer_tags.rb', line 28

def pop(tag)
  if empty? || !include?(tag)
    buffer.push("[/#{tag}]")
  elsif last == tag
    buffer.push(BBCoder::Tag.to_html(_internal.pop, _meta.delete(size+1), buffer.pop(+1)))
  elsif include?(tag)
    # repeats pop(tag) until we reach the last == tag
    buffer.push(join(+1))
    pop(tag)
  end
end

#push(tag) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bbcoder/buffer_tags.rb', line 11

def push(tag)
  tag, meta = if tag.include?("=")
                splits = tag.split("=")
                [splits.shift.to_sym, splits.join]
              else
                [tag.to_sym, nil]
              end

  if criteria_met?(tag)
    _internal.push(tag)
    _meta[size] = meta
  else
    buffer.push(BBCoder::Tag.reform(tag, meta))
  end
end

#sizeObject

delegates to _internal



50
51
52
# File 'lib/bbcoder/buffer_tags.rb', line 50

def size
  _internal.size
end