Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace 'johnson' with ExecJS #1

Merged
merged 6 commits into from Jul 23, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions Gemfile
@@ -1,5 +1,7 @@
source :rubygemss source :rubygems
source :gemcutter source :gemcutter
source "http://gems.github.com" source "http://gems.github.com"


gem "johnson" gem 'json'
gem 'therubyracer'
gem 'execjs'
13 changes: 11 additions & 2 deletions Gemfile.lock
@@ -1,11 +1,20 @@
GEM GEM
remote: http://rubygems.org/
remote: http://rubygems.org/ remote: http://rubygems.org/
remote: http://gems.github.com/ remote: http://gems.github.com/
specs: specs:
johnson (1.2.0) execjs (1.2.0)
multi_json (~> 1.0)
json (1.5.3)
libv8 (3.3.10.2)
multi_json (1.0.3)
therubyracer (0.9.2)
libv8 (~> 3.3.10)


PLATFORMS PLATFORMS
ruby ruby


DEPENDENCIES DEPENDENCIES
johnson execjs
json
therubyracer
7 changes: 6 additions & 1 deletion examples/rails3-example/Gemfile
Expand Up @@ -2,7 +2,12 @@ source 'http://rubygems.org'


gem 'rails', '>=3.0.0' gem 'rails', '>=3.0.0'


# Pick your JS runtime (see https://github.com/sstephenson/execjs)
gem 'therubyracer'
#gem 'johnson'

# gem 'isotope' # gem 'isotope'
gem 'json' gem 'json'
gem 'johnson' gem 'execjs'

gem 'sqlite3-ruby', :require => 'sqlite3' gem 'sqlite3-ruby', :require => 'sqlite3'
104 changes: 55 additions & 49 deletions examples/rails3-example/Gemfile.lock
Expand Up @@ -2,76 +2,82 @@ GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
specs: specs:
abstract (1.0.0) abstract (1.0.0)
actionmailer (3.0.0) actionmailer (3.0.7)
actionpack (= 3.0.0) actionpack (= 3.0.7)
mail (~> 2.2.5) mail (~> 2.2.15)
actionpack (3.0.0) actionpack (3.0.7)
activemodel (= 3.0.0) activemodel (= 3.0.7)
activesupport (= 3.0.0) activesupport (= 3.0.7)
builder (~> 2.1.2) builder (~> 2.1.2)
erubis (~> 2.6.6) erubis (~> 2.6.6)
i18n (~> 0.4.1) i18n (~> 0.5.0)
rack (~> 1.2.1) rack (~> 1.2.1)
rack-mount (~> 0.6.12) rack-mount (~> 0.6.14)
rack-test (~> 0.5.4) rack-test (~> 0.5.7)
tzinfo (~> 0.3.23) tzinfo (~> 0.3.23)
activemodel (3.0.0) activemodel (3.0.7)
activesupport (= 3.0.0) activesupport (= 3.0.7)
builder (~> 2.1.2) builder (~> 2.1.2)
i18n (~> 0.4.1) i18n (~> 0.5.0)
activerecord (3.0.0) activerecord (3.0.7)
activemodel (= 3.0.0) activemodel (= 3.0.7)
activesupport (= 3.0.0) activesupport (= 3.0.7)
arel (~> 1.0.0) arel (~> 2.0.2)
tzinfo (~> 0.3.23) tzinfo (~> 0.3.23)
activeresource (3.0.0) activeresource (3.0.7)
activemodel (= 3.0.0) activemodel (= 3.0.7)
activesupport (= 3.0.0) activesupport (= 3.0.7)
activesupport (3.0.0) activesupport (3.0.7)
arel (1.0.1) arel (2.0.10)
activesupport (~> 3.0.0)
builder (2.1.2) builder (2.1.2)
erubis (2.6.6) erubis (2.6.6)
abstract (>= 1.0.0) abstract (>= 1.0.0)
i18n (0.4.1) execjs (1.2.0)
johnson (1.2.0) multi_json (~> 1.0)
json (1.4.6) i18n (0.5.0)
mail (2.2.7) json (1.5.3)
libv8 (3.3.10.2)
mail (2.2.19)
activesupport (>= 2.3.6) activesupport (>= 2.3.6)
mime-types i18n (>= 0.4.0)
treetop (>= 1.4.5) mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16) mime-types (1.16)
multi_json (1.0.3)
polyglot (0.3.1) polyglot (0.3.1)
rack (1.2.1) rack (1.2.3)
rack-mount (0.6.13) rack-mount (0.6.14)
rack (>= 1.0.0) rack (>= 1.0.0)
rack-test (0.5.6) rack-test (0.5.7)
rack (>= 1.0) rack (>= 1.0)
rails (3.0.0) rails (3.0.7)
actionmailer (= 3.0.0) actionmailer (= 3.0.7)
actionpack (= 3.0.0) actionpack (= 3.0.7)
activerecord (= 3.0.0) activerecord (= 3.0.7)
activeresource (= 3.0.0) activeresource (= 3.0.7)
activesupport (= 3.0.0) activesupport (= 3.0.7)
bundler (~> 1.0.0) bundler (~> 1.0)
railties (= 3.0.0) railties (= 3.0.7)
railties (3.0.0) railties (3.0.7)
actionpack (= 3.0.0) actionpack (= 3.0.7)
activesupport (= 3.0.0) activesupport (= 3.0.7)
rake (>= 0.8.4) rake (>= 0.8.7)
thor (~> 0.14.0) thor (~> 0.14.4)
rake (0.8.7) rake (0.9.0)
sqlite3-ruby (1.3.1) sqlite3-ruby (1.3.1)
thor (0.14.3) therubyracer (0.9.2)
treetop (1.4.8) libv8 (~> 3.3.10)
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1) polyglot (>= 0.3.1)
tzinfo (0.3.23) tzinfo (0.3.29)


