Class: HammerBuilder::StubBuilderForDocumentation::AbstractTag

Inherits:
Object
  • Object
show all
Defined in:
lib/hammer_builder/doc.rb

Direct Known Subclasses

AbstractDoubleTag, AbstractSingleTag

Constant Summary

METHOD_MISSING_REGEXP =
/#{data_attribute}|#{id_class}/

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (AbstractTag) initialize(builder)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A new instance of AbstractTag



84
85
86
87
88
89
90
# File 'lib/hammer_builder/doc.rb', line 84

def initialize(builder)
  @builder  = builder
  @output   = builder.instance_eval { @_output }
  @stack    = builder.instance_eval { @_stack }
  @classes  = []
  @tag_name = self.rclass.tag_name
end

Instance Attribute Details

- (Object) builder (readonly)

Returns the value of attribute builder



81
82
83
# File 'lib/hammer_builder/doc.rb', line 81

def builder
  @builder
end

Class Method Details

+ (Object) add_attributes(attributes) (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

adds attribute to class, triggers dynamical creation of needed instance methods etc.

Parameters:

Raises:

  • (ArgumentError)


72
73
74
75
76
77
# File 'lib/hammer_builder/doc.rb', line 72

def self.add_attributes(attributes)
  attributes = [attributes] unless attributes.is_a? Array
  raise ArgumentError, attributes.inspect unless attributes.all? { |v| v.is_a? Data::Attribute }
  self.send :_attributes=, _attributes + attributes
  define_attribute_methods
end

+ (Object) attribute_content_rendering(attribute) (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hammer_builder/doc.rb', line 57

def self.attribute_content_rendering(attribute)
  name = attribute.name.to_s
  case attribute.type
    when :string
      Strings.add "attr_#{name}", " #{name.gsub('_', '-')}=\""
      "@output << Strings::ATTR_#{name.upcase} << CGI.escapeHTML(content.to_s) << Strings::QUOTE"
    when :boolean
      Strings.add "attr_#{name}", " #{name.gsub('_', '-')}=\"#{name}\""
      "@output << Strings::ATTR_#{name.upcase} if content"
  end
end

+ (Array<String>) attributes

Array of available attributes for the tag

Returns:

  • (Array<String>)

    array of available attributes for the tag



10
11
12
# File 'lib/hammer_builder/doc.rb', line 10

def self.attributes
  _attributes
end

+ (Object) define_attribute_method(attribute) (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

defines dynamically method for attribute

Parameters:



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hammer_builder/doc.rb', line 42

def self.define_attribute_method(attribute)
  return if instance_methods.include?(attribute.name)
  name              = attribute.name.to_s
  content_rendering = attribute_content_rendering(attribute)
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def #{name}(content#{' = true' if attribute.type == :boolean})
        #{content_rendering}
        self
      end
  RUBY
end

+ (Object) define_attribute_methods (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

defines dynamically methods for attributes



31
32
33
# File 'lib/hammer_builder/doc.rb', line 31

def self.define_attribute_methods
  attributes.each { |attr| define_attribute_method(attr) }
end

+ (Object) inherited(base) (protected)



35
36
37
# File 'lib/hammer_builder/doc.rb', line 35

def self.inherited(base)
  base.define_attribute_methods
end

+ (Object) set_tag(tag) (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

sets the tag's name



23
24
25
# File 'lib/hammer_builder/doc.rb', line 23

def self.set_tag(tag)
  @tag = tag.to_s.freeze
end

+ (String) tag_name

Tag's name

Returns:

  • (String)

    tag's name



15
16
17
# File 'lib/hammer_builder/doc.rb', line 15

def self.tag_name
  @tag || superclass.tag_name
end

Instance Method Details

- (Object) attribute(name, value)

it renders attribute using defined attribute method or by rendering attribute directly

Parameters:

  • name (String, Symbol)
  • value (#to_s)


104
105
106
107
108
# File 'lib/hammer_builder/doc.rb', line 104

def attribute(name, value)
  return __send__(name, value) if respond_to?(name)
  @output << Strings::SPACE << name.to_s << Strings::EQL_QUOTE << CGI.escapeHTML(value.to_s) << Strings::QUOTE
  self
end

- (Object) attributes(attrs)

attribute`s methods are called on background (in this case #id is called)

Examples:

div.attributes :id => 'id' # => <div id="id"></div>
div :id => 'id', :class => %w{left right} # => <div id="id" class="left right"></div>
img :src => 'path' # => <img src="path"></div>


115
116
117
118
119
120
121
122
123
124
125
# File 'lib/hammer_builder/doc.rb', line 115

def attributes(attrs)
  return self unless attrs
  attrs.each do |attr, value|
    if value.kind_of?(Array)
      __send__(attr, *value)
    else
      __send__(attr, value)
    end
  end
  self
end

- (Object) class(*classes)

adds classes to the tag by joining classes with ' ' and skipping non-true classes

Examples:

class(!visible? && 'hidden', 'left') #=> class="hidden left" or class="left"

Parameters:

  • classes (Array<#to_s>)


160
161
162
163
# File 'lib/hammer_builder/doc.rb', line 160

def class(*classes)
  @classes.push(*classes.select { |c| c })
  self
end

- (Object) data(hash)

renders data-* attributes by hash

Examples:

div.data(:remote => true, :id => 'an_id') # => <div data-remote="true" data-id="an_id"></div>

Parameters:

  • hash (Hash)


209
210
211
212
# File 'lib/hammer_builder/doc.rb', line 209

def data(hash)
  hash.each { |k, v| __send__ "data_#{k}", v }
  self
end

- (Object) default (protected)

this method is called on each tag opening, useful for default attributes

Examples:

html tag uses this to add xmlns attr.

html # => <html xmlns="http://www.w3.org/1999/xhtml"></html>


219
220
# File 'lib/hammer_builder/doc.rb', line 219

def default
end

- (Object) flush_classes (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

flushes classes to output



224
225
226
227
228
229
# File 'lib/hammer_builder/doc.rb', line 224

def flush_classes
  unless @classes.empty?
    @output << Strings::ATTR_CLASS << CGI.escapeHTML(@classes.join(Strings::SPACE)) << Strings::QUOTE
    @classes.clear
  end
end

- (Object) id(*values)

adds id to the tag by joining values with '_'

Examples:

id('user', 12) #=> id="user_15"

Parameters:

  • values (Array<#to_s>)


170
171
172
173
174
# File 'lib/hammer_builder/doc.rb', line 170

def id(*values)
  @output << Strings::ATTR_ID << CGI.escapeHTML(values.select { |v| v }.join(Strings::UNDERSCORE)) <<
      Strings::QUOTE
  self
end

- (Object) object(obj) Also known as: []

adds id and class to a tag by an object To determine the class it looks for .hammer_builder_ref or it uses class.to_s.underscore.tr('/', '-'). To determine id it combines class and an id of the obj. It looks for #hammer_builder_ref or #id or #object_id.

Examples:

div[AUser.new].with { text 'a' } # => <div id="a_user_1" class="a_user">a</div>

Parameters:

  • obj (Object)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/hammer_builder/doc.rb', line 184

def object(obj)
  klass = if obj.class.respond_to? :hammer_builder_ref
    obj.class.hammer_builder_ref
  else
    ActiveSupport::Inflector.underscore(obj.class.to_s).tr('/', '-')
  end

  id = case
    when obj.respond_to?(:hammer_builder_ref)
      obj.hammer_builder_ref
    when obj.respond_to?(:id)
      obj.id.to_s
    else
      obj.object_id
  end
  #noinspection RubyArgCount
  self.class(klass).id(klass, id)
end

- (Object) open(attributes = nil)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



93
94
95
96
97
98
99
# File 'lib/hammer_builder/doc.rb', line 93

def open(attributes = nil)
  @output << Strings::LT << @tag_name
  @builder.current = self
  attributes(attributes)
  default
  self
end

- (Object) rclass

original Ruby method for class, class is used for html classes



128
# File 'lib/hammer_builder/doc.rb', line 128

alias_method(:rclass, :class)