Class: HammerBuilder::StubBuilderForDocumentation::AbstractTag

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

Direct Known Subclasses

AbstractDoubleTag, AbstractSingleTag

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder) ⇒ AbstractTag

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.

Returns a new instance of AbstractTag.



88
89
90
91
92
93
94
95
96
# File 'lib/hammer_builder/doc.rb', line 88

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

#method_missing(method, *args, &block) ⇒ Object

allows data-* attributes and id, classes by method_missing



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/hammer_builder/doc.rb', line 141

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)
    end
  else
    super(method, *args, &block)
  end
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



85
86
87
# File 'lib/hammer_builder/doc.rb', line 85

def builder
  @builder
end

Class Method Details

.attributesArray<String>

Returns array of available attributes for the tag.

Returns:

  • (Array<String>)

    array of available attributes for the tag



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

def self.attributes
  _attributes
end

.strings_injectorObject



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

def self.strings_injector
  dynamic_class_base.strings_injector
end

.tag_nameString

Returns tag’s name.

Returns:

  • (String)

    tag’s name



19
20
21
# File 'lib/hammer_builder/doc.rb', line 19

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

Instance Method Details

#attribute(name, value) ⇒ Object

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

Parameters:

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


110
111
112
113
114
# File 'lib/hammer_builder/doc.rb', line 110

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

#attributes(attrs) ⇒ Object

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>


121
122
123
124
125
126
127
128
129
130
131
# File 'lib/hammer_builder/doc.rb', line 121

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

#class(*classes) ⇒ Object

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


164
165
166
167
# File 'lib/hammer_builder/doc.rb', line 164

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

#data(hash) ⇒ Object

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)


211
212
213
214
# File 'lib/hammer_builder/doc.rb', line 211

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

#id(*values) ⇒ Object

adds id to the tag by joining values with ‘_’

Examples:

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

Parameters:

  • values (Array<#to_s>)


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

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

#mimic(obj) ⇒ Object 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 looks for #hammer_builder_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)


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

def mimic(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)
           [klass, obj.id]
         else
           [klass, obj.object_id]
       end
  #noinspection RubyArgCount
  self.class(klass).id(id)
end

#open(attributes = nil) ⇒ Object

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.



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

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

#rclassObject

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



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

alias_method(:rclass, :class)