Class: Htmless::StubBuilderForDocumentation::AbstractTag
- Inherits:
-
Object
- Object
- Htmless::StubBuilderForDocumentation::AbstractTag
- Defined in:
- lib/htmless/doc.rb
Direct Known Subclasses
Constant Summary collapse
- METHOD_MISSING_REGEXP =
/#{data_attribute}|#{id_class}/
Instance Attribute Summary collapse
-
#builder ⇒ Object
readonly
Returns the value of attribute builder.
Class Method Summary collapse
- ._attributes ⇒ Object
- ._attributes=(_attributes) ⇒ Object
-
.attributes ⇒ Array<String>
Array of available attributes for the tag.
- .strings_injector ⇒ Object
-
.tag_name ⇒ String
Tag’s name.
Instance Method Summary collapse
-
#attribute(name, value) ⇒ Object
it renders attribute using defined attribute method or by rendering attribute directly.
-
#attributes(attrs) ⇒ Object
attribute`s methods are called on background (in this case #id is called).
-
#class(*classes) ⇒ Object
adds classes to the tag by joining
classes
with ‘ ’ and skipping non-true classes. -
#data(hash) ⇒ Object
renders data-* attributes by
hash
. -
#id(*values) ⇒ Object
adds id to the tag by joining
values
with ‘_’. -
#initialize(builder) ⇒ AbstractTag
constructor
private
A new instance of AbstractTag.
-
#method_missing(method, *args, &block) ⇒ Object
allows data-* attributes and id, classes by method_missing.
-
#mimic(obj) ⇒ Object
(also: #[])
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(‘/’, ‘-’).
- #open(attributes = nil) ⇒ Object private
-
#rclass ⇒ Object
original Ruby method for class, class is used for html classes.
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.
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
#method_missing(method, *args, &block) ⇒ Object
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
#builder ⇒ Object (readonly)
Returns the value of attribute builder.
94 95 96 |
# File 'lib/htmless/doc.rb', line 94 def builder @builder end |
Class Method Details
._attributes ⇒ Object
14 15 16 |
# File 'lib/htmless/doc.rb', line 14 def self._attributes @_attributes or (superclass._attributes if superclass.respond_to? :_attributes) end |
._attributes=(_attributes) ⇒ Object
10 11 12 |
# File 'lib/htmless/doc.rb', line 10 def self._attributes=(_attributes) @_attributes = _attributes end |
.attributes ⇒ Array<String>
Returns array of available attributes for the tag.
21 22 23 |
# File 'lib/htmless/doc.rb', line 21 def self.attributes _attributes end |
.strings_injector ⇒ Object
6 7 8 |
# File 'lib/htmless/doc.rb', line 6 def self.strings_injector dynamic_class_base.strings_injector end |
.tag_name ⇒ String
Returns 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
#attribute(name, value) ⇒ Object
it renders attribute using defined attribute method or by rendering attribute directly
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 |
#attributes(attrs) ⇒ Object
attribute`s methods are called on background (in this case #id is called)
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 |
#class(*classes) ⇒ Object
adds classes to the tag by joining classes
with ‘ ’ and skipping non-true classes
174 175 176 177 |
# File 'lib/htmless/doc.rb', line 174 def class(*classes) @classes.push(*classes.select { |c| c }) self end |
#data(hash) ⇒ Object
renders data-* attributes by 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 |
#id(*values) ⇒ Object
adds id to the tag by joining values
with ‘_’
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 |
#mimic(obj) ⇒ Object 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.
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 |
#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.
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 |
#rclass ⇒ Object
original Ruby method for class, class is used for html classes
143 |
# File 'lib/htmless/doc.rb', line 143 alias_method(:rclass, :class) |