Module: WrapIt::Switches::ClassMethods
- Defined in:
- lib/wrap_it/switches.rb
Overview
Class methods to include
Instance Method Summary collapse
-
#switch(name, options = {}) {|state| ... } ⇒ void
Adds ‘switch`.
Instance Method Details
#switch(name, options = {}) {|state| ... } ⇒ void
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.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/wrap_it/switches.rb', line 72 def switch(name, = {}, &block) .symbolize_keys! name = name.to_sym .merge!(block: block, name: name) if .key?(:html_class) [:html_class] = if [:html_class] == true [html_class_prefix + name.to_s] else HTMLClass.sanitize([:html_class]).map do |c| html_class_prefix + c end end end names = [name] + [[[:aliases]] || []].flatten.compact define_method("#{name}?") { @switches[name] == true } define_method("#{name}=", &Switches.setter(name, &block)) @switches ||= {} names.each { |n| @switches[n] = } end |