Module: Roda::RodaPlugins::Component::InstanceMethods
- Defined in:
- lib/roda/plugins/component.rb
Instance Method Summary collapse
- #component(name, options = {}, &block) ⇒ Object (also: #comp, #roda_component)
- #component_js ⇒ Object
- #component_opts ⇒ Object
- #load_component(name) ⇒ Object
- #load_component_js(comp, action = :display) ⇒ Object
- #loaded_component_js ⇒ Object
Instance Method Details
#component(name, options = {}, &block) ⇒ Object Also known as: comp, roda_component
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/roda/plugins/component.rb', line 101 def component name, = {}, &block comp = load_component name action = .delete(:call) || :display trigger = .delete(:trigger) || false # call action # TODO: make sure the single method parameter isn't a block if trigger comp_response = comp.trigger trigger, else # We want to make sure it's not a method that already exists in ruba # otherwise that would give us a false positive. if comp.respond_to?(action) && !"#{comp.method(action)}"[/\(Kernel\)/] if comp.method(action).parameters.length > 0 comp_response = comp.send(action, , &block) else comp_response = comp.send(action, &block) end else fail "##{action} doesn't exist for #{comp.class}" end end load_component_js comp, action if comp_response.is_a? Roda::Component::DOM comp_response = comp_response.to_xml end if comp_response.is_a?(String) && js = .delete(:js) comp_response << component_js end comp_response end |
#component_js ⇒ Object
140 141 142 |
# File 'lib/roda/plugins/component.rb', line 140 def component_js loaded_component_js.join('\n').to_s end |
#component_opts ⇒ Object
57 58 59 |
# File 'lib/roda/plugins/component.rb', line 57 def component_opts @_component_opts || self.class.component_opts.dup end |
#load_component(name) ⇒ Object
65 66 67 68 69 |
# File 'lib/roda/plugins/component.rb', line 65 def load_component name Object.const_get( component_opts[:class_name][name.to_s] ).new self end |
#load_component_js(comp, action = :display) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/roda/plugins/component.rb', line 71 def load_component_js comp, action = :display # grab a copy of the cache cache = comp.class.cache.dup # remove html and dom cache as we don't need that for the client cache.delete :html cache.delete :dom cache.delete :cache cache = Base64.encode64 cache.to_json = Base64.encode64 .to_json comp_name = comp.class._name js = <<-EOF unless $faye $faye = Roda::Component::Faye.new('/faye') end unless $component_opts[:comp][:"#{comp_name}"] Document.ready? do c = $component_opts[:comp][:"#{comp_name}"] = #{comp.class}.new c.instance_variable_set(:@_cache, JSON.parse(Base64.decode64('#{cache}'))) c.#{action}(JSON.parse(Base64.decode64('#{}'))) c.events.trigger_jquery_events end end EOF loaded_component_js << ("<script>" + Opal.compile(js) + "</script>") end |
#loaded_component_js ⇒ Object
61 62 63 |
# File 'lib/roda/plugins/component.rb', line 61 def loaded_component_js request.env['loaded_component_js'] ||= [] end |