Module: Ovto

Defined in:
lib/ovto.rb,
lib/ovto/app.rb,
lib/ovto/fetch.rb,
lib/ovto/state.rb,
lib/ovto/actions.rb,
lib/ovto/runtime.rb,
lib/ovto/version.rb,
lib/ovto/component.rb,
lib/ovto/middleware.rb,
lib/ovto/wired_actions.rb,
lib/ovto/pure_component.rb,
lib/ovto/wired_action_set.rb

Defined Under Namespace

Modules: Middleware Classes: Actions, App, Component, PureComponent, Runtime, State, WiredActionSet, WiredActions

Constant Summary collapse

VERSION =
'0.6.0'
VALID_NAME_REXP =
/\A[a-zA-Z0-9_]+\z/

Class Method Summary collapse

Class Method Details

._do_fetch(url, init) ⇒ Object

Create an Opal Promise to call fetch API



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ovto/fetch.rb', line 34

def self._do_fetch(url, init)
  promise = Promise.new
  text = error = nil
  %x{
    fetch(url, init).then(response => {
      if (response.ok) {
        return response.text();
      }
      else {
        throw response;
      }
    }).then(text =>
      #{promise.resolve(JSON.parse(text))}
    ).catch(error =>
      #{promise.reject(error)}
    );
  }
  return promise
end

.debug_traceObject



22
# File 'lib/ovto.rb', line 22

def self.debug_trace; @debug_trace; end

.debug_trace=(bool) ⇒ Object



23
# File 'lib/ovto.rb', line 23

def self.debug_trace=(bool); @debug_trace = bool; end

.debug_trace_log(msg) ⇒ Object



24
25
26
# File 'lib/ovto.rb', line 24

def self.debug_trace_log(msg)
  console.log("Ovto: "+msg) if @debug_trace
end

.fetch(url, method = 'GET', data = nil) ⇒ Object

Wrapper for the fetch API The server must respond a json text.

Example:

Ovto.fetch('/api/new_task', 'POST', {title: "do something"}).then{|json_data|
  p json_data
}.fail{|e|  # Network error, 404 Not Found, JSON parse error, etc.
  p e
}


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ovto/fetch.rb', line 15

def self.fetch(url, method='GET', data=nil)
  init = `{
    method: #{method},
    credentials: 'same-origin' // Send cookies to the server (eg. for CookieStore of Rails)
  }`
  if method != 'GET'
    %x{
      var headers = {'Content-Type': 'application/json'};
      var metaTag = document.querySelector('meta[name=csrf-token]');
      if (metaTag) headers['X-CSRF-Token'] = metaTag.content;

      init['headers'] = headers;
      init['body'] = #{data.to_json};
    }
  end
  return _do_fetch(url, init)
end

.inspect(obj) ⇒ Object

JS-object-safe inspect



29
30
31
32
33
34
35
# File 'lib/ovto.rb', line 29

def self.inspect(obj)
  if `obj.$inspect`
    obj.inspect
  else
    `JSON.stringify(#{obj}) || "undefined"`
  end
end

.log_error(&block) ⇒ Object

Call block. If an exception is raised and there is a tag with `id='ovto-debug'`, describe the error in that tag



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ovto.rb', line 39

def self.log_error(&block)
  return block.call
rescue Exception => ex
  raise ex if `typeof document === 'undefined'`  # On unit tests

  div = `document.getElementById('ovto-debug')`
  if `div && !ex.OvtoPrinted`
    %x{
      div.textContent = "ERROR: " + #{ex.class.name};
      var ul = document.createElement('ul');
      // Note: ex.backtrace may be an Array or a String
      #{Array(ex.backtrace)}.forEach(function(line){
        var li = document.createElement('li');
        li.textContent = line;
        ul.appendChild(li);
      });
      div.appendChild(ul);
      ex.OvtoPrinted = true;
    }
  end
  raise ex
end

.Middleware(name) ⇒ Object

Create an ancestor of middleware class Example:

class MiddlewareExample < Ovto::Middleware("middleware_example")


6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ovto/middleware.rb', line 6

def self.Middleware(name)
  unless VALID_NAME_REXP =~ name
    raise "invalid middleware name: #{name}"
  end
  return Class.new(Ovto::Middleware::Base){
    const_set(:OVTO_MIDDLEWARE_NAME, name)
    const_set(:State, Ovto::State)
    const_set(:Actions, Class.new(Ovto::Middleware::Actions))
    const_get(:Actions).const_set(:OVTO_MIDDLEWARE_NAME, name)
    const_set(:Component, Class.new(Ovto::Middleware::Component))
    const_get(:Component).const_set(:OVTO_MIDDLEWARE_NAME, name)
  }
end

.send_args_with_state(obj, meth, args, state, &block) ⇒ Object

Something like `obj.meth(state: state, **args, &block)` Safe even if `obj.meth` does not have `state:`



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ovto.rb', line 64

def self.send_args_with_state(obj, meth, args, state, &block)
  parameters = obj.method(meth).parameters
  accepts_state = `!parameters` || parameters.nil? || parameters.any?{|item|
    item == [:key, :state] ||
    item == [:keyreq, :state] ||
    item[0] == :keyrest
  }
  if accepts_state
    # We can pass `state:` safely
    args_with_state = {state: state}.merge(args)
    return obj.__send__(meth, args_with_state, &block)
  else
    # Check it is empty (see https://github.com/opal/opal/issues/1872)
    if args.empty?
      return obj.__send__(meth, &block)
    else
      return obj.__send__(meth, **args, &block)
    end
  end
end