Class: BBCode::Tag

Inherits:
AbstractController::Base
  • Object
show all
Includes:
AbstractController::AssetPaths, AbstractController::Helpers, AbstractController::Rendering, AbstractController::Translation, ActionView::Rendering
Defined in:
lib/bbcode-rails/tag.rb,
lib/bbcode-rails/tag.rb,
lib/bbcode-rails/tag.rb

Direct Known Subclasses

BTag, ITag, ImgTag, QuoteTag

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Tag

Returns a new instance of Tag.



23
24
25
26
27
# File 'lib/bbcode-rails/tag.rb', line 23

def initialize parent
  @parent = parent
  @content = []
  @argument = ""
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



21
22
23
# File 'lib/bbcode-rails/tag.rb', line 21

def parent
  @parent
end

Class Method Details

.blockObject



90
91
92
# File 'lib/bbcode-rails/tag.rb', line 90

def self.block
  @block
end

.block_options(*args) ⇒ Object



78
79
80
# File 'lib/bbcode-rails/tag.rb', line 78

def self.block_options *args
  @options = args
end

.inherited(subclass) ⇒ Object



72
73
74
75
76
# File 'lib/bbcode-rails/tag.rb', line 72

def self.inherited subclass
  # In case we autoreload, remove earlier instances
  BBCode.tags.delete_if {|c| c.to_s == subclass.to_s }
  BBCode.tags[subclass.to_s.downcase] = subclass
end

.on_layout(&b) ⇒ Object



82
83
84
# File 'lib/bbcode-rails/tag.rb', line 82

def self.on_layout &b
  @block = b
end

.optionsObject



86
87
88
# File 'lib/bbcode-rails/tag.rb', line 86

def self.options
  @options ||= []
end

Instance Method Details

#<<(c) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/bbcode-rails/tag.rb', line 45

def << c
  if !has_option :content
    raise BBCode::ParseError, "Tried to assign content to a tag which takes none, #{name}"
  else
    @content << c
  end
end

#argument=(arg) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/bbcode-rails/tag.rb', line 37

def argument= arg
  if !has_option :argument
    raise BBCode::ParseError, "Tried to assign an argument to a tag which takes none, #{name}"
  else
    @argument = arg
  end
end

#get_blockObject



33
34
35
# File 'lib/bbcode-rails/tag.rb', line 33

def get_block
  self.class.block
end

#has_option(opt) ⇒ Object



29
30
31
# File 'lib/bbcode-rails/tag.rb', line 29

def has_option opt
  self.class.options.include?(opt)
end

#nameObject



68
69
70
# File 'lib/bbcode-rails/tag.rb', line 68

def name
  self.class.to_s.downcase.gsub(/tag$/i,'')
end

#to_sObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bbcode-rails/tag.rb', line 53

def to_s
  if has_option :content
    result = @content.map(&:to_s).join('')
  end
  if has_option(:content) and has_option(:argument)
    self.instance_exec(@argument, result, &get_block)
  elsif has_option :content
    self.instance_exec(result, &get_block)
  elsif has_option :argument
    self.instance_exec(@argument, &get_block)
  else
    self.instance_exec(&get_block)
  end
end