lucy 0.2.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,20 @@
1
+ Copyright (c) 2008 [name of plugin creator]
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
@@ -0,0 +1,18 @@
1
+ Lucy
2
+ ====
3
+
4
+ Lucy in the sky with rubies.
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ #Ruby
11
+ Lucy.generate("horses", [{:name => "Gone with the Wind", :color => "Blue"}, {:name => "Powerade Powell", :color => "Pink"}]);
12
+
13
+ //JS
14
+ Lucy.horses[0].name
15
+ => "Gone with the Wind"
16
+
17
+
18
+ Copyright (c) 2008 Tore Darell, released under the MIT license
@@ -0,0 +1,37 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the lucy plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the lucy plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'Lucy'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ require 'jeweler'
26
+ Jeweler::Tasks.new do |gem|
27
+ # gem is a Gem::Specification. See http://docs.rubygems.org/read/chapter/20 for more options
28
+ gem.name = "lucy"
29
+ gem.homepage = "http://github.com/toretore/lucy"
30
+ gem.license = "MIT"
31
+ gem.summary = %Q{Rails plugin for generating javascript files from Ruby objects}
32
+ gem.description = gem.summary
33
+ gem.email = ""
34
+ gem.authors = ["Tore Darell"]
35
+ gem.add_development_dependency 'activesupport'
36
+ end
37
+ Jeweler::RubygemsDotOrgTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.1
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "lucy"
2
+ require "lucy/common"
@@ -0,0 +1,76 @@
1
+ module Lucy
2
+
3
+ DEFAULT_NAMESPACE = "Lucy"
4
+ DEFAULT_GLOBAL = "window"
5
+
6
+ def self.javascript_dir
7
+ @js_dir ||= File.join(Rails.root, "public", "javascripts")
8
+ end
9
+
10
+ def self.generate(key, content=nil, path=nil)
11
+ File.open(path || File.join(javascript_dir, "#{key}.js"), "w") do |f|
12
+ g = JavascriptGenerator.new
13
+ if block_given?
14
+ yield g
15
+ else
16
+ g.write key, content
17
+ end
18
+ f.write g.to_js
19
+ end
20
+ end
21
+
22
+ def self.file(src_path, dest_path=nil)
23
+ File.cp(src_path, dest_path || javascript_dir)
24
+ end
25
+
26
+
27
+ class JavascriptGenerator
28
+
29
+ attr_writer :namespace, :global, :init_namespace
30
+
31
+ def initialize
32
+ @keyed = []
33
+ @raw = []
34
+ yield self if block_given?
35
+ end
36
+
37
+ def write(key, obj)
38
+ @keyed << [key, ActiveSupport::JSON.encode(obj)]
39
+ end
40
+ alias []= write
41
+
42
+ def write_raw(data)
43
+ @raw << data
44
+ end
45
+ alias << write_raw
46
+
47
+ def to_a
48
+ (init_namespace? ? [init_namespace] : []) + @keyed.map do |key, value|
49
+ "#{namespace}.#{[key].flatten.join('.')} = #{value};"
50
+ end + @raw
51
+ end
52
+
53
+ def to_js
54
+ to_a.join("\n\n")
55
+ end
56
+
57
+ def namespace
58
+ @namespace || DEFAULT_NAMESPACE
59
+ end
60
+
61
+ def global
62
+ @global || DEFAULT_GLOBAL
63
+ end
64
+
65
+ def init_namespace
66
+ "if (!#{global}.#{namespace}) { #{namespace} = {}; }"
67
+ end
68
+
69
+ def init_namespace?
70
+ @init_namespace.nil? ? true : @init_namespace
71
+ end
72
+
73
+ end
74
+
75
+
76
+ end
@@ -0,0 +1,25 @@
1
+ module Lucy
2
+ module Common
3
+
4
+ def self.included(controller)
5
+ controller.send(:before_filter, :generate_common_javascript)
6
+ end
7
+
8
+ private
9
+
10
+ def generate_common_javascript
11
+ unless Rails.env.production? && Thread.current[:common_javascript_generated]
12
+ Lucy.generate "common" do |g|
13
+ g.namespace = "Rails"
14
+ g[:authenticityToken] = protect_against_forgery? ? form_authenticity_token : nil
15
+ g[:env] = Rails.env
16
+ g[:version] = Rails.version
17
+ end
18
+ Thread.current[:common_javascript_generated] = true
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+
25
+ ActionController::Base.send(:include, Lucy::Common)
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :lucy do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,78 @@
1
+ require 'test_helper'
2
+
3
+ class LucyTest < Test::Unit::TestCase
4
+
5
+ KEY = 'test'
6
+ PATH = File.join(File.dirname(__FILE__), 'test.js')
7
+ CONTENT = {:foo => "bar", :bar => {:ponies => true}, :numbers => [1,2,3]}
8
+
9
+ def test_generate_should_generate_file
10
+ Lucy.generate(KEY, CONTENT, PATH)
11
+ assert File.exists?(PATH)
12
+ end
13
+
14
+ def test_generate_should_yield_generator
15
+ generator = nil
16
+ Lucy.generate(KEY,CONTENT,PATH){|g| generator = g }
17
+ assert_not_nil generator
18
+ assert generator.is_a?(Lucy::JavascriptGenerator)
19
+ end
20
+
21
+ def test_generate_should_write_file_after_block_returns
22
+ content = nil
23
+ Lucy.generate KEY, nil, PATH do |g|
24
+ g.init_namespace = false
25
+ g.namespace = "Test"
26
+ g[:foo] = "humbaba"
27
+ content = g.to_js
28
+ end
29
+ assert_equal content, File.read(PATH)
30
+ end
31
+
32
+ def teardown
33
+ File.unlink(PATH) if File.exists?(PATH)
34
+ end
35
+
36
+ end
37
+
38
+
39
+ class JavascriptGeneratorTest < Test::Unit::TestCase
40
+
41
+ GLOBAL = 'test'
42
+ NAMESPACE = 'Test'
43
+ NAMESPACE_INIT = "if (!#{GLOBAL}.#{NAMESPACE}) { #{NAMESPACE} = {}; }"
44
+ NAMED = {:foo => "bar", :bar => {:ponies => true}, :numbers => [1,2,3]}
45
+ NAMED_EXPECTED = NAMED.map{|k,v| "#{NAMESPACE}.#{k} = #{ActiveSupport::JSON.encode(v)};" }
46
+ RAW = "function humbaba(){ alert('Whatcha doin in my forest?'); };"
47
+ EXPECTED_OUTPUT = ([NAMESPACE_INIT]+NAMED_EXPECTED+[RAW]).join("\n\n")
48
+
49
+ def setup
50
+ @generator = Lucy::JavascriptGenerator.new
51
+ @generator.global = GLOBAL
52
+ @generator.namespace = NAMESPACE
53
+ NAMED.each{|k,v| @generator[k] = v }
54
+ @generator << RAW
55
+ end
56
+
57
+ def test_should_include_init_namespace_by_default
58
+ assert_equal NAMESPACE_INIT, @generator.to_a.first
59
+ end
60
+
61
+ def test_should_not_include_init_namespace_when_told_not_to
62
+ @generator.init_namespace = false
63
+ assert_not_equal NAMESPACE_INIT, @generator.to_a.first
64
+ end
65
+
66
+ def test_should_generate_properties_added_by_write
67
+ assert_equal NAMED_EXPECTED, @generator.to_a[1,3]
68
+ end
69
+
70
+ def test_should_add_raw
71
+ assert_equal RAW, @generator.to_a.last
72
+ end
73
+
74
+ def test_should_concatenate_named_and_raw_with_to_js
75
+ assert_equal EXPECTED_OUTPUT, @generator.to_js
76
+ end
77
+
78
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'test/unit'
4
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'lucy')
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lucy
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 1
10
+ version: 0.2.1
11
+ platform: ruby
12
+ authors:
13
+ - Tore Darell
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-20 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activesupport
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Rails plugin for generating javascript files from Ruby objects
36
+ email: ""
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ files:
44
+ - MIT-LICENSE
45
+ - README
46
+ - Rakefile
47
+ - VERSION
48
+ - init.rb
49
+ - lib/lucy.rb
50
+ - lib/lucy/common.rb
51
+ - lib/tasks/lucy_tasks.rake
52
+ - test/lucy_test.rb
53
+ - test/test_helper.rb
54
+ has_rdoc: true
55
+ homepage: http://github.com/toretore/lucy
56
+ licenses:
57
+ - MIT
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.7
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Rails plugin for generating javascript files from Ruby objects
88
+ test_files:
89
+ - test/lucy_test.rb
90
+ - test/test_helper.rb