browserio 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,106 @@
1
+ class Object
2
+ # Invokes the public method whose name goes as first argument just like
3
+ # +public_send+ does, except that if the receiver does not respond to it the
4
+ # call returns +nil+ rather than raising an exception.
5
+ #
6
+ # This method is defined to be able to write
7
+ #
8
+ # @person.try(:name)
9
+ #
10
+ # instead of
11
+ #
12
+ # @person.name if @person
13
+ #
14
+ # +try+ calls can be chained:
15
+ #
16
+ # @person.try(:spouse).try(:name)
17
+ #
18
+ # instead of
19
+ #
20
+ # @person.spouse.name if @person && @person.spouse
21
+ #
22
+ # +try+ will also return +nil+ if the receiver does not respond to the method:
23
+ #
24
+ # @person.try(:non_existing_method) # => nil
25
+ #
26
+ # instead of
27
+ #
28
+ # @person.non_existing_method if @person.respond_to?(:non_existing_method) # => nil
29
+ #
30
+ # +try+ returns +nil+ when called on +nil+ regardless of whether it responds
31
+ # to the method:
32
+ #
33
+ # nil.try(:to_i) # => nil, rather than 0
34
+ #
35
+ # Arguments and blocks are forwarded to the method if invoked:
36
+ #
37
+ # @posts.try(:each_slice, 2) do |a, b|
38
+ # ...
39
+ # end
40
+ #
41
+ # The number of arguments in the signature must match. If the object responds
42
+ # to the method the call is attempted and +ArgumentError+ is still raised
43
+ # in case of argument mismatch.
44
+ #
45
+ # If +try+ is called without arguments it yields the receiver to a given
46
+ # block unless it is +nil+:
47
+ #
48
+ # @person.try do |p|
49
+ # ...
50
+ # end
51
+ #
52
+ # You can also call try with a block without accepting an argument, and the block
53
+ # will be instance_eval'ed instead:
54
+ #
55
+ # @person.try { upcase.truncate(50) }
56
+ #
57
+ # Please also note that +try+ is defined on +Object+. Therefore, it won't work
58
+ # with instances of classes that do not have +Object+ among their ancestors,
59
+ # like direct subclasses of +BasicObject+. For example, using +try+ with
60
+ # +SimpleDelegator+ will delegate +try+ to the target instead of calling it on
61
+ # the delegator itself.
62
+ def try(*a, &b)
63
+ try!(*a, &b) if a.empty? || respond_to?(a.first)
64
+ end
65
+
66
+ # Same as #try, but raises a NoMethodError exception if the receiver is
67
+ # not +nil+ and does not implement the tried method.
68
+ #
69
+ # "a".try!(:upcase) # => "A"
70
+ # nil.try!(:upcase) # => nil
71
+ # 123.try!(:upcase) # => NoMethodError: undefined method `upcase' for 123:Fixnum
72
+ def try!(*a, &b)
73
+ if a.empty? && block_given?
74
+ if b.arity.zero?
75
+ instance_eval(&b)
76
+ else
77
+ yield self
78
+ end
79
+ else
80
+ public_send(*a, &b)
81
+ end
82
+ end
83
+ end
84
+
85
+ class NilClass
86
+ # Calling +try+ on +nil+ always returns +nil+.
87
+ # It becomes especially helpful when navigating through associations that may return +nil+.
88
+ #
89
+ # nil.try(:name) # => nil
90
+ #
91
+ # Without +try+
92
+ # @person && @person.children.any? && @person.children.first.name
93
+ #
94
+ # With +try+
95
+ # @person.try(:children).try(:first).try(:name)
96
+ def try(*args)
97
+ nil
98
+ end
99
+
100
+ # Calling +try!+ on +nil+ always returns +nil+.
101
+ #
102
+ # nil.try!(:name) # => nil
103
+ def try!(*args)
104
+ nil
105
+ end
106
+ end
@@ -1,3 +1,3 @@
1
1
  module BrowserIO
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,63 @@
1
+ class Roda
2
+ module RodaPlugins
3
+ module BrowserIO
4
+ def self.configure(app, opts = {})
5
+ if app.opts[:browserio]
6
+ app.opts[:browserio].merge!(opts)
7
+ else
8
+ app.opts[:browserio] = opts.dup
9
+ end
10
+
11
+ opts = app.opts[:browserio]
12
+
13
+ opts.each do |k, v|
14
+ case k.to_s
15
+ when 'plugins'
16
+ v.each { |p| ::BrowserIO.config.plugin p }
17
+ when 'scope'
18
+ ::BrowserIO.config.scope v.new('')
19
+ else
20
+ ::BrowserIO.config.send(k, v)
21
+ end
22
+ end
23
+ end
24
+
25
+ module InstanceMethods
26
+ def bio(*args)
27
+ args << { scope: self }
28
+ ::BrowserIO[*args]
29
+ end
30
+ alias_method :comp, :bio unless defined? comp
31
+ alias_method :component, :bio unless defined? component
32
+ end
33
+
34
+ module RequestClassMethods
35
+ def bio_route_regex
36
+ %r{#{roda_class.opts[:browserio][:assets_url]}/(.*)\.(.*)$}
37
+ end
38
+ end
39
+
40
+ module RequestMethods
41
+ def browserio
42
+ on self.class.bio_route_regex do |component, ext|
43
+ case ext
44
+ when 'map'
45
+ ::BrowserIO.source_map component
46
+ when 'rb'
47
+ if component =~ /^browserio/
48
+ path = ::BrowserIO.opts.file_path.gsub(/\/browserio.rb$/, '')
49
+ File.read("#{path}/#{component}.rb")
50
+ else
51
+ File.read("#{ROOT_PATH}/#{component}.rb")
52
+ end
53
+ else
54
+ "#{::BrowserIO.javascript(component)}\n//# sourceMappingURL=/assets/bio/#{component}.map"
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ register_plugin(:browserio, BrowserIO)
62
+ end
63
+ end
@@ -0,0 +1,34 @@
1
+ require 'browserio'
2
+ require 'roda'
3
+
4
+ require 'pry'
5
+ require 'awesome_print'
6
+
7
+ ROOT_PATH = File.dirname(__FILE__)
8
+
9
+ class DummyApp < Roda
10
+ plugin :assets, {
11
+ group_subdirs: false,
12
+ path: ROOT_PATH,
13
+ css_dir: '',
14
+ js_dir: '',
15
+ js: [ 'bio/browserio.js' ]
16
+ }
17
+
18
+ plugin :browserio, {
19
+ scope: self,
20
+ assets_url: 'assets/bio',
21
+ plugins: [:form]
22
+ }
23
+
24
+ route do |r|
25
+ r.browserio
26
+
27
+ r.root do
28
+ bio(:root, :js).display
29
+ end
30
+ end
31
+ end
32
+
33
+ Dir["#{ROOT_PATH}/forms/*.rb"].sort.each { |file| require file }
34
+ Dir["#{ROOT_PATH}/components/*.rb"].sort.each { |file| require file }
@@ -0,0 +1,16 @@
1
+ require_relative 'root' unless RUBY_ENGINE == 'opal'
2
+
3
+ class DummyApp
4
+ class BarComponent < BrowserIO::Component
5
+ config.name :bar
6
+ config.requires :root
7
+
8
+ def moo
9
+ 'cow'
10
+ end
11
+
12
+ on :clicked_foo, for: :root do
13
+ dom.find('body').append moo
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,39 @@
1
+ class DummyApp
2
+ class RootComponent < BrowserIO::Component
3
+ config.name :root
4
+ config.html <<-HTML
5
+ <!DOCTYPE html>
6
+ <html>
7
+ <head>
8
+ <script src="//code.jquery.com/jquery-1.11.2.js"></script>
9
+ </head>
10
+ <body>
11
+ <div id='foo'>bar</div>
12
+ </body>
13
+ </html>
14
+ HTML
15
+ config.dom do
16
+ dom.find('body') << assets(:js)
17
+ end
18
+ config.requires :bar, :foo_form
19
+
20
+ def display
21
+ if server?
22
+ dom
23
+ else
24
+ el = Element['<div>']
25
+ el.html 'foo'
26
+ dom.find('#foo').before el
27
+ end
28
+ end
29
+
30
+ on :ready do
31
+ puts 'dom ready'
32
+ end
33
+
34
+ on :click, '#foo' do |el|
35
+ el.after '<div>bar</div>'
36
+ trigger :clicked_foo
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,6 @@
1
+ require 'bundler'
2
+ Bundler.setup :default, ENV.fetch('RACK_ENV') { 'development' }
3
+
4
+ require_relative 'app'
5
+
6
+ run DummyApp
@@ -0,0 +1,6 @@
1
+ class DummyApp
2
+ class FooForm < BrowserIO::Form
3
+ config.name :foo_form
4
+ config.requires :pjax_plugin
5
+ end
6
+ end
@@ -0,0 +1,59 @@
1
+ var page = require('webpage').create();
2
+ page.settings.localToRemoteUrlAccessEnabled = true;
3
+ page.settings.resourceTimeout = 1000;
4
+ // page.content = "<!doctype html>\n<html>\n<head>\new<script type=\"text/javascript\" src=\"https://code.jquery.com/jquery-1.11.2.min.js\"></script>\n</head>\n<body>\n<div id=\"foo\">bar<div>\n</body>\n</html>";
5
+ var content = '<!doctype html>';
6
+ content += '<html><head>';
7
+ content += '<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.2.min.js"></script>';
8
+ content += '</head><body>';
9
+ content += '<h1 id="foo">bar</h1>';
10
+ content += '</body></html>';
11
+
12
+ // page.content = "<div id='foo'>bar</div>";
13
+
14
+ page.onConsoleMessage = function(msg) {
15
+ console.log(msg);
16
+ };
17
+
18
+ page.onResourceTimeout = function(a) {
19
+ phantom.exit(1);
20
+ };
21
+
22
+ page.onError = function(msg, trace) {
23
+
24
+ var msgStack = ['ERROR: ' + msg];
25
+
26
+ if (trace && trace.length) {
27
+ msgStack.push('TRACE:');
28
+ trace.forEach(function(t) {
29
+ msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
30
+ });
31
+ }
32
+
33
+ console.log(msgStack.join('\n'));
34
+ phantom.exit();
35
+ };
36
+
37
+ phantom.onError = function(msg, trace) {
38
+ var msgStack = ['PHANTOM ERROR: ' + msg];
39
+ if (trace && trace.length) {
40
+ msgStack.push('TRACE:');
41
+ trace.forEach(function(t) {
42
+ msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
43
+ });
44
+ }
45
+ console.log(msgStack.join('\n'));
46
+ phantom.exit();
47
+ };
48
+
49
+ page.content = content
50
+
51
+ page.onLoadFinished = function() {
52
+ page.evaluate(function() {
53
+ console.log($('#foo').html());
54
+ });
55
+ phantom.exit();
56
+ };
57
+
58
+ // page.includeJs("http://code.jquery.com/jquery-1.11.2.min.js", function(){
59
+ // });
@@ -0,0 +1,34 @@
1
+ require_relative 'test_helper'
2
+
3
+ class BasicComponent < BrowserIO::Component
4
+ config.name :basic
5
+ config.html <<-HTML
6
+ <!DOCTYPE html>
7
+ <html>
8
+ <body>
9
+ <div id='foo'>bar</div>
10
+ </body>
11
+ </html>
12
+ HTML
13
+ config.dom do
14
+ tmpl :foo, dom.find('#foo')
15
+ end
16
+
17
+ def foo
18
+ 'bar'
19
+ end
20
+ end
21
+
22
+ class TestComponent < Minitest::Test
23
+ def test_calling_basic_component
24
+ assert_equal 'bar', bio(:basic).foo
25
+ end
26
+
27
+ def test_parsing_html
28
+ assert_equal '<div id="foo">bar</div>', bio(:basic).tmpl(:foo).to_html
29
+ end
30
+
31
+ def test_returning_js
32
+ assert bio(:basic, :js).foo[/Opal/]
33
+ end
34
+ end
@@ -0,0 +1,13 @@
1
+ require 'phantomjs'
2
+ require_relative 'test_helper'
3
+
4
+ class TestBrowserIO < Minitest::Test
5
+ def test_javascript_and_source_maps
6
+ assert BrowserIO.javascript[/Opal/]
7
+ assert BrowserIO.source_map[/mappings/]
8
+ end
9
+
10
+ # def test_moo
11
+ # Phantomjs.run('./test/test.js')
12
+ # end
13
+ end
@@ -0,0 +1,38 @@
1
+ require 'bundler'
2
+ Bundler.setup :default, ENV.fetch('RACK_ENV') { 'development' }
3
+
4
+ require 'minitest/autorun'
5
+ require 'minitest/reporters'
6
+ require 'browserio'
7
+
8
+ require 'pry'
9
+ require 'awesome_print'
10
+
11
+ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # spec-like progress
12
+
13
+ module Minitest
14
+ class Test
15
+ extend Minitest::Spec::DSL
16
+
17
+ def session
18
+ @_session ||= OpenStruct.new
19
+ end
20
+
21
+ def bio(*args)
22
+ BrowserIO[*args]
23
+ end
24
+
25
+ # def app(*args)
26
+ # a = Class.new(PropertyLink).new
27
+ #
28
+ # a.instance_variable_set(:@_request, OpenStruct.new(
29
+ # session: session,
30
+ # env: {
31
+ # 'rack.session' => {}
32
+ # }
33
+ # ))
34
+ #
35
+ # a.component(*args)
36
+ # end
37
+ end
38
+ end
metadata CHANGED
@@ -1,23 +1,65 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: browserio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - cj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-22 00:00:00.000000000 Z
11
+ date: 2015-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: roda
14
+ name: opal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.7.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.7.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: opal-jquery
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.3.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.6.6.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.6.6.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
15
57
  requirement: !ruby/object:Gem::Requirement
16
58
  requirements:
17
59
  - - ">="
18
60
  - !ruby/object:Gem::Version
19
61
  version: '0'
20
- type: :runtime
62
+ type: :development
21
63
  prerelease: false
22
64
  version_requirements: !ruby/object:Gem::Requirement
23
65
  requirements:
@@ -25,34 +67,90 @@ dependencies:
25
67
  - !ruby/object:Gem::Version
26
68
  version: '0'
27
69
  - !ruby/object:Gem::Dependency
28
- name: bundler
70
+ name: awesome_print
29
71
  requirement: !ruby/object:Gem::Requirement
30
72
  requirements:
31
- - - "~>"
73
+ - - ">="
32
74
  - !ruby/object:Gem::Version
33
- version: '1.7'
75
+ version: '0'
34
76
  type: :development
35
77
  prerelease: false
36
78
  version_requirements: !ruby/object:Gem::Requirement
37
79
  requirements:
38
- - - "~>"
80
+ - - ">="
39
81
  - !ruby/object:Gem::Version
40
- version: '1.7'
82
+ version: '0'
41
83
  - !ruby/object:Gem::Dependency
42
- name: rake
84
+ name: yard
43
85
  requirement: !ruby/object:Gem::Requirement
44
86
  requirements:
45
- - - "~>"
87
+ - - ">="
46
88
  - !ruby/object:Gem::Version
47
- version: '10.0'
89
+ version: '0'
48
90
  type: :development
49
91
  prerelease: false
50
92
  version_requirements: !ruby/object:Gem::Requirement
51
93
  requirements:
52
- - - "~>"
94
+ - - ">="
53
95
  - !ruby/object:Gem::Version
54
- version: '10.0'
55
- description: ''
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: phantomjs.rb
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: minitest-line
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: minitest-reporters
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: roda
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Components for the Browser and Server
56
154
  email:
57
155
  - cjlazell@gmail.com
58
156
  executables: []
@@ -66,7 +164,34 @@ files:
66
164
  - Rakefile
67
165
  - browserio.gemspec
68
166
  - lib/browserio.rb
167
+ - lib/browserio/component.rb
168
+ - lib/browserio/config.rb
169
+ - lib/browserio/dom.rb
170
+ - lib/browserio/events.rb
171
+ - lib/browserio/html.rb
172
+ - lib/browserio/opal.rb
173
+ - lib/browserio/plugins/form.rb
174
+ - lib/browserio/plugins/history.rb
175
+ - lib/browserio/plugins/location.rb
176
+ - lib/browserio/plugins/pjax.rb
177
+ - lib/browserio/plugins/validations.rb
178
+ - lib/browserio/utilis/blank.rb
179
+ - lib/browserio/utilis/hash.rb
180
+ - lib/browserio/utilis/indifferent_hash.rb
181
+ - lib/browserio/utilis/methods.rb
182
+ - lib/browserio/utilis/titleize.rb
183
+ - lib/browserio/utilis/try.rb
69
184
  - lib/browserio/version.rb
185
+ - lib/roda/plugins/browserio.rb
186
+ - test/dummy/app.rb
187
+ - test/dummy/components/bar.rb
188
+ - test/dummy/components/root.rb
189
+ - test/dummy/config.ru
190
+ - test/dummy/forms/foo.rb
191
+ - test/test.js
192
+ - test/test_basic_component.rb
193
+ - test/test_browserio.rb
194
+ - test/test_helper.rb
70
195
  homepage: ''
71
196
  licenses:
72
197
  - MIT
@@ -90,5 +215,15 @@ rubyforge_project:
90
215
  rubygems_version: 2.2.2
91
216
  signing_key:
92
217
  specification_version: 4
93
- summary: ''
94
- test_files: []
218
+ summary: Components for the Browser and Server
219
+ test_files:
220
+ - test/dummy/app.rb
221
+ - test/dummy/components/bar.rb
222
+ - test/dummy/components/root.rb
223
+ - test/dummy/config.ru
224
+ - test/dummy/forms/foo.rb
225
+ - test/test.js
226
+ - test/test_basic_component.rb
227
+ - test/test_browserio.rb
228
+ - test/test_helper.rb
229
+ has_rdoc: