Module: Glimmer::Web::Component::ClassMethods

Includes:
Glimmer
Defined in:
lib/glimmer/web/component.rb

Instance Method Summary collapse

Instance Method Details

#after_render(&block) ⇒ Object



88
89
90
# File 'lib/glimmer/web/component.rb', line 88

def after_render(&block)
  @after_render = block
end

#before_render(&block) ⇒ Object



75
76
77
# File 'lib/glimmer/web/component.rb', line 75

def before_render(&block)
  @before_render = block
end

#component_element_classObject



124
125
126
# File 'lib/glimmer/web/component.rb', line 124

def component_element_class
  self.keyword.gsub('_', '-')
end

#component_element_selectorObject Also known as: component_markup_root_selector



128
129
130
# File 'lib/glimmer/web/component.rb', line 128

def component_element_selector
  ".#{component_element_class}"
end

#component_shortcut_element_classObject



133
134
135
# File 'lib/glimmer/web/component.rb', line 133

def component_shortcut_element_class
  self.shortcut_keyword.gsub('_', '-')
end

#create(*args) ⇒ Object

Creates component without rendering



138
139
140
141
142
143
# File 'lib/glimmer/web/component.rb', line 138

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



64
65
66
67
68
69
70
71
72
73
# File 'lib/glimmer/web/component.rb', line 64

def def_option_attr_accessors(new_options)
  new_options.each do |option, default|
    define_method(option) do
      options[:"#{option}"]
    end
    define_method("#{option}=") do |option_value|
      self.options[:"#{option}"] = option_value
    end
  end
end

#default_slot(slot_name = nil) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/glimmer/web/component.rb', line 107

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



92
93
94
95
96
# File 'lib/glimmer/web/component.rb', line 92

def event(event_name)
  @events ||= []
  event_name = event_name.to_sym
  @events << event_name unless @events.include?(event_name)
end

#events(*event_names) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/glimmer/web/component.rb', line 98

def events(*event_names)
  @events ||= []
  if event_names.empty?
    @events
  else
    event_names.each { |event| event(event) }
  end
end

#keywordObject



115
116
117
# File 'lib/glimmer/web/component.rb', line 115

def keyword
  self.name.underscore.gsub('::', '__')
end

#markup(&block) ⇒ Object



79
80
81
# File 'lib/glimmer/web/component.rb', line 79

def markup(&block)
  @markup_block = block
end

#option(new_option, default: nil) ⇒ Object Also known as: attribute



54
55
56
57
58
59
60
61
# File 'lib/glimmer/web/component.rb', line 54

def option(new_option, default: nil)
  new_option = new_option.to_s.to_sym
  new_options = {new_option => default}
  '@options = options.merge(new_options)'
  @options = options.merge(new_options)
  'def_option_attr_accessors(new_options)'
  def_option_attr_accessors(new_options)
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



42
43
44
45
46
47
48
49
50
51
# File 'lib/glimmer/web/component.rb', line 42

def options(*new_options)
  new_options = new_options.compact.map(&:to_s).map(&:to_sym)
  if new_options.empty?
    @options ||= {} # maps options to defaults
  else
    new_options = new_options.reduce({}) {|new_options_hash, new_option| new_options_hash.merge(new_option => nil)}
    @options = options.merge(new_options)
    def_option_attr_accessors(new_options)
  end
end

#render(*args) ⇒ Object

Creates and renders component



146
147
148
149
150
# File 'lib/glimmer/web/component.rb', line 146

def render(*args)
  Glimmer::DSL::Engine.new_parent_stack unless Glimmer::DSL::Engine.parent.nil?
  rendered_component = send(keyword, *args)
  rendered_component
end

#shortcut_keywordObject

Returns shortcut keyword to use for this component (keyword minus namespace)



120
121
122
# File 'lib/glimmer/web/component.rb', line 120

def shortcut_keyword
  self.name.underscore.gsub('::', '__').split('__').last
end

#style(&block) ⇒ Object

TODO in the future support a string value too



84
85
86
# File 'lib/glimmer/web/component.rb', line 84

def style(&block)
  @style_block = block
end