PLATFORMS PLATFORMS
ruby ruby


DEPENDENCIES DEPENDENCIES
johnson execjs
json json
rails (>= 3.0.0) rails (>= 3.0.0)
sqlite3-ruby sqlite3-ruby
therubyracer
61 changes: 38 additions & 23 deletions examples/rails3-example/public/javascripts/isotope_functions.js
Expand Up @@ -10,37 +10,52 @@ function getOrdinalSuffix(n) {


function formatDate(dateISO8601) { function formatDate(dateISO8601) {
var date = new Date(Date.parse(dateISO8601)); var date = new Date(Date.parse(dateISO8601));
return getMonthName(date.getMonth())+" "+date.getDate()+getOrdinalSuffix(date.getDate())+", "+date.getFullYear(); return getMonthName(date.getUTCMonth())+" "+date.getUTCDate()+getOrdinalSuffix(date.getUTCDate())+", "+date.getUTCFullYear();
} }


String.prototype.parenthesize = function () { String.prototype.parenthesize = function () {
return "(" + this + ")"; return "(" + this + ")";
}; };


/** /**
* Date.parse with progressive enhancement for ISO-8601, version 2 * Date.parse with progressive enhancement for ISO-8601, version 5
* © 2010 Colin Snover <http://zetafleet.com> * © 2010 Colin Snover <http://zetafleet.com>
* Released under MIT license. * Released under MIT license.
*/ */
// http://zetafleet.com/blog/javascript-dateparse-for-iso-8601 // http://zetafleet.com/blog/javascript-dateparse-for-iso-8601
(function () { (function (Date, undefined) {
var origParse = Date.parse; var origParse = Date.parse, numericKeys = [ 1, 4, 5, 6, 7, 10, 11 ];
Date.parse = function (date) { Date.parse = function (date) {
var timestamp = origParse(date), minutesOffset = 0, struct; var timestamp, struct, minutesOffset = 0;
if (isNaN(timestamp) && (struct = /^(\d{4}|[+\-]\d{6})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3,}))?)?(?:(Z)|([+\-])(\d{2})(?::?(\d{2}))?))?/.exec(date))) {
if (struct[8] !== 'Z') { // ES5 §15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string
minutesOffset = +struct[10] * 60 + (+struct[11]); // before falling back to any implementation-specific date parsing, so that’s what we do, even if native

// implementations could be faster
if (struct[9] === '+') { // 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec 8 Z 9 ± 10 tzHH 11 tzmm
minutesOffset = 0 - minutesOffset; if ((struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec(date))) {
} // avoid NaN timestamps caused by “undefined” values being passed to Date.UTC
} for (var i = 0, k; (k = numericKeys[i]); ++i) {

struct[k] = +struct[k] || 0;
if (!struct[7]) struct[7] = "0000"; }


timestamp = new Date(+struct[1], +struct[2] - 1, +struct[3], +struct[4], +struct[5] + minutesOffset, +struct[6], +struct[7].substr(0, 3)).getTime(); // allow undefined days and months
} struct[2] = (+struct[2] || 1) - 1;

struct[3] = +struct[3] || 1;
return timestamp;
}; if (struct[8] !== 'Z' && struct[9] !== undefined) {
}()); minutesOffset = struct[10] * 60 + struct[11];

if (struct[9] === '+') {
minutesOffset = 0 - minutesOffset;
}
}

