ruby-jquery 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor/
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 1.9.2
6
+ - ruby-head
7
+ - jruby-19mode
8
+ - rbx-19mode
9
+ - jruby-head
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jquery.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Kentaro Kuribayashi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,61 @@
1
+ # ruby-jquery [![BuildStatus](https://secure.travis-ci.org/kentaro/ruby-jquery.png)](http://travis-ci.org/kentaro/ruby-jquery)
2
+
3
+ ruby-jquery is a jQuery expression generator.
4
+
5
+ ## Synopsis
6
+
7
+ ```ruby
8
+ require 'jquery'
9
+
10
+ jQuery() #=> jQuery()
11
+ jQuery('a') #=> jQuery("a")
12
+ jQuery(:document) #=> jQuery(document)
13
+ jQuery('a').text() #=> jQuery("a").text()
14
+ jQuery('a').text('aaa') #=> jQuery("a").text("aaa")
15
+ jQuery('a').foo(['a', 'b']) #=> jQuery("a").foo(["a","b"])
16
+ jQuery('a').foo({'a' => 'b'}) #=> jQuery("a").foo({"a":"b"})
17
+ jQuery('a').click(->(f) { f.e 'return true' }) #=> jQuery("a").click(function (e) { return true })
18
+
19
+ jQuery('#content').show().on('click', 'a', ->(f) { f.e 'return true' })
20
+ #=> jQuery("#content").show().on("click","a",function (e) { return true })
21
+ ```
22
+
23
+ ## With Selenium Driver
24
+
25
+ You can use this library with [Selenium Driver for Ruby](http://code.google.com/p/selenium/wiki/RubyBindings) like below:
26
+
27
+ ```ruby
28
+ driver.execute_script(
29
+ "return " << jQuery('#content a').attr('href')
30
+ )
31
+ ```
32
+
33
+ ## Installation
34
+
35
+ Add this line to your application's Gemfile:
36
+
37
+ gem 'ruby-jquery'
38
+
39
+ And then execute:
40
+
41
+ $ bundle
42
+
43
+ Or install it yourself as:
44
+
45
+ $ gem install ruby-jquery
46
+
47
+ ## TODO
48
+
49
+ * Support property access (e.g. `jQuery.ajax(...)` and `jQuery('a').length`). But can I do it with Ruby?
50
+
51
+ ## See Also
52
+
53
+ * [String-jQuery](https://github.com/motemen/String-jQuery)
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = FileList['spec/**/*_spec.rb']
7
+ end
8
+
9
+ task :default => :spec
@@ -0,0 +1,107 @@
1
+ require "json"
2
+ require "jquery/version"
3
+
4
+ module Kernel
5
+ def jQuery(*args)
6
+ JQuery::Object.new(:jQuery, *args)
7
+ end
8
+ end
9
+
10
+ module JQuery
11
+ class Object
12
+ attr_accessor :label, :args, :prev, :next
13
+
14
+ def initialize(label, *args)
15
+ @label = label.to_s
16
+ @args = args.map do |arg|
17
+ case arg
18
+ when String
19
+ JSString.new(arg)
20
+ when Numeric
21
+ JSNumeric.new(arg)
22
+ when Symbol
23
+ JSVar.new(arg)
24
+ when Array, Hash
25
+ JSStruct.new(arg)
26
+ when Proc
27
+ JSFunction.new(arg)
28
+ else
29
+ raise ArgumentError.new("#{arg.class} is not supported")
30
+ end
31
+ end
32
+ end
33
+
34
+ def to_s
35
+ current = self
36
+ chain = [current]
37
+
38
+ while current.prev
39
+ current = current.prev
40
+ chain.unshift(current)
41
+ end
42
+
43
+ result = []
44
+ chain.map do |obj|
45
+ expr = ''
46
+ expr << "#{obj.label}("
47
+ expr << obj.args.map { |arg| arg.to_s }.join(',')
48
+ expr << ')'
49
+ result << expr
50
+ end
51
+
52
+ result.join('.')
53
+ end
54
+
55
+ alias_method :to_str, :to_s
56
+
57
+ def method_missing(method, *args)
58
+ next_obj = self.class.new(method, *args)
59
+ self.next = next_obj
60
+ next_obj.prev = self
61
+ next_obj
62
+ end
63
+ end
64
+
65
+ class JSExpr
66
+ attr_accessor :expr
67
+
68
+ def initialize(expr)
69
+ @expr = expr
70
+ end
71
+ end
72
+
73
+ class JSString < JSExpr
74
+ def to_s
75
+ %Q|"#{expr}"|
76
+ end
77
+ end
78
+
79
+ class JSNumeric < JSExpr
80
+ def to_s
81
+ expr
82
+ end
83
+ end
84
+
85
+ class JSVar < JSExpr
86
+ def to_s
87
+ expr.to_s
88
+ end
89
+ end
90
+
91
+ class JSStruct < JSExpr
92
+ def to_s
93
+ JSON.dump(expr)
94
+ end
95
+ end
96
+
97
+ class JSFunction < JSExpr
98
+ def to_s
99
+ arg, expression = expr.call(self)
100
+ "function (#{arg}) { #{expression} }"
101
+ end
102
+
103
+ def method_missing(method, *args)
104
+ [method, *args]
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,3 @@
1
+ module JQuery
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jquery/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby-jquery"
8
+ spec.version = JQuery::VERSION
9
+ spec.authors = ["Kentaro Kuribayashi"]
10
+ spec.email = ["kentarok@gmail.com"]
11
+ spec.description = %q{jQuery Expression Generator}
12
+ spec.summary = %q{jQuery Expression Generator}
13
+ spec.homepage = "http://github.com/kentaro/ruby-jquery"
14
+ spec.license = "MIT"
15
+
16
+ spec.required_ruby_version = '>= 1.9.2'
17
+
18
+ spec.files = `git ls-files`.split($/)
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "rspec", "~> 2.12"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,240 @@
1
+ require_relative '../spec_helper'
2
+
3
+ module Kernel
4
+ describe '#jQuery' do
5
+ context 'when called with no argument' do
6
+ it {
7
+ expect(jQuery().to_s).to be == 'jQuery()'
8
+ }
9
+ end
10
+
11
+ context 'when called with a string argument' do
12
+ it {
13
+ expect(jQuery('a').to_s).to be == 'jQuery("a")'
14
+ }
15
+ end
16
+
17
+ context 'when called with a symbol argument' do
18
+ it {
19
+ expect(jQuery(:document).to_s).to be == 'jQuery(document)'
20
+ }
21
+ end
22
+
23
+ context 'when called with a integer argument' do
24
+ it {
25
+ expect(jQuery('a').text(1).to_s).to be == 'jQuery("a").text(1)'
26
+ }
27
+ end
28
+
29
+ context 'when called with a float argument' do
30
+ it {
31
+ expect(jQuery("a").text(0.1).to_s).to be == 'jQuery("a").text(0.1)'
32
+ }
33
+ end
34
+
35
+ context 'when called with an array argument' do
36
+ it {
37
+ expect(jQuery('a').foo(['a', 'b']).to_s).to be == 'jQuery("a").foo(["a","b"])'
38
+ }
39
+ end
40
+
41
+ context 'when called with a hash argument' do
42
+ it {
43
+ expect(jQuery('a').foo({'a' => 'b'}).to_s).to be == 'jQuery("a").foo({"a":"b"})'
44
+ }
45
+ end
46
+
47
+ context 'when called with a lambda' do
48
+ it {
49
+ expect(jQuery(->(f) { f.e 'return true' }).to_s).to be == 'jQuery(function (e) { return true })'
50
+ }
51
+ end
52
+
53
+ context 'when called with method chain and various arguments' do
54
+ it {
55
+ expect(jQuery('#content').show().on('click', 'a', ->(f) { f.e 'return true' }).to_s).to be ==
56
+ 'jQuery("#content").show().on("click","a",function (e) { return true })'
57
+ }
58
+ end
59
+ end
60
+ end
61
+
62
+ module JQuery
63
+ describe Object do
64
+ describe '.initialize' do
65
+ context 'when no argument passed' do
66
+ let(:obj) { JQuery::Object.new(:jQuery) }
67
+
68
+ it {
69
+ expect(obj).to be_an_instance_of(JQuery::Object)
70
+ expect(obj.args.size).to be == 0
71
+ }
72
+ end
73
+
74
+ context 'when an argument passed' do
75
+ context 'and it is a String object' do
76
+ let(:obj) { JQuery::Object.new(:jQuery, 'foo') }
77
+
78
+ it {
79
+ expect(obj).to be_an_instance_of(JQuery::Object)
80
+ expect(obj.args.size).to be == 1
81
+ expect(obj.args.first).to be_an_instance_of(JQuery::JSString)
82
+ }
83
+ end
84
+
85
+ context 'and it is a Integer object' do
86
+ let(:obj) { JQuery::Object.new(:jQuery, 1) }
87
+
88
+ it {
89
+ expect(obj).to be_an_instance_of(JQuery::Object)
90
+ expect(obj.args.size).to be == 1
91
+ expect(obj.args.first).to be_an_instance_of(JQuery::JSNumeric)
92
+ }
93
+ end
94
+
95
+ context 'and it is a Float object' do
96
+ let(:obj) { JQuery::Object.new(:jQuery, 0.1) }
97
+
98
+ it {
99
+ expect(obj).to be_an_instance_of(JQuery::Object)
100
+ expect(obj.args.size).to be == 1
101
+ expect(obj.args.first).to be_an_instance_of(JQuery::JSNumeric)
102
+ }
103
+ end
104
+
105
+ context 'and it is a Symbol object' do
106
+ let(:obj) { JQuery::Object.new(:jQuery, :document) }
107
+
108
+ it {
109
+ expect(obj).to be_an_instance_of(JQuery::Object)
110
+ expect(obj.args.size).to be == 1
111
+ expect(obj.args.first).to be_an_instance_of(JQuery::JSVar)
112
+ }
113
+ end
114
+
115
+ context 'and it is an Array object' do
116
+ let(:obj) { JQuery::Object.new(:jQuery, ['a', 'b']) }
117
+
118
+ it {
119
+ expect(obj).to be_an_instance_of(JQuery::Object)
120
+ expect(obj.args.size).to be == 1
121
+ expect(obj.args.first).to be_an_instance_of(JQuery::JSStruct)
122
+ }
123
+ end
124
+
125
+ context 'and it is a Hash object' do
126
+ let(:obj) { JQuery::Object.new(:jQuery, { 'a' => 'b' }) }
127
+
128
+ it {
129
+ expect(obj).to be_an_instance_of(JQuery::Object)
130
+ expect(obj.args.size).to be == 1
131
+ expect(obj.args.first).to be_an_instance_of(JQuery::JSStruct)
132
+ }
133
+ end
134
+
135
+ context 'and it is a Proc object' do
136
+ let(:obj) { JQuery::Object.new(:jQuery, ->() {}) }
137
+
138
+ it {
139
+ expect(obj).to be_an_instance_of(JQuery::Object)
140
+ expect(obj.args.size).to be == 1
141
+ expect(obj.args.first).to be_an_instance_of(JQuery::JSFunction)
142
+ }
143
+ end
144
+
145
+ context 'and it is an unsupported object' do
146
+ it {
147
+ expect {
148
+ JQuery::Object.new(:jQuery, Object.new)
149
+ }.to raise_error(ArgumentError)
150
+ }
151
+ end
152
+ end
153
+
154
+ context 'when multiple argument passed' do
155
+ let(:obj) { JQuery::Object.new(:jQuery, 'foo', 1) }
156
+
157
+ it {
158
+ expect(obj).to be_an_instance_of(JQuery::Object)
159
+ expect(obj.args.size).to be == 2
160
+ expect(obj.args[0]).to be_an_instance_of(JQuery::JSString)
161
+ expect(obj.args[1]).to be_an_instance_of(JQuery::JSNumeric)
162
+ }
163
+ end
164
+ end
165
+ end
166
+
167
+ describe JSString do
168
+ describe '#to_s' do
169
+ let(:obj) { JQuery::JSString.new('foo') }
170
+
171
+ it {
172
+ expect(obj.to_s).to be == %Q|"foo"|
173
+ }
174
+ end
175
+ end
176
+
177
+ describe JSNumeric do
178
+ context 'when expr is an Integer' do
179
+ describe '#to_s' do
180
+ let(:obj) { JQuery::JSNumeric.new(1) }
181
+
182
+ it {
183
+ expect(obj.to_s).to be == 1
184
+ }
185
+ end
186
+ end
187
+
188
+ context 'when expr is a Float' do
189
+ describe '#to_s' do
190
+ let(:obj) { JQuery::JSNumeric.new(0.1) }
191
+
192
+ it {
193
+ expect(obj.to_s).to be == 0.1
194
+ }
195
+ end
196
+ end
197
+ end
198
+
199
+ describe JSVar do
200
+ describe '#to_s' do
201
+ let(:obj) { JQuery::JSVar.new(:document) }
202
+
203
+ it {
204
+ expect(obj.to_s).to be == 'document'
205
+ }
206
+ end
207
+ end
208
+
209
+ describe JSStruct do
210
+ context 'when expr is an Array' do
211
+ describe '#to_s' do
212
+ let(:obj) { JQuery::JSStruct.new(['a', 'b']) }
213
+
214
+ it {
215
+ expect(obj.to_s).to be == %Q|["a","b"]|
216
+ }
217
+ end
218
+ end
219
+
220
+ context 'when expr is a Hash' do
221
+ describe '#to_s' do
222
+ let(:obj) { JQuery::JSStruct.new({ 'a' => 'b' }) }
223
+
224
+ it {
225
+ expect(obj.to_s).to be == %Q|{"a":"b"}|
226
+ }
227
+ end
228
+ end
229
+ end
230
+
231
+ describe JSFunction do
232
+ describe '#to_s' do
233
+ let(:obj) { JQuery::JSFunction.new(->(f) { f.e 'return true' }) }
234
+
235
+ it {
236
+ expect(obj.to_s).to be == 'function (e) { return true }'
237
+ }
238
+ end
239
+ end
240
+ end
@@ -0,0 +1,5 @@
1
+ $:.unshift File.expand_path("../../lib", __FILE__)
2
+ require 'jquery'
3
+
4
+ RSpec.configure do |config|
5
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-jquery
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kentaro Kuribayashi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.12'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.12'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: jQuery Expression Generator
47
+ email:
48
+ - kentarok@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .travis.yml
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/jquery.rb
60
+ - lib/jquery/version.rb
61
+ - ruby-jquery.gemspec
62
+ - spec/lib/jquery_spec.rb
63
+ - spec/spec_helper.rb
64
+ homepage: http://github.com/kentaro/ruby-jquery
65
+ licenses:
66
+ - MIT
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: 1.9.2
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.23
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: jQuery Expression Generator
89
+ test_files:
90
+ - spec/lib/jquery_spec.rb
91
+ - spec/spec_helper.rb