Class: Roda::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/roda/component.rb,
lib/roda/component/dom.rb,
lib/roda/component/faye.rb,
lib/roda/component/faye.rb,
lib/roda/component/form.rb,
lib/roda/component/events.rb,
lib/roda/component/version.rb,
lib/roda/component/instance.rb,
lib/roda/component/form/validations.rb

Defined Under Namespace

Classes: DOM, Events, Faye, Form, Instance

Constant Summary collapse

VERSION =
"0.1.2"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope = false) ⇒ Component

Returns a new instance of Component.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/roda/component.rb', line 36

def initialize(scope = false)
  @scope   = scope

  if client?
    $faye.subscribe "/components/#{self.class._name}" do |msg|
      msg = Native(msg)

      trigger :"#{msg[:type]}", msg unless $faye.public_id == msg[:public_id]
    end

    $faye.on 'transport:up' do
      $faye.online = true

      if $faye.disconnected
        trigger :reconnect
      else
        trigger :connect
      end
    end

    $faye.on 'transport:down' do
      $faye.disconnected = true
      $faye.online       = false
      trigger :disconnect
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



279
280
281
282
283
284
285
# File 'lib/roda/component.rb', line 279

def method_missing method, *args, &block
  if server? && scope.respond_to?(method, true)
    scope.send method, *args, &block
  else
    super
  end
end

Class Attribute Details

._name ⇒ Object

Returns the value of attribute _name.



65
66
67
# File 'lib/roda/component.rb', line 65

def _name
  @_name
end

Instance Attribute Details

#cache ⇒ Object

Returns the value of attribute cache.



34
35
36
# File 'lib/roda/component.rb', line 34

def cache
  @cache
end

#scope ⇒ Object

Returns the value of attribute scope.



34
35
36
# File 'lib/roda/component.rb', line 34

def scope
  @scope
end

Class Method Details

.add_tmpl ⇒ Object

We need to save the oga dom and the raw html. the reason we ave the raw html is so that we can use it client side.



212
213
214
215
216
# File 'lib/roda/component.rb', line 212

def tmpl name, dom, remove = true
  cache[:tmpl][name] = { dom: remove ? dom.remove : dom }
  cache[:tmpl][name][:html] = cache[:tmpl][name][:dom].to_xml
  cache[:tmpl][name]
end

.app ⇒ Object

roda app method



201
202
203
# File 'lib/roda/component.rb', line 201

def app
  @_app ||= {}
end

.cache ⇒ Object

cache for class



185
186
187
188
189
190
191
192
193
# File 'lib/roda/component.rb', line 185

def cache
  unless @_cache
    @_cache ||= Roda::RodaCache.new
    @_cache[:tmpl] = {}
    @_cache[:server_methods] = []
  end

  @_cache
end

.comp_html(_html, &block) ⇒ Object

The html source



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/roda/component.rb', line 137

def comp_html _html, &block
  if server?
    if _html.is_a? String
      if _html[%r{\A./}]
        cache[:html] = File.read _html
      else
        cache[:html] = File.read "#{component_opts[:path]}/#{_html}"
      end
    else
      cache[:html] = yield
    end

    cache[:dom] = DOM.new(cache[:html])
  end
end

.comp_name(_name = nil) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/roda/component.rb', line 126

def comp_name(_name = nil)
  return name_original unless _name

  @_name = _name.to_s

  if server?
    component_opts[:class_name][@_name] = self.to_s
  end
end

.comp_setup(&block) ⇒ Object

setup your dom



168
169
170
# File 'lib/roda/component.rb', line 168

def comp_setup &block
  block.call cache[:dom] if server?
end

.component_opts ⇒ Object

shortcut to comp opts



216
217
218
219
220
221
222
# File 'lib/roda/component.rb', line 216

def component_opts
  if client?
    $component_opts
  else
    super
  end
end

.events ⇒ Object



172
173
174
# File 'lib/roda/component.rb', line 172

def events
  @_events ||= Events.new self, component_opts, false
end

.HTML(raw_html) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/roda/component.rb', line 153

def HTML raw_html
  if defined? Oga
    Oga.parse_html(raw_html)
  elsif defined? Nokogiri
    if raw_html[/\A<!DOCTYPE/]
      Nokogiri::HTML(raw_html)
    else
      Nokogiri::HTML.fragment(raw_html)
    end
  else
    warn 'No HTML parsing lib loaded.  Please require Nokogiri or Oga'
  end
