to-javascript 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README +0 -0
- data/lib/to-javascript.rb +52 -0
- data/lib/to-javascript/code.rb +41 -0
- data/lib/to-javascript/encoders/date.rb +10 -0
- data/lib/to-javascript/encoders/date_time.rb +10 -0
- data/lib/to-javascript/encoders/enumerable.rb +12 -0
- data/lib/to-javascript/encoders/false_class.rb +5 -0
- data/lib/to-javascript/encoders/hash.rb +48 -0
- data/lib/to-javascript/encoders/nil_class.rb +5 -0
- data/lib/to-javascript/encoders/numeric.rb +5 -0
- data/lib/to-javascript/encoders/object.rb +8 -0
- data/lib/to-javascript/encoders/regexp.rb +5 -0
- data/lib/to-javascript/encoders/string.rb +22 -0
- data/lib/to-javascript/encoders/symbol.rb +5 -0
- data/lib/to-javascript/encoders/time.rb +10 -0
- data/lib/to-javascript/encoders/time_with_zone.rb +7 -0
- data/lib/to-javascript/encoders/true_class.rb +5 -0
- data/lib/to-javascript/encoding.rb +37 -0
- data/lib/to-javascript/serializer.rb +16 -0
- metadata +90 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 martin.rehfeld@glnetworks.de
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
File without changes
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# to-javascript was designed to allow you to write comples dynamic javascript
|
2
|
+
# using standard ruby object. Unlike <i>to_json<i> <i>to_js<i> creates code
|
3
|
+
# exicutable only in javascript. This means its useless as a transport medium and
|
4
|
+
# therefore is not trying to replace or improove upon json in anway.
|
5
|
+
#
|
6
|
+
# <i>to_js<i> is almost identical to to_json except certain object are converted
|
7
|
+
# into their code that create's their javascript equvalent rather then a string
|
8
|
+
# equvalent
|
9
|
+
#
|
10
|
+
# ==== Example:
|
11
|
+
# Date.new(2005,2,1).to_js
|
12
|
+
# # => "new Date(2005/02/01)"
|
13
|
+
#
|
14
|
+
#
|
15
|
+
# Complex ruby objects are just as easily be converted into valid javascript.
|
16
|
+
#
|
17
|
+
# ==== Example:
|
18
|
+
# {
|
19
|
+
# :desc => 'my dog at my homework',
|
20
|
+
# :dates => [
|
21
|
+
# Date.new(2005,2,8),
|
22
|
+
# Date.new(2005,5,4),
|
23
|
+
# Date.new(2005,7,10),
|
24
|
+
# Date.new(2005,8,9)
|
25
|
+
# ],
|
26
|
+
# :location => 'window.location'.as_js
|
27
|
+
# }.to_js
|
28
|
+
#
|
29
|
+
# # {
|
30
|
+
# # "desc": "my dog at my homework",
|
31
|
+
# # "dates": [
|
32
|
+
# # new Date('2005/02/08'),
|
33
|
+
# # new Date('2005/05/04'),
|
34
|
+
# # new Date('2005/07/10'),
|
35
|
+
# # new Date('2005/08/09')
|
36
|
+
# # ],
|
37
|
+
# # "location": window.location
|
38
|
+
# # }
|
39
|
+
#
|
40
|
+
# This is extremely helpful in projects that dynamically generate large
|
41
|
+
# amounts of javascript
|
42
|
+
#
|
43
|
+
require 'activesupport'
|
44
|
+
require 'activerecord'
|
45
|
+
require "to-javascript/encoding"
|
46
|
+
# require "to-javascript/javascript"
|
47
|
+
require "to-javascript/serializer"
|
48
|
+
|
49
|
+
# Alias to ActiveSupport::JS::Code.new
|
50
|
+
def JS(object)
|
51
|
+
ActiveSupport::JS::Code.new object
|
52
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# The Javascript object is just a String that when parced into
|
2
|
+
# Javascript code isnt wrapped in quotes.
|
3
|
+
#
|
4
|
+
# ==== Example:
|
5
|
+
# here = 'http://www.examples.com'
|
6
|
+
# {
|
7
|
+
# :isHere => Javascript("(window.location == #{here})"),
|
8
|
+
# }.to_js
|
9
|
+
#
|
10
|
+
# # {
|
11
|
+
# # "isHere": (window.location == http://www.examples.com)
|
12
|
+
# # }
|
13
|
+
module ActiveSupport
|
14
|
+
module JS
|
15
|
+
|
16
|
+
# A string that returns itself as Javascript code.
|
17
|
+
class Code < String
|
18
|
+
|
19
|
+
def initialize(object)
|
20
|
+
if object.is_a? String
|
21
|
+
super(object)
|
22
|
+
elsif object.respond_to? :to_js
|
23
|
+
super(object.to_js)
|
24
|
+
else
|
25
|
+
super(object)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Returns an unquoted string to be exectuted as Javacript code
|
30
|
+
#
|
31
|
+
# { :location => 'window.location'.as_js }.to_js
|
32
|
+
# # => {"location": window.location}
|
33
|
+
def to_js(options=nil)
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class Date
|
2
|
+
# Returns a Javascript string to create a Date object of the same date.
|
3
|
+
#
|
4
|
+
# ==== Example:
|
5
|
+
# Date.new(2005,2,1).to_js
|
6
|
+
# # => "new Date(2005/02/01)"
|
7
|
+
def to_js(options = nil)
|
8
|
+
ActiveSupport::JS::Code.new "new Date('#{strftime("%Y/%m/%d")}')"
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class DateTime
|
2
|
+
# Returns a Javascript string to create a Date object of the same datetime.
|
3
|
+
#
|
4
|
+
# ==== Example:
|
5
|
+
# DateTime.civil(2005,2,1,15,15,10).to_js
|
6
|
+
# # => "new Date(2005/02/01 15:15:10 +0000)"
|
7
|
+
def to_js(options = nil)
|
8
|
+
ActiveSupport::JS::Code.new "new Date('#{strftime("%Y/%m/%d %H:%M:%S %z")}')"
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Enumerable
|
2
|
+
# Returns a Javascript string representing the enumerable. Any +options+
|
3
|
+
# given will be passed on to its elements. For example:
|
4
|
+
#
|
5
|
+
# users = User.find(:all)
|
6
|
+
# # => users.to_js(:only => :name)
|
7
|
+
#
|
8
|
+
# will pass the <tt>:only => :name</tt> option to each user.
|
9
|
+
def to_js(options = {}) #:nodoc:
|
10
|
+
ActiveSupport::JS::Code.new "[#{map { |value| ActiveSupport::JS.encode(value, options) } * ', '}]"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
class Hash
|
2
|
+
# Returns a Javascript string representing the hash.
|
3
|
+
#
|
4
|
+
# Just like to_json, without any +options+, the returned javascript equivalent the obejct
|
5
|
+
# For example:
|
6
|
+
#
|
7
|
+
# { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_js
|
8
|
+
# # => {"name": "Konata Izumi", 1: 2, "age": 16}
|
9
|
+
#
|
10
|
+
# The keys in the javascript string are unordered due to the nature of hashes.
|
11
|
+
#
|
12
|
+
# Also like to_jason, the <tt>:only</tt> and <tt>:except</tt> options can be used to limit the
|
13
|
+
# attributes included, and will accept 1 or more hash keys to include/exclude.
|
14
|
+
#
|
15
|
+
# { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_js(:only => [:name, 'age'])
|
16
|
+
# # => {"name": "Konata Izumi", "age": 16}
|
17
|
+
#
|
18
|
+
# { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_js(:except => 1)
|
19
|
+
# # => {"name": "Konata Izumi", "age": 16}
|
20
|
+
#
|
21
|
+
# The +options+ also filter down to any hash values. This is particularly
|
22
|
+
# useful for converting hashes containing ActiveRecord objects or any object
|
23
|
+
# that responds to options in their <tt>to_js</tt> method. For example:
|
24
|
+
#
|
25
|
+
# users = User.find(:all)
|
26
|
+
# { :users => users, :count => users.size }.to_js(:include => :posts)
|
27
|
+
#
|
28
|
+
# would pass the <tt>:include => :posts</tt> option to <tt>users</tt>,
|
29
|
+
# allowing the posts association in the User model to be converted to javascript
|
30
|
+
# as well.
|
31
|
+
def to_js(options = {})
|
32
|
+
hash_keys = self.keys
|
33
|
+
|
34
|
+
if options[:except]
|
35
|
+
hash_keys = hash_keys - Array(options[:except])
|
36
|
+
elsif options[:only]
|
37
|
+
hash_keys = hash_keys & Array(options[:only])
|
38
|
+
end
|
39
|
+
|
40
|
+
returning result = ActiveSupport::JS::Code.new('{') do
|
41
|
+
result << hash_keys.map do |key|
|
42
|
+
"#{ActiveSupport::JS.encode(key)}: #{ActiveSupport::JS.encode(self[key], options)}"
|
43
|
+
end * ', '
|
44
|
+
result << '}'
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class String
|
2
|
+
def to_js(options = nil) #:nodoc:
|
3
|
+
ActiveSupport::JS::Code.new to_json(options)
|
4
|
+
end
|
5
|
+
|
6
|
+
# Synonymous with Javascript('string')
|
7
|
+
# Returns an unquoted Javascript object that will not be quoted when renderded as
|
8
|
+
# javasctipy
|
9
|
+
#
|
10
|
+
# datecode = 'new Date()'.to_javascript
|
11
|
+
# { :now => datecode }.to_js
|
12
|
+
# # => { "now": new Date() }
|
13
|
+
#
|
14
|
+
# { :location => 'window.location'.as_js }.to_js
|
15
|
+
# # => {"location": window.location}
|
16
|
+
def as_js
|
17
|
+
ActiveSupport::JS::Code.new self
|
18
|
+
end
|
19
|
+
# def to_javascript
|
20
|
+
# Javascript.new self
|
21
|
+
# end
|
22
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class Time
|
2
|
+
# Returns a Javascript string to create a Date object of the same time.
|
3
|
+
#
|
4
|
+
# ==== Example:
|
5
|
+
# Time.utc(2005,2,1,15,15,10).to_js
|
6
|
+
# # => "new Date(2005/02/01 15:15:10 +0000)"
|
7
|
+
def to_js(options = nil)
|
8
|
+
ActiveSupport::JS::Code.new "new Date('#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}')"
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "to-javascript/code"
|
2
|
+
require "to-javascript/encoders/object" # Require explicitly for rdoc.
|
3
|
+
Dir["#{File.dirname(__FILE__)}/encoders/**/*.rb"].each do |file|
|
4
|
+
basename = File.basename(file, '.rb')
|
5
|
+
unless basename == 'object'
|
6
|
+
require "#{file}"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module ActiveSupport
|
11
|
+
module JS
|
12
|
+
class CircularReferenceError < StandardError
|
13
|
+
end
|
14
|
+
|
15
|
+
class << self
|
16
|
+
REFERENCE_STACK_VARIABLE = :json_reference_stack #:nodoc:
|
17
|
+
|
18
|
+
# Converts a Ruby object into a Javascript string.
|
19
|
+
def encode(value, options = {})
|
20
|
+
raise_on_circular_reference(value) do
|
21
|
+
value.send(:to_js, options)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
def raise_on_circular_reference(value) #:nodoc:
|
27
|
+
stack = Thread.current[REFERENCE_STACK_VARIABLE] ||= []
|
28
|
+
raise CircularReferenceError, 'object references itself' if
|
29
|
+
stack.include? value
|
30
|
+
stack << value
|
31
|
+
yield
|
32
|
+
ensure
|
33
|
+
stack.pop
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module ActiveRecord #:nodoc:
|
2
|
+
module Serialization
|
3
|
+
|
4
|
+
# converts an object to executable javascript code (not json)
|
5
|
+
def to_js(options = {})
|
6
|
+
JavascriptSerializer.new(self, options).to_s
|
7
|
+
end
|
8
|
+
|
9
|
+
class JavascriptSerializer < ActiveRecord::Serialization::Serializer #:nodoc:
|
10
|
+
def serialize
|
11
|
+
serializable_record.to_js
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: to-javascript
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jared Grippe
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-09-02 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activesupport
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.1.0
|
23
|
+
version:
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: activerecord
|
26
|
+
version_requirement:
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: 2.1.0
|
32
|
+
version:
|
33
|
+
description: "Extends ActiveSupport adding: - to_js method to all objects that support to_json - Javascript class this is used to convert objects into executable javascript code with the goal of supporting easier javascript generation in ruby"
|
34
|
+
email: jared@deadlyicon.com
|
35
|
+
executables: []
|
36
|
+
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files:
|
40
|
+
- README
|
41
|
+
- MIT-LICENSE
|
42
|
+
files:
|
43
|
+
- lib/to-javascript/code.rb
|
44
|
+
- lib/to-javascript/encoders/date.rb
|
45
|
+
- lib/to-javascript/encoders/date_time.rb
|
46
|
+
- lib/to-javascript/encoders/enumerable.rb
|
47
|
+
- lib/to-javascript/encoders/false_class.rb
|
48
|
+
- lib/to-javascript/encoders/hash.rb
|
49
|
+
- lib/to-javascript/encoders/nil_class.rb
|
50
|
+
- lib/to-javascript/encoders/numeric.rb
|
51
|
+
- lib/to-javascript/encoders/object.rb
|
52
|
+
- lib/to-javascript/encoders/regexp.rb
|
53
|
+
- lib/to-javascript/encoders/string.rb
|
54
|
+
- lib/to-javascript/encoders/symbol.rb
|
55
|
+
- lib/to-javascript/encoders/time.rb
|
56
|
+
- lib/to-javascript/encoders/time_with_zone.rb
|
57
|
+
- lib/to-javascript/encoders/true_class.rb
|
58
|
+
- lib/to-javascript/encoding.rb
|
59
|
+
- lib/to-javascript/serializer.rb
|
60
|
+
- lib/to-javascript.rb
|
61
|
+
- README
|
62
|
+
- MIT-LICENSE
|
63
|
+
has_rdoc: true
|
64
|
+
homepage: http://tojavascript.rubyforge.org/
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: "0"
|
81
|
+
version:
|
82
|
+
requirements: []
|
83
|
+
|
84
|
+
rubyforge_project: toJavascript
|
85
|
+
rubygems_version: 1.1.1
|
86
|
+
signing_key:
|
87
|
+
specification_version: 2
|
88
|
+
summary: Converts objects into executable javascript code (not json)
|
89
|
+
test_files: []
|
90
|
+
|