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



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

def after_render(&block)
  @after_render_blocks ||= []
  @after_render_blocks << block
end

#before_render(&block) ⇒ Object



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

def before_render(&block)
  @before_render_blocks ||= []
  @before_render_blocks << block
end

#create(*args) ⇒ Object

Creates component without rendering



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

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



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

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

#keywordObject



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

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

#markup(&block) ⇒ Object



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

def markup(&block)
  @markup_block = block
end

#option(new_option, default: nil) ⇒ Object



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

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

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



105
106
107
108
# File 'lib/glimmer/web/component.rb', line 105

def render(*args)
  rendered_component = send(keyword, *args)
  rendered_component
end

#shortcut_keywordObject

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



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

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