Class: Wedge

Inherits:
Object show all
Includes:
Methods
Defined in:
lib/wedge.rb,
lib/wedge/dom.rb,
lib/wedge/html.rb,
lib/wedge/opal.rb,
lib/wedge/config.rb,
lib/wedge/events.rb,
lib/wedge/version.rb,
lib/wedge/component.rb,
lib/wedge/middleware.rb,
lib/wedge/plugins/form.rb,
lib/wedge/plugins/pjax.rb,
lib/wedge/utilis/methods.rb,
lib/wedge/plugins/history.rb,
lib/wedge/plugins/uploader.rb,
lib/wedge/plugins/validations.rb,
lib/wedge/utilis/indifferent_hash.rb

Defined Under Namespace

Modules: HTML, Methods, Plugins Classes: Component, Config, DOM, Events, IndifferentHash, Middleware

Constant Summary collapse

ATTR_ACCESSORS =
%i{scope store config events}
Opal =

Create our own opal instance.

::Opal.dup
VERSION =
'0.1.7'
Form =
Wedge::Plugins::Form

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Methods

#client?, included, #server?

Class Method Details

.[](name, *args, &block) ⇒ Wedge::Component#method

Used to call a component.

Examples:

Browser[:foo].bar

Parameters:

  • name (String, Symbol, #to_s)

    The unique name given to a component.

Returns:



92
93
94
# File 'lib/wedge.rb', line 92

def [](name, *args, &block)
  config.component_class[name].wedge_new self, *args, &block
end

.assets_urlObject



30
31
32
33
# File 'lib/wedge.rb', line 30

def assets_url
  url = config.assets_url.gsub(%r{^(http(|s)://[^\/]*\/|\/)}, '/')
  "#{url}#{config.cache_assets ? "/#{config.assets_key}" : ''}"
end

.assets_url_regexObject



35
36
37
38
39
40
41
42
# File 'lib/wedge.rb', line 35

def assets_url_regex
  @assets_url_regex ||= begin
    assets_url = ::Wedge.assets_url.gsub(%r{^\/}, '')
    # # We also allow for no assets key so when we post server methods there
    # # isn't an error if the key has been changed since a browser refresh.
    %r{(?:#{assets_url}|#{assets_url.sub("#{::Wedge.config.assets_key}/", '')})/(.*)\.(.*)$}
  end
end

.assets_url_with_hostObject



44
45
46
# File 'lib/wedge.rb', line 44

def assets_url_with_host
  "#{config.assets_url}#{config.cache_assets ? "/#{config.assets_key}" : ''}"
end

.configObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/wedge.rb', line 214

def config
  @config ||= begin
    args = { component_class: IndifferentHash.new }

    unless RUBY_ENGINE == 'opal'
      args[:assets_key] = begin
        if defined?(PlatformAPI) && ENV['HEROKU_TOKEN'] && ENV['HEROKU_APP']
          heroku = PlatformAPI.connect_oauth(ENV['HEROKU_TOKEN'], default_headers: {'Range' => 'version ..; order=desc'})
          slug_id = heroku.release.list(ENV['HEROKU_APP']).first["slug"]["id"]
          heroku.slug.info(ENV['HEROKU_APP'], slug_id)["commit"]
        else
          `git rev-parse HEAD 2>/dev/null`.to_s.strip
        end
      end
    end

    Config.new(args)
  end
end

.eventsObject



81
82
83
# File 'lib/wedge.rb', line 81

def events
  @events ||= Events.new
end

.html!(scope = false, &block) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/wedge.rb', line 48

def html!(scope = false, &block)
  if !block_given?
    DOM.new HTML::DSL.html(&scope).to_html
  else
    DOM.new HTML::DSL.scope!(scope).html(&block).to_html
  end
end

.javascript(path_name = 'wedge', options = {}) ⇒ Object

Return the opal javascript.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/wedge.rb', line 141

def javascript(path_name = 'wedge', options = {})
  path_name = path_name.to_s

  if server?
    javascript_cache[path_name] ||= begin
      js = options.delete(:js) || build(path_name, options).javascript

      if path_name == 'wedge'
        compiled_data = Base64.encode64 config.client_data.to_json
        # We need to merge in some data that is only set on the server.
        # i.e. path, assets_key etc....
        js << Opal.compile("Wedge.config.data = HashObject.new(Wedge.config.data.to_h.merge JSON.parse(Base64.decode64('#{compiled_data}')))")
        # load all global plugins into wedge
        config.plugins.each do |path|
          js << Wedge.javascript(path)
        end
      elsif comp_class = Wedge.config.component_class[path_name.gsub(/\//, '__')]
        comp_class.config.on_compile.each { |blk| comp_class.instance_eval(&blk) }
        comp_name     = comp_class.config.name
        compiled_data = Base64.encode64 comp_class.config.client_data.to_json

        js << Opal.compile("Wedge.config.component_class[:#{comp_name}].config.data = HashObject.new(Wedge.config.component_class[:#{comp_name}].config.data.to_h.merge JSON.parse(Base64.decode64('#{compiled_data}')))")

        load_requires path_name, js
      end

      js
    end
  else
    url   = "#{Wedge.assets_url_with_host}/#{options[:path]}.js"
    cache = options[:cache_assets]

    `jQuery.ajax({ url: url, dataType: "script", cache: cache }).done(function() {`
      # fix: at the moment to_js called from the server will set the class
      # store for whatever method it calls.  we need to think of a better idea
      # for global and local data store.
      Wedge.config.component_class[options[:name]].config.store = options[:store].indifferent

      if initialize_args = options.delete(:initialize_args)
        comp = Wedge[options[:name], *initialize_args]
      else
        comp = Wedge[options[:name]]
      end

      if options[:method_args].any?
        comp.send(options[:method_called], options[:method_args])
      else
        comp.send(options[:method_called])
      end

      Wedge.trigger_browser_events

     `}).fail(function(jqxhr, settings, exception){ window.console.log(exception); })`
  end
end

.load_requires(path_name, js) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/wedge.rb', line 197

def load_requires path_name, js
  if requires = config.requires[path_name.gsub(/\//, '__')]
    requires.each do |path|
      next unless comp_class = Wedge.config.component_class[path]

      comp_class.config.on_compile.each { |blk| comp_class.instance_eval(&blk) }

      comp_name     = comp_class.config.name
      compiled_data = Base64.encode64 comp_class.config.client_data.to_json

      load_requires path, js

      js << Opal.compile("Wedge.config.component_class[:#{comp_name}].config.data = HashObject.new(Wedge.config.component_class[:#{comp_name}].config.data.to_h.merge JSON.parse(Base64.decode64('#{compiled_data}')))")
    end
  end
end

.script_tagObject



56
57
58
# File 'lib/wedge.rb', line 56

def script_tag
  "<script src='#{assets_url}/wedge.js'></script>"
end

.trigger(wedge_name, event_name, *args) ⇒ Object



77
78
79
# File 'lib/wedge.rb', line 77

def trigger(wedge_name, event_name, *args)
  events.trigger wedge_name, event_name, *args
end

Instance Method Details

#append_pathsArray

Append the correct paths to opal.

Returns:

  • (Array)

    List of opal paths.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/wedge.rb', line 124

def append_paths
  @append_paths ||= begin
    file     = method(:assets_url).source_location.first.sub('/wedge.rb', '')
    gems_dir = ::Opal.gem_dir.gsub(/(?<=gems)\/opal-.*/, '')
    Wedge::Opal.append_path file
    Wedge::Opal.append_path Dir.pwd
    # fix: make this a config option i.e.
    # gems: [:ability_list]
    # then grab that path and add it to the opal path list
    Dir["#{gems_dir}/**/"].sort.each do |folder|
      Wedge::Opal.append_path "#{folder}/lib"
    end
  end
end

#build(path = 'wedge', options = {}) ⇒ String, Opal::Builder#build

Returns the build object for opal.

Parameters:

  • path (String) (defaults to: 'wedge')

    require path to file to build.

Returns:



112
113
114
# File 'lib/wedge.rb', line 112

def build(path = 'wedge', options = {})
  Opal::Builder.build(path, options) if append_paths
end

#javascript_cacheObject



61
62
63
# File 'lib/wedge.rb', line 61

def javascript_cache
  @javascript_cache ||= IndifferentHash.new
end

#source_map(path = 'wedge', options = {}) ⇒ Object

Source maps for the javascript



117
118
119
# File 'lib/wedge.rb', line 117

def source_map(path = 'wedge', options = {})
  build(path, options).source_map
end

#trigger_browser_eventsObject



67
68
69
70
71
72
73
74
# File 'lib/wedge.rb', line 67

def trigger_browser_events
  config.component_class.each do |k, klass|
    next if klass.config.triggered_browser_events
    klass.config.triggered_browser_events = true

    Wedge.trigger klass.config.name, :browser_events
  end
end