end

.method_missing(method, *args, &block) ⇒ Object



224
225
226
227
228
229
230
# File 'lib/roda/component.rb', line 224

def method_missing method, *args, &block
  if server? && app.respond_to?(method, true)
    app.send method, *args, &block
  else
    super
  end
end

.on(*args, &block) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/roda/component.rb', line 176

def on *args, &block
  if args.first.to_s != 'server'
    events.on(*args, &block)
  else
    on_server &block
  end
end

.on_server(&block) ⇒ Object



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/roda/component.rb', line 75

def on_server &block
  if server?
    yield
  else
    m = Module.new(&block)

    m.public_instance_methods(false).each do |meth|
      define_method "#{meth}" do |*args, &blk|
        name     = self.class._name
        # event_id = "comp-event-#{$faye.generate_id}"

        HTTP.post("/components/#{name}/call/#{meth}",
          headers: {
            'X-CSRF-TOKEN' => Element.find('meta[name=_csrf]').attr('content'),
            'X-RODA-COMPONENT-ON-SERVER' => true
          },
          payload: args.first) do |response|

            # We set the new csrf token
            xhr  = Native(response.xhr)
            csrf = xhr.getResponseHeader('NEW-CSRF')
            Element.find('meta[name=_csrf]').attr 'content', csrf
            ###########################

            blk.call Native(response.body), response
        end

        # Element['body'].on event_id do |event, local, data|
        #   blk.call local, event, data
        # end
        #
        # $faye.publish("/components/outgoing/#{$faye.private_id}/#{$faye.public_id}", {
        #   name: name,
        #   type: 'event',
        #   event_type: 'call',
        #   event_method: meth,
        #   event_id: event_id,
        #   local: args.first || nil
        # })


        true
      end
    end

    include m
  end
end

.set_app(app) ⇒ Object

set the current roda app



196
197
198
# File 'lib/roda/component.rb', line 196

def set_app app
  @_app = app.respond_to?(:new) ? app.new : app
end

.set_tmpl ⇒ Object

We need to save the oga dom and the raw html. the reason we ave the raw html is so that we can use it client side.



213
214
215
216
217
# File 'lib/roda/component.rb', line 213

def tmpl name, dom, remove = true
  cache[:tmpl][name] = { dom: remove ? dom.remove : dom }
  cache[:tmpl][name][:html] = cache[:tmpl][name][:dom].to_xml
  cache[:tmpl][name]
end

.tmpl(name, dom, remove = true) ⇒ Object

We need to save the oga dom and the raw html. the reason we ave the raw html is so that we can use it client side.



207
208
209
210
211
# File 'lib/roda/component.rb', line 207

def tmpl name, dom, remove = true
  cache[:tmpl][name] = { dom: remove ? dom.remove : dom }
  cache[:tmpl][name][:html] = cache[:tmpl][name][:dom].to_xml
  cache[:tmpl][name]
end

Instance Method Details

#component_opts ⇒ Object



271
272
273
# File 'lib/roda/component.rb', line 271

def component_opts
  self.class.component_opts
end

#dom ⇒ Object Also known as: element



251
252
253
254
255
256
257
258
# File 'lib/roda/component.rb', line 251

def dom
  if server?
    # TODO: duplicate cache[:dom] so we don't need to parse all the html again
    @_dom ||= DOM.new(cache[:dom].dom.to_s)
  else
    @_dom ||= DOM.new(Element)
  end
end

#events ⇒ Object



247
248
249
# File 'lib/roda/component.rb', line 247

def events
  @_events ||= Events.new self.class, component_opts, scope
end

#inherited(subclass) ⇒ Object



68
69
70
71
72
# File 'lib/roda/component.rb', line 68

def inherited(subclass)
  super
  # We want to set the app for all sub classes
  subclass.set_app app
end

#tmpl(name) ⇒ Object

Grab the template from the cache, use the nokogiri dom or create a jquery element for server side



263
264
265
266
267
268
269
# File 'lib/roda/component.rb', line 263

def tmpl name
  if t = cache[:tmpl][name]
    DOM.new t[:html]
  else
    false
  end
end

#trigger(*args) ⇒ Object



275
276
277
# File 'lib/roda/component.rb', line 275

def trigger *args
  events.trigger(*args)
end