Class: Roda::RodaPlugins::Components::ComponentRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/roda/plugins/components.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, component_class, component_name, opts = {}, block) ⇒ ComponentRequest

Returns a new instance of ComponentRequest.



98
99
100
101
102
103
104
105
# File 'lib/roda/plugins/components.rb', line 98

def initialize app, component_class, component_name, opts = {}, block
  @app             = app
  @component_class = component_class
  @component_name  = component_name
  @component_opts  = opts
  @cache           = Roda::RodaCache.new
  @_block          = block
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



96
97
98
# File 'lib/roda/plugins/components.rb', line 96

def app
  @app
end

#cacheObject (readonly)

Returns the value of attribute cache.



96
97
98
# File 'lib/roda/plugins/components.rb', line 96

def cache
  @cache
end

#component_classObject (readonly)

Returns the value of attribute component_class.



96
97
98
# File 'lib/roda/plugins/components.rb', line 96

def component_class
  @component_class
end

#component_nameObject (readonly)

Returns the value of attribute component_name.



96
97
98
# File 'lib/roda/plugins/components.rb', line 96

def component_name
  @component_name
end

#component_optsObject (readonly)

Returns the value of attribute component_opts.



96
97
98
# File 'lib/roda/plugins/components.rb', line 96

def component_opts
  @component_opts
end

Instance Method Details

#blockObject



173
174
175
# File 'lib/roda/plugins/components.rb', line 173

def block
  @_block
end

#display(&block) ⇒ Object



115
116
117
# File 'lib/roda/plugins/components.rb', line 115

def display &block
  on 'display', &block
end

#domObject



133
134
135
# File 'lib/roda/plugins/components.rb', line 133

def dom
  cache[:dom] ||= comp_cache[:dom].dup
end

#dom_htmlObject



137
138
139
# File 'lib/roda/plugins/components.rb', line 137

def dom_html
  dom.to_html
end

#html(&block) ⇒ Object



119
120
121
122
123
124
# File 'lib/roda/plugins/components.rb', line 119

def html &block
  comp_cache[:html_loaded] ||= begin
    comp_cache[:html] ||= yield
    true
  end
end

#on(name, &block) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/roda/plugins/components.rb', line 107

def on name, &block
  name = name.to_s

  if name == component_opts[:call].to_s
    throw :halt, yield(component_opts[:locals] || {})
  end
end

#set_tmpl(name, value, keep = false) ⇒ Object



145
146
147
148
# File 'lib/roda/plugins/components.rb', line 145

def set_tmpl name, value, keep = false
  comp_tmpl[name] = value
  value.remove unless keep
end

#setup(&block) ⇒ Object



126
127
128
129
130
131
# File 'lib/roda/plugins/components.rb', line 126

def setup &block
  comp_cache[:ran_setup] ||= begin
    block.call comp_dom, comp_tmpl
    true
  end
end

#tmpl(name) ⇒ Object



141
142
143
# File 'lib/roda/plugins/components.rb', line 141

def tmpl name
  (cache[:tmpl] ||= {}).fetch(name){ comp_tmpl.fetch(name).dup }
end

#trigger(event, opts = {}) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/roda/plugins/components.rb', line 154

def trigger event, opts = {}
  event = event.to_s

  if opts.key?(:for)
    name = opts.delete(:for).to_s
  else
    name = component_name
  end

  if events = class_cache[:events][name]
    (events[event] || []).each do |e|
      if component_opts[:call] != e[:call]
        e_opts = opts.dup.merge({call: e[:call]})
        app.component e[:component], e_opts
      end
    end
  end
end

#trigger_eventsObject



150
151
152
# File 'lib/roda/plugins/components.rb', line 150

def trigger_events
  trigger component_opts.dup.delete(:call), component_opts
end