wedge 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef2f58249fc92d7275ded58a7adf02cb4c902cc7
4
- data.tar.gz: 9ca0bf1765457342dc330f60e65fb2ae4f4fa388
3
+ metadata.gz: a0bb0fe7c7495477be902553128393527696afe1
4
+ data.tar.gz: 11a43824c93d96973043520a5429e73bc89f94df
5
5
  SHA512:
6
- metadata.gz: 6c00389efaee2f6a672c1631c6c9b5ed198a1236f4b8aa08e2b8b1a46952701b376d71561263a7a0027a9de68095bfb8e8bbfa8b9060c67eabd61c643e4ffde5
7
- data.tar.gz: 1de60359395c01ef233f3c6e23cfcfc0aa264692d7bcc0ad4ada476c30004a2f29a5f437c35d22bc926226e75feab7d47e2015ba239fac67b2c308b74c1ad4f7
6
+ metadata.gz: 394f8a1cef109e32f546ea7d9166a67307f11ed6701739ce4feab23b9e7d6033397e0ef90b0e13b57a5311dd2e996ef6189452110f517c092eab5621924b007b
7
+ data.tar.gz: 49b95ed36fe62f5ab1fe7234e4bac2066d6f027c9dbea69082df961f091e2f5c5d50152fa5f5f4e419f4b05106cadf076078331bfa29d8f31d0fbd5ed8813eeb
@@ -76,7 +76,10 @@ class Roda
76
76
  method_args = [data]
77
77
  res = scope.wedge(name).send(method_called, *method_args) || ''
78
78
  else
79
- res = scope.wedge(name, data).send(method_called, *method_args) || ''
79
+ # This used to send things like init, we need a better way to
80
+ # send client config data to the server
81
+ # res = scope.wedge(name, data).send(method_called, *method_args) || ''
82
+ res = scope.wedge(name).send(method_called, *method_args) || ''
80
83
  end
81
84
 
82
85
  scope.response.headers["WEDGE-CSRF-TOKEN"] = scope.csrf_token if scope.methods.include? :csrf_token
data/lib/wedge.rb CHANGED
@@ -11,6 +11,7 @@ require 'base64'
11
11
  unless RUBY_ENGINE == 'opal'
12
12
  require 'nokogiri'
13
13
  require 'wedge/utilis/nokogiri'
14
+ require 'wedge/middleware'
14
15
  end
15
16
  require 'wedge/html'
16
17
  require 'wedge/dom'
@@ -31,6 +32,19 @@ class Wedge
31
32
  "#{url}#{config.cache_assets ? "/#{config.assets_key}" : ''}"
32
33
  end
33
34
 