timestamp = Date.UTC(struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7]);
}
else {
timestamp = origParse ? origParse(date) : NaN;
}

return timestamp;
};
}(Date));
15 changes: 7 additions & 8 deletions lib/isotope/isotope.rb
@@ -1,5 +1,5 @@
require 'rubygems' require 'rubygems'
require 'johnson' require 'execjs'
require 'json' require 'json'
require 'yaml' require 'yaml'
require 'erb' require 'erb'
Expand Down Expand Up @@ -39,14 +39,13 @@ def self.render_partial(view_file, options = {})


view_file_content = template_file_content(view_file) view_file_content = template_file_content(view_file)


script = " initial_script = included_scripts_source ? included_scripts_source + ";" : ""
#{included_scripts_source} initial_script << IO.read(isotope_file_path)
Johnson.runtime.load('#{isotope_file_path}');
Isotope(#{view_file_content.to_json}, #{options[:locals].to_json}); context = ExecJS.compile(initial_script)
" script = "Isotope(#{view_file_content.to_json}, #{options[:locals].to_json})"
output = context.eval(script)


output = Johnson.evaluate(script)

output output
end end


Expand Down
61 changes: 38 additions & 23 deletions test/isotope_functions.js
Expand Up @@ -10,37 +10,52 @@ function getOrdinalSuffix(n) {


function formatDate(dateISO8601) { function formatDate(dateISO8601) {
var date = new Date(Date.parse(dateISO8601)); var date = new Date(Date.parse(dateISO8601));
return getMonthName(date.getMonth())+" "+date.getDate()+getOrdinalSuffix(date.getDate())+", "+date.getFullYear(); return getMonthName(date.getUTCMonth())+" "+date.getUTCDate()+getOrdinalSuffix(date.getUTCDate())+", "+date.getUTCFullYear();
} }


String.prototype.parenthesize = function () { String.prototype.parenthesize = function () {
return "(" + this + ")"; return "(" + this + ")";
}; };


/** /**
* Date.parse with progressive enhancement for ISO-8601, version 2 * Date.parse with progressive enhancement for ISO-8601, version 5
* © 2010 Colin Snover <http://zetafleet.com> * © 2010 Colin Snover <http://zetafleet.com>
* Released under MIT license. * Released under MIT license.
*/ */
// http://zetafleet.com/blog/javascript-dateparse-for-iso-8601 // http://zetafleet.com/blog/javascript-dateparse-for-iso-8601
(function () { (function (Date, undefined) {
var origParse = Date.parse; var origParse = Date.parse, numericKeys = [ 1, 4, 5, 6, 7, 10, 11 ];
Date.parse = function (date) { Date.parse = function (date) {
var timestamp = origParse(date), minutesOffset = 0, struct; var timestamp, struct, minutesOffset = 0;
if (isNaN(timestamp) && (struct = /^(\d{4}|[+\-]\d{6})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3,}))?)?(?:(Z)|([+\-])(\d{2})(?::?(\d{2}))?))?/.exec(date))) {
if (struct[8] !== 'Z') { // ES5 §15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string
minutesOffset = +struct[10] * 60 + (+struct[11]); // before falling back to any implementation-specific date parsing, so that’s what we do, even if native

// implementations could be faster
if (struct[9] === '+') { // 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec 8 Z 9 ± 10 tzHH 11 tzmm
minutesOffset = 0 - minutesOffset; if ((struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec(date))) {
} // avoid NaN timestamps caused by “undefined” values being passed to Date.UTC
} for (var i = 0, k; (k = numericKeys[i]); ++i) {

struct[k] = +struct[k] || 0;
if (!struct[7]) struct[7] = "0000"; }


timestamp = new Date(+struct[1], +struct[2] - 1, +struct[3], +struct[4], +struct[5] + minutesOffset, +struct[6], +struct[7].substr(0, 3)).getTime(); // allow undefined days and months
} struct[2] = (+struct[2] || 1) - 1;

struct[3] = +struct[3] || 1;
return timestamp;
}; if (struct[8] !== 'Z' && struct[9] !== undefined) {
}()); minutesOffset = struct[10] * 60 + struct[11];

if (struct[9] === '+') {
minutesOffset = 0 - minutesOffset;
}
}

timestamp = Date.UTC(struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7]);
}
else {
timestamp = origParse ? origParse(date) : NaN;
}

return timestamp;
};
}(Date));