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
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 137 138 139 140 141 142 143 |
# File 'lib/roda/plugins/component.rb', line 108 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
147 148 149 |
# File 'lib/roda/plugins/component.rb', line 147 def component_js loaded_component_js.join(' ').to_s end |
#component_opts ⇒ Object
58 59 60 |
# File 'lib/roda/plugins/component.rb', line 58 def component_opts @_component_opts || self.class.component_opts.dup end |
#load_component(name) ⇒ Object
66 67 68 69 70 |
# File 'lib/roda/plugins/component.rb', line 66 def load_component name Object.const_get( component_opts[:class_name][name.to_s] ).new self end |
#load_component_js(comp, action = :display) ⇒ Object
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 100 101 102 103 104 105 106 |
# File 'lib/roda/plugins/component.rb', line 72 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 = " unless $faye\n $faye = Roda::Component::Faye.new('/faye')\n end\n\n unless $component_opts[:comp][:\"\#{comp_name}\"]\n Document.ready? do\n c = $component_opts[:comp][:\"\#{comp_name}\"] = \#{comp.class}.new\n c.instance_variable_set(:@_cache, JSON.parse(Base64.decode64('\#{cache}')))\n c.\#{action}(JSON.parse(Base64.decode64('\#{options}')))\n c.events.trigger_jquery_events\n end\n end\n EOF\n\n loaded_component_js << (\"<script>\#{Opal.compile(js)}</script>\")\n\n file_path = comp.class.instance_methods(false).map { |m|\n comp.class.instance_method(m).source_location.first\n }.uniq.first.gsub(\"\#{Dir.pwd}/\#{component_opts[:path]}\", '').gsub(/\\.rb\\Z/, '.js')\n\n loaded_component_js << \"<script type=\\\"text/javascript\\\" src=\\\"/\#{component_opts[:assets_route]}\#{file_path}\\\"></script>\"\nend\n" |
#loaded_component_js ⇒ Object
62 63 64 |
# File 'lib/roda/plugins/component.rb', line 62 def loaded_component_js request.env['loaded_component_js'] ||= [] end |