Class: HammerBuilder::StubBuilderForDocumentation::AbstractTag
- Inherits:
-
Object
- Object
- HammerBuilder::StubBuilderForDocumentation::AbstractTag
- Defined in:
- lib/hammer_builder/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 ⇒ 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 .hammer_builder_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.
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
#builder ⇒ Object (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
.attributes ⇒ Array<String>
Returns array of available attributes for the tag.
14 15 16 |
# File 'lib/hammer_builder/doc.rb', line 14 def self.attributes _attributes end |
.strings_injector ⇒ Object
6 7 8 |
# File 'lib/hammer_builder/doc.rb', line 6 def self.strings_injector dynamic_class_base.strings_injector end |
.tag_name ⇒ String
Returns 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
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)
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
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
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 ‘_’
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.
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 |
#rclass ⇒ Object
original Ruby method for class, class is used for html classes
134 |
# File 'lib/hammer_builder/doc.rb', line 134 alias_method(:rclass, :class) |