Class: Htmless::StubBuilderForDocumentation::AbstractTag

Inherits:
Object
  • Object
show all
Defined in:
lib/htmless/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



97
98
99
100
101
102
103
104
105
# File 'lib/htmless/doc.rb', line 97

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

  self.rclass.strings_injector.inject_to self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method, *args, &block)

allows data-* attributes and id, classes by method_missing



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/htmless/doc.rb', line 150

def method_missing(method, *args, &block)
  method = method.to_s
  if method =~ METHOD_MISSING_REGEXP
    if $1
      self.rclass.add_attributes Data::Attribute.new(method, :string)
      self.send method, *args
    else
      self.__send__($3 == '!' ? :id : :class, $2.gsub(@_str_underscore, @_str_dash))
      self.attributes args.first
    end
  else
    super(method, *args, &block)
  end
end

Instance Attribute Details

- (Object) builder (readonly)

Returns the value of attribute builder



94
95
96
# File 'lib/htmless/doc.rb', line 94

def builder
  @builder
end

Class Method Details

+ (Object) _attributes



14
15
16
# File 'lib/htmless/doc.rb', line 14

def self._attributes
  @_attributes or (superclass._attributes if superclass.respond_to? :_attributes)
end

+ (Object) _attributes=(_attributes)



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

def self._attributes=(_attributes)
  @_attributes = _attributes
end

+ (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)


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

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:



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/htmless/doc.rb', line 68

def self.attribute_content_rendering(attribute)
  name = attribute.name.to_s
  case attribute.type
    when :string
      strings_injector.add "attr_#{name}", " #{name.gsub('_', '-')}=#{strings_injector[:quote]}"
      "@output << @_str_attr_#{name} << CGI.escapeHTML(content.to_s) << @_str_quote"
    when :boolean
      strings_injector.add(
          "attr_#{name}",
          " #{name.gsub('_', '-')}=#{strings_injector[:quote]}#{name}#{strings_injector[:quote]}")
      "@output << @_str_attr_#{name} if content"
  end
end

+ (Array<String>) attributes

Array of available attributes for the tag

Returns:

  • (Array<String>)

    array of available attributes for the tag



21
22
23
# File 'lib/htmless/doc.rb', line 21

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:



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/htmless/doc.rb', line 53

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



42
43
44
# File 'lib/htmless/doc.rb', line 42

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

+ (Object) inherited(base) (protected)



46
47
48
# File 'lib/htmless/doc.rb', line 46

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



34
35
36
# File 'lib/htmless/doc.rb', line 34

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

+ (Object) strings_injector



6
7
8
# File 'lib/htmless/doc.rb', line 6

def self.strings_injector
  dynamic_class_base.strings_injector
end

+ (String) tag_name

Tag's name

Returns:

  • (String)

    tag's name



26
27
28
# File 'lib/htmless/doc.rb', line 26

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)


119
120
121
122
123
# File 'lib/htmless/doc.rb', line 119

def attribute(name, value)
  return __send__(name, value) if respond_to?(name)
  @output << @_str_space << name.to_s << @_str_eql_quote << CGI.escapeHTML(value.to_s) << @_str_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>


130
131
132
133
134
135
136
137
138
139
140
# File 'lib/htmless/doc.rb', line 130

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>)


174
175
176
177
# File 'lib/htmless/doc.rb', line 174

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)


221
222
223
224
# File 'lib/htmless/doc.rb', line 221

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>


231
232
# File 'lib/htmless/doc.rb', line 231

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



236
237
238
239
240
241
# File 'lib/htmless/doc.rb', line 236

def flush_classes
  unless @classes.empty?
    @output << @_str_attr_class << CGI.escapeHTML(@classes.join(@_str_space)) << @_str_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>)


184
185
186
187
# File 'lib/htmless/doc.rb', line 184

def id(*values)
  @output << @_str_attr_id << CGI.escapeHTML(values.select { |v| v }.join(@_str_dash)) << @_str_quote
  self
end

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

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

Examples:

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

Parameters:

  • obj (Object)


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/htmless/doc.rb', line 196

def mimic(obj)
  klass = if obj.class.respond_to? :htmless_ref
            obj.class.htmless_ref
          else
            obj.class.to_s.scan(/[A-Z][a-z\d]*/).join('_').downcase.gsub('::', '-')
          end

  id = case
         when obj.respond_to?(:htmless_ref)
           obj.htmless_ref
         when obj.respond_to?(:id)
           [klass, obj.id]
         else
           [klass, obj.object_id]
       end
  #noinspection RubyArgCount
  self.class(klass).id(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.



108
109
110
111
112
113
114
# File 'lib/htmless/doc.rb', line 108

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

- (Object) rclass

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



143
# File 'lib/htmless/doc.rb', line 143

alias_method(:rclass, :class)