35
+ def requires
36
+ @requires ||= IndifferentHash.new
37
+ end
38
+
39
+ def assets_url_regex
40
+ @assets_url_regex ||= begin
41
+ assets_url = ::Wedge.assets_url.gsub(%r{^\/}, '')
42
+ # # We also allow for no assets key so when we post server methods there
43
+ # # isn't an error if the key has been changed since a browser refresh.
44
+ %r{(?:#{assets_url}|#{assets_url.sub("#{::Wedge.config.assets_key}/", '')})/(.*)\.(.*)$}
45
+ end
46
+ end
47
+
34
48
  def assets_url_with_host
35
49
  "#{config.assets_url}#{config.cache_assets ? "/#{config.assets_key}" : ''}"
36
50
  end
@@ -126,11 +140,28 @@ class Wedge
126
140
  compiled_data = Base64.encode64 config.client_data.to_json
127
141
  # We need to merge in some data that is only set on the server.
128
142
  # i.e. path, assets_key etc....
129
- js << Opal.compile("Wedge.config.data = HashObject.new(JSON.parse(Base64.decode64('#{compiled_data}')).merge Wedge.config.data.to_h)")
143
+ js << Opal.compile("Wedge.config.data = HashObject.new(Wedge.config.data.to_h.merge JSON.parse(Base64.decode64('#{compiled_data}')))")
130
144
  # load all global plugins into wedge
131
145
  config.plugins.each do |path|
132
146
  js << Wedge.javascript(path)
133
147
  end
148
+ elsif comp_class = Wedge.config.component_class[path_name.gsub(/\//, '__')]
149
+ comp_name = comp_class.config.name
150
+ compiled_data = Base64.encode64 comp_class.config.client_data.to_json
151
+
152
+ 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}')))")
153
+
154
+ if requires = config.requires[path_name.gsub(/\//, '__')]
155
+
156
+ requires.each do |path|
157
+ next unless comp_class = Wedge.config.component_class[path]
158
+
159
+ comp_name = comp_class.config.name
160
+ compiled_data = Base64.encode64 comp_class.config.client_data.to_json
161
+
162
+ 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}')))")
163
+ end
164
+ end
134
165
  end
135
166
 
136
167
  js
@@ -159,7 +190,7 @@ class Wedge
159
190
  args = { klass: self, component_class: IndifferentHash.new }
160
191
 
161
192
  unless RUBY_ENGINE == 'opal'
162
- args[:path] = caller.first.gsub(/(?<=\.rb):.*/, '')
193
+ # args[:path] = caller.first.gsub(/(?<=\.rb):.*/, '')
163
194
  args[:assets_key] = begin
164
195
  if defined?(PlatformAPI) && ENV['HEROKU_TOKEN'] && ENV['HEROKU_APP']
165
196
  heroku = PlatformAPI.connect_oauth(ENV['HEROKU_TOKEN'], default_headers: {'Range' => 'version ..; order=desc'})
@@ -1,3 +1,5 @@
1
+ require 'wedge/require'
2
+
1
3
  class Wedge
2
4
  class Component
3
5
  include Methods
@@ -94,8 +96,10 @@ class Wedge
94
96
  def wedge_dom &block
95
97
  @wedge_dom ||= DOM.new wedge_config.html
96
98
 
97
- if block_given?
98
- yield
99
+ unless RUBY_ENGINE == 'opal'
100
+ if block_given?
101
+ yield
102
+ end
99
103
  end
100
104
 
101
105
  @wedge_dom
@@ -166,7 +170,7 @@ class Wedge
166
170
  # we want to remove the assets key from the call so we don't get
167
171
  # an error if they assets_key has changed and the user hasn't
168
172
  # refreshed the browser yet.
169
- call_url = "#{Wedge.assets_url.sub("#{Wedge.config.assets_key}/")}/#{path_name}.call"
173
+ call_url = "#{Wedge.assets_url.sub("#{Wedge.config.assets_key}/",'')}/#{path_name}.call"
170
174
 
171
175
  HTTP.post(call_url,
172
176
  headers: {
data/lib/wedge/config.rb CHANGED
@@ -21,6 +21,7 @@ class Wedge
21
21
  assets_key: false,
22
22
  cache_assets: false,
23
23
  is_plugin: false,
24
+ requires: IndifferentHash.new,
24
25
  triggered_browser_events: false,
25
26
  store: IndifferentHash.new,
26
27
  settings: IndifferentHash.new,
data/lib/wedge/events.rb CHANGED
@@ -46,7 +46,8 @@ class Wedge
46
46
  if for_component = event[:options].delete(:for)
47
47
  events = @events[for_component] ||= IndifferentHash.new({
48
48
  browser_events: [],
49
- object_events: IndifferentHash.new
49
+ object_events: IndifferentHash.new,
50
+ on_count: 0
50
51
  })
51
52
  end
52
53
 
@@ -0,0 +1,104 @@
1
+ class Wedge
2
+ class Middleware
3
+ def initialize(app, settings = {})
4
+ @app = app
5
+
6
+ # Add settings to wedge
7
+ settings.each do |k, v|
8
+ Wedge.config.send "#{k}=", v
9
+ end
10
+ end
11
+
12
+ def call(env)
13
+ responder = Responder.new(@app, env)
14
+ responder.respond
15
+ end
16
+
17
+ class Responder
18
+ attr_accessor :app, :env, :wedge_path, :extension
19
+
20
+ def initialize(app, env)
21
+ @app = app; @env = env
22
+ end
23
+
24
+ def respond
25
+ if path =~ Wedge.assets_url_regex
26
+ @wedge_path, @extension = $1, $2
27
+ body, headers, status = [], {}, 200
28
+
29
+ case extension
30
+ when 'map'
31
+ ::Wedge.source_map wedge_path
32
+ when 'rb'
33
+ if wedge_path =~ /^wedge/
34
+ path = ::Wedge.config.path.gsub(/\/wedge.rb$/, '')
35
+ File.read("#{path}/#{wedge_path}.rb")
36
+ else
37
+ File.read("#{ROOT_PATH}/#{wedge_path}.rb")
38
+ end if Wedge.config.debug
39
+ when 'call'
40
+ body = scope.request.body.read
41
+ data = scope.request.params
42
+
43
+ begin
44
+ data.merge!(body ? JSON.parse(body) : {})
45
+ rescue
46
+ # can't be parsed by json
47
+ end
48
+
49
+ data = data.indifferent
50
+ name = data.delete(:wedge_name)
51
+ method_called = data.delete(:wedge_method_called)
52
+ method_args = data.delete(:wedge_method_args)
53
+
54
+ if method_args == 'wedge_data' && data
55
+ method_args = [data]
56
+ res = Wedge.scope!(app)[name].send(method_called, *method_args) || ''
57
+ else
58
+ res = Wedge.scope!(app)[name, data].send(method_called, *method_args) || ''
59
+ end
60
+
61
+ headers["WEDGE-CSRF-TOKEN"] = scope.csrf_token if scope.methods.include? :csrf_token
62
+
63
+ if res.is_a? Hash
64
+ headers["Content-Type"] = 'application/json; charset=UTF-8'
65
+ body = res.to_json
66
+ else
67
+ body = res.to_s
68
+ end
69
+ else
70
+ headers['Content-Type'] = 'application/javascript; charset=UTF-8'
71
+
72
+ if Wedge.config.debug
73
+ body << "#{Wedge.javascript(wedge_path)}\n//# sourceMappingURL=#{Wedge.assets_url}/#{wedge_path}.map"
74
+ else
75
+ binding.pry
76
+ body << Wedge.javascript(wedge_path)
77
+ end
78
+ end
79
+
80
+ [status, headers, body.join]
81
+ else
82
+ response.finish
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ def path
89
+ @env['PATH_INFO']
90
+ end
91
+
92
+ def request
93
+ @request ||= Rack::Request.new(@env)
94
+ end
95
+
96
+ def response
97
+ @response ||= begin
98
+ status, headers, body = @app.call(request.env)
99
+ Rack::Response.new(body, status, headers)
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,47 @@
1
+ unless RUBY_ENGINE == 'opal'
2
+ module Kernel
3
+ # make an alias of the original require
4
+ alias_method :wedge_original_require, :require
5
+
6
+ # rewrite require
7
+ def require(name)
8
+ return wedge_original_require(name) unless defined?(Wedge)
9
+
10
+ result = wedge_original_require name
11
+
12
+ if name[/\Awedge/] || name[Dir.pwd]
13
+ name = name.sub("#{Dir.pwd}/", '').gsub(/\.rb$/, '').gsub(/\//, '__')
14
+ caller_str = "#{caller[0]}".gsub(/(#{Dir.pwd}\/|.*(?=wedge))/, '').gsub(/:.+$/, '').gsub(/\.rb$/, '').gsub(/\//, '__')
15
+
16
+ if !caller_str['.'] && !(Wedge.config.requires[caller_str] ||= []).include?(name)
17
+ Wedge.config.requires[caller_str] << name
18
+ end
19
+ end
20
+
21
+ result
22
+ end
23
+
24
+ # make an alias of the original require
25
+ alias_method :wedge_original_require_relative, :require_relative
26
+
27
+ # rewrite require_relative
28
+ def require_relative(name)
29
+ return wedge_original_require_relative(name) unless defined?(Wedge)
30
+
31
+ caller_str = "#{caller[0]}".gsub(/:.+$/, '').gsub(/\.rb$/, '')
32
+ caller_path_name = caller_str.gsub(%r{(#{Dir.pwd}/|.*wedge)}, '').gsub(/:.+$/, '').gsub(/^\//, '')
33
+
34
+ path_name = caller_path_name.gsub(/(?<=\/)([^\/]*)$/, "#{name}")
35
+ path_name = File.expand_path(path_name).sub("#{Dir.pwd}/", '') if path_name['..']
36
+ path_name = path_name.gsub(/\//, '__')
37
+ file = caller_str.gsub(/(?<=\/)([^\/]*)$/, "#{name}")
38
+ caller_path_name = caller_path_name.gsub(/\//, '__')
39
+
40
+ if !caller_path_name['.'] && !(Wedge.config.requires[caller_path_name] ||= []).include?(path_name)
41
+ Wedge.config.requires[caller_path_name] << path_name
42
+ end
43
+
44
+ wedge_original_require file
45
+ end
46
+ end
47
+ end
data/lib/wedge/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Wedge
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -9,10 +9,12 @@ class DummyApp
9
9
  #{Wedge.script_tag}
10
10
  </head>
11
11
  <body>
12
+ <div id='template'>Template</div>
12
13
  </body>
13
14
  </html>
14
15
  HTML
15
16
  dom do
17
+ tmpl :template, dom.find('#template')
16
18
  dom.find('body') << assets(:js)
17
19
  end
18
20
 
@@ -1,3 +1,4 @@
1
+ require_relative 'layout'
1
2
  require_relative 'base'
2
3
  require_relative 'bar'
3
4
  require_relative '../forms/foo'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wedge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - cj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-17 00:00:00.000000000 Z
11
+ date: 2015-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opal
@@ -169,12 +169,14 @@ files:
169
169
  - lib/wedge/dom.rb
170
170
  - lib/wedge/events.rb
171
171
  - lib/wedge/html.rb
172
+ - lib/wedge/middleware.rb
172
173
  - lib/wedge/opal.rb
173
174
  - lib/wedge/plugins/form.rb
174
175
  - lib/wedge/plugins/history.rb
175
176
  - lib/wedge/plugins/location.rb
176
177
  - lib/wedge/plugins/pjax.rb
177
178
  - lib/wedge/plugins/validations.rb
179
+ - lib/wedge/require.rb
178
180
  - lib/wedge/utilis/blank.rb
179
181
  - lib/wedge/utilis/element.rb
180
182
  - lib/wedge/utilis/hash.rb
@@ -218,7 +220,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
218
220
  version: '0'
219
221
  requirements: []
220
222
  rubyforge_project:
221
- rubygems_version: 2.2.2
223
+ rubygems_version: 2.4.3
222
224
  signing_key:
223
225
  specification_version: 4
224
226
  summary: Components for the Browser and Server