Module: Glimmer::Web::Component::ClassMethods
- Includes:
- Glimmer
- Defined in:
- lib/glimmer/web/component.rb
Instance Method Summary collapse
- #after_render(&block) ⇒ Object
- #before_render(&block) ⇒ Object
- #component_element_class ⇒ Object
- #component_element_selector ⇒ Object (also: #component_markup_root_selector, #component_structure_root_selector)
- #component_shortcut_element_class ⇒ Object
-
#create(*args) ⇒ Object
Creates component without rendering.
- #def_option_attr_accessors(new_options) ⇒ Object
- #default_slot(slot_name = nil) ⇒ Object
- #event(event_name) ⇒ Object
- #events(*event_names) ⇒ Object
- #keyword ⇒ Object
- #markup(&block) ⇒ Object (also: #structure)
- #option(new_option, default: nil) ⇒ Object (also: #attribute)
- #options(*new_options) ⇒ Object (also: #attributes)
-
#render(*args) ⇒ Object
Creates and renders component.
-
#shortcut_keyword ⇒ Object
Returns shortcut keyword to use for this component (keyword minus namespace).
-
#style(&block) ⇒ Object
TODO in the future support a string value too.
Instance Method Details
#after_render(&block) ⇒ Object
68 69 70 |
# File 'lib/glimmer/web/component.rb', line 68 def after_render(&block) @after_render = block end |
#before_render(&block) ⇒ Object
54 55 56 |
# File 'lib/glimmer/web/component.rb', line 54 def before_render(&block) @before_render = block end |
#component_element_class ⇒ Object
104 105 106 |
# File 'lib/glimmer/web/component.rb', line 104 def component_element_class self.keyword.gsub('_', '-') end |
#component_element_selector ⇒ Object Also known as: component_markup_root_selector, component_structure_root_selector
108 109 110 |
# File 'lib/glimmer/web/component.rb', line 108 def component_element_selector ".#{component_element_class}" end |
#component_shortcut_element_class ⇒ Object
114 115 116 |
# File 'lib/glimmer/web/component.rb', line 114 def component_shortcut_element_class self.shortcut_keyword.gsub('_', '-') end |
#create(*args) ⇒ Object
Creates component without rendering
119 120 121 122 123 124 |
# File 'lib/glimmer/web/component.rb', line 119 def create(*args) args << {} unless args.last.is_a?(Hash) args.last[:render] = false rendered_component = send(keyword, *args) rendered_component end |
#def_option_attr_accessors(new_options) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/glimmer/web/component.rb', line 43 def def_option_attr_accessors() .each do |option, default| define_method(option) do [:"#{option}"] end define_method("#{option}=") do |option_value| self.[:"#{option}"] = option_value end end end |
#default_slot(slot_name = nil) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/glimmer/web/component.rb', line 87 def default_slot(slot_name = nil) if slot_name.nil? @default_slot else @default_slot = slot_name.to_s.to_sym end end |
#event(event_name) ⇒ Object
72 73 74 75 76 |
# File 'lib/glimmer/web/component.rb', line 72 def event(event_name) @events ||= [] event_name = event_name.to_sym @events << event_name unless @events.include?(event_name) end |
#events(*event_names) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/glimmer/web/component.rb', line 78 def events(*event_names) @events ||= [] if event_names.empty? @events else event_names.each { |event| event(event) } end end |
#keyword ⇒ Object
95 96 97 |
# File 'lib/glimmer/web/component.rb', line 95 def keyword self.name.underscore.gsub('::', '__') end |
#markup(&block) ⇒ Object Also known as: structure
58 59 60 |
# File 'lib/glimmer/web/component.rb', line 58 def markup(&block) @markup_block = block end |
#option(new_option, default: nil) ⇒ Object Also known as: attribute
33 34 35 36 37 38 39 40 |
# File 'lib/glimmer/web/component.rb', line 33 def option(new_option, default: nil) new_option = new_option.to_s.to_sym = {new_option => default} '@options = options.merge(new_options)' @options = .merge() 'def_option_attr_accessors(new_options)' def_option_attr_accessors() end |
#options(*new_options) ⇒ Object Also known as: attributes
Allows defining convenience option accessors for an array of option names Example: ‘options :color1, :color2` defines `#color1` and `#color2` where they return the instance values `options` and `options` respectively. Can be called multiple times to set more options additively. When passed no arguments, it returns list of all option names captured so far
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/glimmer/web/component.rb', line 21 def (*) = .compact.map(&:to_s).map(&:to_sym) if .empty? @options ||= {} # maps options to defaults else = .reduce({}) {|, new_option| .merge(new_option => nil)} @options = .merge() def_option_attr_accessors() end end |
#render(*args) ⇒ Object
Creates and renders component
127 128 129 130 131 |
# File 'lib/glimmer/web/component.rb', line 127 def render(*args) Glimmer::DSL::Engine.new_parent_stack unless Glimmer::DSL::Engine.parent.nil? rendered_component = send(keyword, *args) rendered_component end |
#shortcut_keyword ⇒ Object
Returns shortcut keyword to use for this component (keyword minus namespace)
100 101 102 |
# File 'lib/glimmer/web/component.rb', line 100 def shortcut_keyword self.name.underscore.gsub('::', '__').split('__').last end |
#style(&block) ⇒ Object
TODO in the future support a string value too
64 65 66 |
# File 'lib/glimmer/web/component.rb', line 64 def style(&block) @style_block = block end |