Module: Roda::RodaPlugins::Component::InstanceMethods

Defined in:
lib/roda/plugins/component.rb

Instance Method Summary collapse

Instance Method Details

#component(name, options = {}, &block) ⇒ Object Also known as: comp, roda_component



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
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/roda/plugins/component.rb', line 110

def component name, options = {}, &block
  comp = load_component name

  action  = options.delete(:call)    || :display
  trigger = options.delete(:trigger) || false
  js      = options.delete(:js)
  args    = options.delete(:args)

  # call action
  # TODO: make sure the single method parameter isn't a block
  if trigger
    if args
      comp_response = comp.trigger trigger, *args
    else
      comp_response = comp.trigger trigger, options
    end
  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
        if args
          comp_response = comp.send(action, *args, &block)
        else
          comp_response = comp.send(action, options, &block)
        end
      else
        comp_response = comp.send(action, &block)
      end
    else
      fail "##{action} doesn't exist for #{comp.class}"
    end
  end

  load_component_js comp, action, options

  if js && comp_response.is_a?(Roda::Component::DOM)
    comp_response = comp_response.to_xml
  end

  if comp_response.is_a?(String) && js
    comp_response << component_js
  end

  comp_response
end

#component_jsObject



159
160
161
# File 'lib/roda/plugins/component.rb', line 159

def component_js
  loaded_component_js.join(' ').to_s
end

#component_optsObject



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, options = {}) ⇒ 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
107
108
# File 'lib/roda/plugins/component.rb', line 72

def load_component_js comp, action = :display, options = {}
  # 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
  options   = Base64.encode64 options.to_json
  comp_name = comp.class._name

  file_path = comp.class.instance_methods(false).map { |m|
      comp.class.instance_method(m).source_location.first
  }.uniq.first.gsub("#{Dir.pwd}/#{component_opts[:path]}", '').gsub(/\.rb\Z/, '.js')

  js = <<-EOF
    unless $faye
      $faye = Roda::Component::Faye.new('/faye')
    end

    unless $component_opts[:comp][:"#{comp_name}"]
      $component_opts[:faye] ||= {}
      $component_opts[:comp][:"#{comp_name}"] = true
      `$.getScript("/#{component_opts[:assets_route]}#{file_path}", function(){`
        Document.ready? do
          c = $component_opts[:comp][:"#{comp_name}"] = #{comp.class}.new
          c.instance_variable_set(:@_cache, JSON.parse(Base64.decode64('#{cache}')))
          c.events.trigger_jquery_events
          c.#{action}(JSON.parse(Base64.decode64('#{options}')))
        end
      `});`
    end
  EOF

  loaded_component_js << ("<script>#{Opal.compile(js)}</script>")
end

#loaded_component_jsObject



62
63
64
# File 'lib/roda/plugins/component.rb', line 62

def loaded_component_js
  request.env['loaded_component_js'] ||= []
end