Class: WrapIt::Base
- Inherits:
-
Object
- Object
- WrapIt::Base
- Extended by:
- Arguments::ClassMethods, Enums::ClassMethods, HTML::ClassMethods, Sections::ClassMethods, Switches::ClassMethods
- Defined in:
- lib/wrap_it/base.rb
Overview
Base class for all HTML helper classes
Instance Attribute Summary collapse
-
#helper_name ⇒ Object
Returns the value of attribute helper_name.
Class Method Summary collapse
-
#argument(name, opts = {}) {|name, value| ... } ⇒ void
extended
from Arguments::ClassMethods
Desclares argument for capturing on initialization process.
-
#capture_arguments!(args, opts = {}, &block) ⇒ Array<Object>
extended
from Arguments::ClassMethods
Capture arguments for class and it's ancestors.
-
.enum(name, values, opts = {}) {|value| ... } ⇒ void
extended
from Enums::ClassMethods
Adds
enum. -
#html_class([html_class, ...]) ⇒ void
extended
from HTML::ClassMethods
Adds default html classes, thats are automatically added when element created.
-
.html_class_prefix(prefix = nil) ⇒ void
extended
from HTML::ClassMethods
Sets HTML class prefix.
-
#option(name, opts = {}) {|name, value| ... } ⇒ void
extended
from Arguments::ClassMethods
Desclares option for capturing on initialization process.
-
.place(src, at, dst = nil) ⇒ void
extended
from Sections::ClassMethods
Places specific section in specified place.
-
.placement ⇒ Array<Symbol>
extended
from Sections::ClassMethods
Retrieves section names in current order.
-
#section([name, ...]) ⇒ void
extended
from Sections::ClassMethods
Defines new section or sections.
-
.sections ⇒ Array<Symbol>
extended
from Sections::ClassMethods
Retrieves all sections, including ancestors.
-
.switch(name, options = {}) {|state| ... } ⇒ void
extended
from Switches::ClassMethods
Adds
switch.
Instance Method Summary collapse
-
#[](name) ⇒ String
included
from Sections
Retrieves specified section content.
-
#[]=(name, value) ⇒ String
included
from Sections
Sets specified section content.
-
#capture_arguments!(args, &block) ⇒ Array<Object>
included
from Arguments
Captures arguments.
-
#html_attr ⇒ Hash
included
from HTML
Retrieves HTML attributes hash (without HTML class and HTML data).
-
#html_attr=(hash) ⇒ Hash
included
from HTML
TODO: actually we should have separate setter and merge (see Base).
-
#html_class ⇒ HTMLClass
included
from HTML
Retrieves HTML class of element.
-
#html_class=(value) ⇒ HTMLClass
included
from HTML
Sets HTML class(es) for element.
-
#html_class_prefix ⇒ String
included
from HTML
HTML class prefix getter.
-
#html_data ⇒ Hash
included
from HTML
Retrieves HTML data hash.
-
#initialize(template, *args, &block) ⇒ Base
constructor
A new instance of Base.
- #omit_content? ⇒ Boolean
-
#render([content, ...]) {|element| ... } ⇒ String
Renders element to template.
-
#run_callbacks(name) ⇒ void
included
from Callbacks
Runs specified callbacks with block.
- #tag ⇒ Object
- #tag=(value) ⇒ Object
- #unwrap ⇒ Object
-
#wrap(*args, &block) ⇒ void
Wraps element with another.
Constructor Details
#initialize(template, *args, &block) ⇒ Base
Returns a new instance of Base.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/wrap_it/base.rb', line 54 def initialize(template, *args, &block) @template, @block = template, block add_default_classes run_callbacks :initialize do capture_arguments!(args, &block) # TODO: uncomment following after html_attr implementation finished #html_attr.merge!(args.extract_options!) self.html_attr = args. # TODO: find convenient way to save unprocessed arguments @arguments = args end end |
Instance Attribute Details
#helper_name ⇒ Object
Returns the value of attribute helper_name.
46 47 48 |
# File 'lib/wrap_it/base.rb', line 46 def helper_name @helper_name end |
Class Method Details
#argument(name, opts = {}) {|name, value| ... } ⇒ void Originally defined in module Arguments::ClassMethods
This method returns an undefined value.
Desclares argument for capturing on initialization process.
Inside initialization process, all arguments (except options hash),
passed to constructor will be inspected to satisfy conditions,
specified in :if and :and options. If this happens, and block
given, it evaluated in context of component instance. If no block
given, setter with name will be attempted to set value. In any way
if conditions satisfied, argument removed from future processing.
If no conditions specified, the name of attribute taked as only
condition.
#capture_arguments!(args, opts = {}, &block) ⇒ Array<Object> Originally defined in module Arguments::ClassMethods
Capture arguments for class and it's ancestors. All captured arguments
and options will be extracted from original args argument.
Actually you rare needs to call this method directly. For example you can call it in instance capture_arguments! override to capture arguments for some child components.
.enum(name, values, opts = {}) {|value| ... } ⇒ void Originally defined in module Enums::ClassMethods
This method returns an undefined value.
Adds enum. When element created, creation arguments will be scanned
for Symbol, that included contains in values. If it founded, enum
takes this value. Also creation options inspected. If its contains
name: value key-value pair with valid value, this pair removed from
options and enum takes this value.
If you set html_class option to true, with each enum change, HTML
class, composed from html_class_prefix and enum value will be
added to element. If you want to override this prefix, specify it
with html_class_prefix option. By default, enum changes are not
affected to html classes.
This method also adds getter and setter for this enum.
#html_class([html_class, ...]) ⇒ void Originally defined in module HTML::ClassMethods
This method returns an undefined value.
Adds default html classes, thats are automatically added when element created.
.html_class_prefix(prefix = nil) ⇒ void Originally defined in module HTML::ClassMethods
This method returns an undefined value.
Sets HTML class prefix. It used in switchers and enums
#option(name, opts = {}) {|name, value| ... } ⇒ void Originally defined in module Arguments::ClassMethods
This method returns an undefined value.
Desclares option for capturing on initialization process.
Provides same manner as #argument but for hash of options, passed to constructor. Specified conditions are applied to options keys, not to values.
Hint: you can specify argument and options with same name to call same setter.
#place(src, to) ⇒ void #place(src, at, dst) ⇒ void Originally defined in module Sections::ClassMethods
This method returns an undefined value.
Places specific section in specified place
.placement ⇒ Array<Symbol> Originally defined in module Sections::ClassMethods
Retrieves section names in current order
#section([name, ...]) ⇒ void Originally defined in module Sections::ClassMethods
This method returns an undefined value.
Defines new section or sections. Places its to end of section list
.sections ⇒ Array<Symbol> Originally defined in module Sections::ClassMethods
Retrieves all sections, including ancestors
.switch(name, options = {}) {|state| ... } ⇒ void Originally defined in module Switches::ClassMethods
This method returns an undefined value.
Adds switch. Switch is a boolean flag. When element created, creation
arguments will be scanned for Symbol, that equals to name. If
it founded, switch turned on. Also creation options inspected. If
its contains name: true key-value pair, this pair removed from
options and switch also turned on.
This method also adds getter and setter for this switch in form name?
and name= respectively.
When html_class option specified and switch changes its state, HTML
class for element will be computed as follows. if html_class options
is true, html class produced from html_class_prefix and name of
switch. If html_class is a String, Symbol or Array of this types,
html class produced as array of html_class_prefix and each
html_class concatinations. This classes added to element if switch is
on or removed in other case.
Instance Method Details
#[](name) ⇒ String Originally defined in module Sections
Retrieves specified section content
#[]=(name, value) ⇒ String Originally defined in module Sections
Sets specified section content
#capture_arguments!(args, &block) ⇒ Array<Object> Originally defined in module Arguments
Captures arguments
In rare cases you can override this method to control directly arguments capturing process. Refer to capture_arguments! for examples.
Note that this method is
protected, so override should beprotectedtoo.
#html_attr ⇒ Hash Originally defined in module HTML
Retrieves HTML attributes hash (without HTML class and HTML data)
#html_attr=(hash) ⇒ Hash Originally defined in module HTML
TODO: actually we should have separate setter and merge (see Base)
Sets HTML attributes hash.
Actually it merges its with current
attributes. To remove some attributes use html_attr.delete(:attr).
extracts HTML class and data from provided hash and places its to
appropriate holder
#html_class ⇒ HTMLClass Originally defined in module HTML
Retrieves HTML class of element
See WrapIt::HTMLClass for details
#html_class_prefix ⇒ String Originally defined in module HTML
HTML class prefix getter
This prefix used in enums to combine HTML classes.
#html_data ⇒ Hash Originally defined in module HTML
Retrieves HTML data hash
#omit_content? ⇒ Boolean
81 82 83 |
# File 'lib/wrap_it/base.rb', line 81 def omit_content? self.class.get_derived(:@omit_content) == true end |
#render([content, ...]) {|element| ... } ⇒ String
Renders element to template
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/wrap_it/base.rb', line 97 def render(*args, &render_block) # return cached copy if it available return @rendered unless @rendered.nil? capture_sections # add to content string args and block result if its present args.flatten.each { |a| self[:render_arguments] << a if a.is_a? String } if block_given? result = instance_exec(self, &render_block) || empty_html result.is_a?(String) && self[:render_block] << result end do_render do_wrap if @template.output_buffer.nil? # when render called from code, just return content as a String @rendered else # in template context, write content to templates buffer concat(@rendered) empty_html end end |
#run_callbacks(name) ⇒ void Originally defined in module Callbacks
This method returns an undefined value.
Runs specified callbacks with block
Runs first before callbacks in inheritance order, then yields block if
it given and then after callbacks in reverse order.
#tag ⇒ Object
67 68 69 |
# File 'lib/wrap_it/base.rb', line 67 def tag @tag ||= (self.class.get_derived(:@default_tag) || 'div').to_s end |
#tag=(value) ⇒ Object
71 72 73 74 |
# File 'lib/wrap_it/base.rb', line 71 def tag=(value) value.is_a?(Symbol) && value = value.to_s value.is_a?(String) && @tag = value end |
#unwrap ⇒ Object
155 156 157 |
# File 'lib/wrap_it/base.rb', line 155 def unwrap @wrapper = nil end |
#wrap(wrapper) ⇒ void #wrap(wrapper_class, [arg, ...], options = {}) ⇒ void #wrap([arg, ...], options = {}) ⇒ void
This method returns an undefined value.
Wraps element with another.
You can provide wrapper directly or specify wrapper class as first argument. In this case wrapper will created with specified set of arguments and options. If wrapper class ommited, WrapIt::Base will be used.
If block present, it will be called when wrapper will rendered.
146 147 148 149 150 151 152 153 |
# File 'lib/wrap_it/base.rb', line 146 def wrap(*args, &block) if args.first.is_a?(Base) @wrapper = args.shift else wrapper_class = args.first.is_a?(Class) ? args.shift : Base @wrapper = wrapper_class.new(@template, *args, &block) end end |