to_js 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3@to_js --create
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in to_js.gemspec
4
+ gemspec
data/MIT-LICENCE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2012 by Dmitriy Kiriyenko.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ [![Build Status](https://secure.travis-ci.org/dmitriy-kiriyenko/to_js.png)](http://travis-ci.org/dmitriy-kiriyenko/to_js)
2
+
3
+ Adds to_js to basic ruby types. Use when you need js, not json.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'bundler/gem_tasks'
5
+ require 'rspec/core/rake_task'
6
+
7
+ task :default => :spec
8
+
9
+ RSpec::Core::RakeTask.new(:spec) do |t|
10
+ end
@@ -0,0 +1,3 @@
1
+ module ToJs
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,36 @@
1
+ require 'visitor'
2
+
3
+ module ToJs
4
+ class Visitor < Visitor::Base
5
+ add_accept_method! :to_js
6
+
7
+ visitor_for String do |string|
8
+ if string =~ /`([^`]+)`/
9
+ $1
10
+ else
11
+ "\"#{string}\""
12
+ end
13
+ end
14
+
15
+ visitor_for Numeric do |number|
16
+ number.to_s
17
+ end
18
+
19
+ visitor_for NilClass do |_|
20
+ 'undefined'
21
+ end
22
+
23
+ visitor_for Enumerable do |array|
24
+ "[#{array.map {|o| visit o}.join(", ")}]"
25
+ end
26
+
27
+ visitor_for Hash do |hash|
28
+ "{#{hash.to_a.map{|key, value| "#{key}: #{visit value}"}.join(', ')}}"
29
+ end
30
+
31
+ visitor_for Object do |o|
32
+ o.to_s
33
+ end
34
+
35
+ end
36
+ end
data/lib/to_js.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "to_js/version"
2
+ require "to_js/visitor"
3
+
4
+ module ToJs
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ class A
4
+ def initialize a
5
+ @a = a
6
+ end
7
+
8
+ def to_s
9
+ @a
10
+ end
11
+ end
12
+
13
+ describe "Object#to_js" do
14
+ it "should return quoted string for string" do
15
+ "Hello".to_js.should == "\"Hello\""
16
+ end
17
+
18
+ it "should return string representation for fixnum" do
19
+ 5.to_js.should == "5"
20
+ end
21
+
22
+ it "should return string representation for float" do
23
+ 5.5.to_js.should == "5.5"
24
+ end
25
+
26
+ it "should return string representation for bigint" do
27
+ (1000**1000).to_js.should == (1000**1000).to_s
28
+ end
29
+
30
+ it "should return undefined for nil" do
31
+ nil.to_js.should == "undefined"
32
+ end
33
+
34
+ it "should return js array literal for array" do
35
+ [1, 2, "Hello"].to_js.should == "[1, 2, \"Hello\"]"
36
+ end
37
+
38
+ it "should return js array literal for set" do
39
+ Set.new([1, 2, "Hello"]).to_js.should == "[1, 2, \"Hello\"]"
40
+ end
41
+
42
+ it "should return js object literal for hash" do
43
+ {:a => "hello", :b => 5, "c" => "world"}.to_js.should == "{a: \"hello\", b: 5, c: \"world\"}"
44
+ end
45
+
46
+ it "should return to_s representation of object" do
47
+ A.new("Hello").to_js.should == "Hello"
48
+ end
49
+
50
+ it "should return direct to_s representation of symbol" do
51
+ :hello.to_js.should == "hello"
52
+ end
53
+
54
+ it "should return string without graves to string with graves" do
55
+ "`Hello`".to_js.should == "Hello"
56
+ end
57
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ Bundler.require(:default, :development)
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir[File.join(File.dirname(__FILE__), "support", "**", "*.rb")].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+ # some (optional) config here
12
+ end
File without changes
data/to_js.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "to_js/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "to_js"
7
+ s.version = ToJs::VERSION
8
+ s.authors = ["Dmitriy Kiriyenko, Maxim Tsaplin"]
9
+ s.email = ["dmitriy.kiriyenko@anahoret.com, maxim.tsaplin@anahoret.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Adds to_js to basic ruby types}
12
+ s.description = %q{Adds to_js to basic ruby types. Use when you need js, not json.}
13
+
14
+ s.rubyforge_project = "to_js"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "pry"
22
+ s.add_development_dependency "pry-doc"
23
+ s.add_development_dependency "rake"
24
+ s.add_development_dependency "rspec"
25
+
26
+ s.add_runtime_dependency "visitor"
27
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_js
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dmitriy Kiriyenko, Maxim Tsaplin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pry
16
+ requirement: &2157837720 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2157837720
25
+ - !ruby/object:Gem::Dependency
26
+ name: pry-doc
27
+ requirement: &2157837300 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2157837300
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &2157836860 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2157836860
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &2157836400 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2157836400
58
+ - !ruby/object:Gem::Dependency
59
+ name: visitor
60
+ requirement: &2157835960 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *2157835960
69
+ description: Adds to_js to basic ruby types. Use when you need js, not json.
70
+ email:
71
+ - dmitriy.kiriyenko@anahoret.com, maxim.tsaplin@anahoret.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - .rvmrc
79
+ - .travis.yml
80
+ - Gemfile
81
+ - MIT-LICENCE
82
+ - README.md
83
+ - Rakefile
84
+ - lib/to_js.rb
85
+ - lib/to_js/version.rb
86
+ - lib/to_js/visitor.rb
87
+ - spec/lib/to_js_spec.rb
88
+ - spec/spec_helper.rb
89
+ - spec/support/.gitkeep
90
+ - to_js.gemspec
91
+ homepage: ''
92
+ licenses: []
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ segments:
104
+ - 0
105
+ hash: -1987957576940724029
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ segments:
113
+ - 0
114
+ hash: -1987957576940724029
115
+ requirements: []
116
+ rubyforge_project: to_js
117
+ rubygems_version: 1.8.15
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: Adds to_js to basic ruby types
121
+ test_files:
122
+ - spec/lib/to_js_spec.rb
123
+ - spec/spec_helper.rb
124
+ - spec/support/.gitkeep
125
+ has_rdoc: