jam-ruby 0.0.4

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.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rails", "3.0.9"
4
+ gem "capybara", ">= 0.4.0"
5
+ gem "sqlite3"
6
+ gem 'tilt'
7
+
8
+ # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
9
+ # gem 'ruby-debug'
10
+ # gem 'ruby-debug19'
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 YOURNAME
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.rdoc ADDED
@@ -0,0 +1,94 @@
1
+ = Jam
2
+
3
+ Jam - Simple, readable and flexible JSON Api Maker based on standart rails tools and hash syntax.
4
+
5
+ == Installation
6
+
7
+ gem install jam
8
+
9
+ == Configuration
10
+
11
+ Standart Rails configuration:
12
+
13
+ ActiveRecord::Base.include_root_in_json = true
14
+
15
+ == Examples
16
+
17
+ === Basic usage
18
+
19
+ object @post do |p|
20
+ {
21
+ :body => p.downcase
22
+ }
23
+ end
24
+
25
+ === Use to_json attributes (only, except, methods, root)
26
+
27
+ object @post, :only => [:title] do |p|
28
+ {
29
+ :body => p.downcase
30
+ }
31
+ end
32
+
33
+ === For collections
34
+
35
+ collection @posts { |p|
36
+ {
37
+ :body => p.downcase
38
+ }
39
+ }
40
+
41
+ === Use template
42
+
43
+ collection @posts do |post|
44
+ template('posts/show', :@post => post)
45
+ end
46
+
47
+ === Add children
48
+
49
+ collection @posts do |p|
50
+ {
51
+ :comments => collection p.comments, :only => [:body]...
52
+ }
53
+ end
54
+
55
+ === Root
56
+
57
+ Works like in Rails to_json:
58
+
59
+ object @post, :root => :bost
60
+
61
+ == Contributions
62
+
63
+ We open for you contributions, sugesstions and ideas.
64
+
65
+ == Inspired by
66
+
67
+ - Tequila
68
+ - Rabl
69
+
70
+ Some of code was borrowed from RABL.
71
+
72
+ == Licence
73
+
74
+ The MIT License
75
+
76
+ Copyright (c) 2011 Eduard Tsech edtsech@gmail.com
77
+
78
+ Permission is hereby granted, free of charge, to any person obtaining a copy
79
+ of this software and associated documentation files (the "Software"), to deal
80
+ in the Software without restriction, including without limitation the rights
81
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
82
+ copies of the Software, and to permit persons to whom the Software is
83
+ furnished to do so, subject to the following conditions:
84
+
85
+ The above copyright notice and this permission notice shall be included in
86
+ all copies or substantial portions of the Software.
87
+
88
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
89
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
90
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
91
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
92
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
93
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
94
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ begin
4
+ require 'bundler'
5
+ require 'bundler/setup'
6
+ Bundler::GemHelper.install_tasks
7
+ rescue LoadError
8
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
9
+ end
10
+
11
+ require 'rake'
12
+ require 'rake/rdoctask'
13
+
14
+ require 'rake/testtask'
15
+
16
+ Rake::TestTask.new(:test) do |t|
17
+ t.libs << 'lib'
18
+ t.libs << 'test'
19
+ t.pattern = 'test/**/*_test.rb'
20
+ t.verbose = false
21
+ end
22
+
23
+ task :default => :test
24
+
25
+ Rake::RDocTask.new(:rdoc) do |rdoc|
26
+ rdoc.rdoc_dir = 'rdoc'
27
+ rdoc.title = 'jam'
28
+ rdoc.options << '--line-numbers' << '--inline-source'
29
+ rdoc.rdoc_files.include('README.rdoc')
30
+ rdoc.rdoc_files.include('lib/jam/builder.rb')
31
+ end
@@ -0,0 +1,87 @@
1
+ module Jam
2
+ class Builder
3
+ include ActionView::Helpers
4
+
5
+ # Constructs a new engine based on given source and options
6
+ #
7
+ # JamTemplate.new { 'object @post' }
8
+ def initialize(source, options={})
9
+ @_source = source
10
+ @_options = options
11
+ end
12
+
13
+ # Renders the representation based on source, object, scope and locals
14
+ #
15
+ # JamTemplate.new { 'object @post' }.render(@scope)
16
+ def render(scope, locals, &block)
17
+ @_locals, @_scope = locals, scope
18
+ self.copy_instance_variables_from(@_scope, [:@assigns, :@helpers])
19
+
20
+ instance_eval(@_source) if @_source.present?
21
+ end
22
+
23
+ # Returns an hash representation for each element in the collection
24
+ # with options and passed block
25
+ #
26
+ # collection @posts, :only => [:body] do |p|
27
+ # { :title => p.title.titleize }
28
+ # end
29
+ def collection(obj, options={}, &block)
30
+ obj.inject [] do |res, x|
31
+ res << object(x, options, &block)
32
+ end
33
+ end
34
+
35
+ # Returns an hash representation of the passed object
36
+ # with options and passed block
37
+ #
38
+ # object @post, :only => [:body] do |p|
39
+ # { :title => p.title.titleize }
40
+ # end
41
+ def object(obj, options={}, &block)
42
+ if ActiveRecord::Base.include_root_in_json
43
+ root = options[:root] || obj.class.model_name.element
44
+ end
45
+
46
+ result = {}
47
+
48
+ result.merge!(obj.as_json(options))
49
+
50
+ if block
51
+ blk = block.call(obj).stringify_keys
52
+ result.merge!(blk) unless root
53
+ result[root].merge!(blk) if root
54
+ end
55
+
56
+ result.stringify_keys
57
+ end
58
+
59
+ # Render template with variables passed in the hash
60
+ #
61
+ # template ('posts/show', :@post => post)
62
+ def template(file, vars={})
63
+ source = self.fetch_source(file)
64
+ vars.each do |k, v|
65
+ @_scope.instance_variable_set k, v
66
+ end
67
+ Jam::Builder.new(source).render(@_scope, @_locals)
68
+ end
69
+
70
+ protected
71
+
72
+ def copy_instance_variables_from(object, exclude = []) #:nodoc:
73
+ vars = object.instance_variables.map(&:to_s) - exclude.map(&:to_s)
74
+ vars.each { |name| instance_variable_set(name, object.instance_variable_get(name)) }
75
+ end
76
+
77
+ # Returns source for a given relative file
78
+ #
79
+ # fetch_source("show", :view_path => "...") => "...contents..."
80
+ def fetch_source(file, options={})
81
+ root_path = Rails.root if defined?(Rails)
82
+ view_path = options[:view_path] || File.join(root_path, "app/views/")
83
+ file_path = Dir[File.join(view_path, file + "*.jam")].first
84
+ File.read(file_path) if file_path
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,67 @@
1
+ # TILT Template
2
+ if defined?(Tilt)
3
+ class JamTemplate < Tilt::Template
4
+ def initialize_engine
5
+ return if defined?(::Jam)
6
+ require_template_library 'jam'
7
+ end
8
+
9
+ def prepare
10
+ options = @options.merge(:format => @options[:format])
11
+ @engine = ::Jam::Builder.new(data, options)
12
+ end
13
+
14
+ def evaluate(scope, locals, &block)
15
+ @engine.render(scope, locals, &block)
16
+ end
17
+ end
18
+
19
+ Tilt.register 'jam', JamTemplate
20
+ end
21
+
22
+ # Rails 2.X Template
23
+ if defined?(Rails) && Rails.version =~ /^2/
24
+ require 'action_view/base'
25
+ require 'action_view/template'
26
+
27
+ module ActionView
28
+ module TemplateHandlers
29
+ class JamHandler < TemplateHandler
30
+ include Compilable
31
+
32
+ def compile(template) %{
33
+ ::Jam::Builder.new(#{template.source.inspect}, { :format => #{template.format.inspect} }).
34
+ render(self, assigns.merge(local_assigns)).to_json
35
+ } end
36
+ end
37
+ end
38
+ end
39
+
40
+ ActionView::Template.register_template_handler :jam, ActionView::TemplateHandlers::JamHandler
41
+ end
42
+
43
+ # Rails 3.X Template
44
+ if defined?(Rails) && Rails.version =~ /^3/
45
+ module ActionView
46
+ module Template::Handlers
47
+ class Jam
48
+
49
+ class_attribute :default_format
50
+ self.default_format = Mime::JSON
51
+
52
+ def self.call(template)
53
+ source = if template.source.empty?
54
+ File.read(template.identifier)
55
+ else # use source
56
+ template.source
57
+ end
58
+
59
+ %{ ::Jam::Builder.new(#{source.inspect}).
60
+ render(self, assigns.merge(local_assigns)).to_json }
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ ActionView::Template.register_template_handler :jam, ActionView::Template::Handlers::Jam
67
+ end
data/lib/jam.rb ADDED
@@ -0,0 +1,5 @@
1
+ require File.join(File.dirname(__FILE__), 'jam/template.rb')
2
+ require File.join(File.dirname(__FILE__), 'jam/builder.rb')
3
+
4
+ module Jam
5
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jam-ruby
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 4
10
+ version: 0.0.4
11
+ platform: ruby
12
+ authors:
13
+ - Eduard Tsech
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-17 00:00:00 +07:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: JSON Api Maker.
23
+ email:
24
+ - edtsech@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - lib/jam/builder.rb
33
+ - lib/jam/template.rb
34
+ - lib/jam.rb
35
+ - MIT-LICENSE
36
+ - Rakefile
37
+ - Gemfile
38
+ - README.rdoc
39
+ has_rdoc: true
40
+ homepage: ""
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.5.2
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: JSON Api Maker.
73
+ test_files: []
74
+