notator 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Kristoph Cichocki-Romanov
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.
@@ -0,0 +1,109 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # Copyright 2004, 2006 by Jim Weirich (jim@weirichhouse.org).
4
+ # All rights reserved.
5
+
6
+ # Permission is granted for use, copying, modification, distribution,
7
+ # and distribution of modified versions of this work as long as the
8
+ # above copyright notice is included.
9
+ #++
10
+
11
+ ######################################################################
12
+ # BlankSlate provides an abstract base class with no predefined
13
+ # methods (except for <tt>\_\_send__</tt> and <tt>\_\_id__</tt>).
14
+ # BlankSlate is useful as a base class when writing classes that
15
+ # depend upon <tt>method_missing</tt> (e.g. dynamic proxies).
16
+ #
17
+ class BlankSlate
18
+ class << self
19
+
20
+ # Hide the method named +name+ in the BlankSlate class. Don't
21
+ # hide +instance_eval+ or any method beginning with "__".
22
+ def hide(name)
23
+ if instance_methods.include?(name.to_s) and
24
+ name !~ /^(__|instance_eval)/
25
+ @hidden_methods ||= {}
26
+ @hidden_methods[name.to_sym] = instance_method(name)
27
+ undef_method name
28
+ end
29
+ end
30
+
31
+ def find_hidden_method(name)
32
+ @hidden_methods ||= {}
33
+ @hidden_methods[name] || superclass.find_hidden_method(name)
34
+ end
35
+
36
+ # Redefine a previously hidden method so that it may be called on a blank
37
+ # slate object.
38
+ def reveal(name)
39
+ hidden_method = find_hidden_method(name)
40
+ fail "Don't know how to reveal method '#{name}'" unless hidden_method
41
+ define_method(name, hidden_method)
42
+ end
43
+ end
44
+
45
+ instance_methods.each { |m| hide(m) }
46
+ end
47
+
48
+ ######################################################################
49
+ # Since Ruby is very dynamic, methods added to the ancestors of
50
+ # BlankSlate <em>after BlankSlate is defined</em> will show up in the
51
+ # list of available BlankSlate methods. We handle this by defining a
52
+ # hook in the Object and Kernel classes that will hide any method
53
+ # defined after BlankSlate has been loaded.
54
+ #
55
+ module Kernel
56
+ class << self
57
+ alias_method :blank_slate_method_added, :method_added
58
+
59
+ # Detect method additions to Kernel and remove them in the
60
+ # BlankSlate class.
61
+ def method_added(name)
62
+ result = blank_slate_method_added(name)
63
+ return result if self != Kernel
64
+ BlankSlate.hide(name)
65
+ result
66
+ end
67
+ end
68
+ end
69
+
70
+ ######################################################################
71
+ # Same as above, except in Object.
72
+ #
73
+ class Object
74
+ class << self
75
+ alias_method :blank_slate_method_added, :method_added
76
+
77
+ # Detect method additions to Object and remove them in the
78
+ # BlankSlate class.
79
+ def method_added(name)
80
+ result = blank_slate_method_added(name)
81
+ return result if self != Object
82
+ BlankSlate.hide(name)
83
+ result
84
+ end
85
+
86
+ def find_hidden_method(name)
87
+ nil
88
+ end
89
+ end
90
+ end
91
+
92
+ ######################################################################
93
+ # Also, modules included into Object need to be scanned and have their
94
+ # instance methods removed from blank slate. In theory, modules
95
+ # included into Kernel would have to be removed as well, but a
96
+ # "feature" of Ruby prevents late includes into modules from being
97
+ # exposed in the first place.
98
+ #
99
+ class Module
100
+ alias blankslate_original_append_features append_features
101
+ def append_features(mod)
102
+ result = blankslate_original_append_features(mod)
103
+ return result if mod != Object
104
+ instance_methods.each do |name|
105
+ BlankSlate.hide(name)
106
+ end
107
+ result
108
+ end
109
+ end
@@ -0,0 +1,2 @@
1
+ require 'notator/javascript_object_notation'
2
+ require 'notator/template' if defined? Rails
@@ -0,0 +1,125 @@
1
+ require 'rubygems'
2
+ require 'blankslate' unless defined? BlankSlate
3
+ require 'active_support'
4
+ require 'json'
5
+
6
+ module Notator
7
+ #-------------
8
+
9
+ class JavascriptObjectNotation < BlankSlate
10
+ #------------------------------------------
11
+
12
+ def initialize( options = {} )
13
+ #-------------------------------
14
+
15
+ @pretty = options[ :pretty ] || false;
16
+
17
+ @context_stack = [];
18
+ @context_stack.push( Hash.new )
19
+
20
+ end
21
+
22
+ def hash!( key, *args, &block )
23
+ #------------------------------
24
+ method_missing( key.to_sym, *args, &block )
25
+ end
26
+
27
+ alias :tag! :hash!
28
+
29
+ def array!( key = nil, *args, &block )
30
+ #-------------------------------------
31
+
32
+ if block.nil?
33
+
34
+ raise 'Thearray! method requires an array argument.' \
35
+ unless args.first.is_a?( Array )
36
+
37
+ @context_stack.last[ key ] = args.first
38
+
39
+ else
40
+
41
+ array = Array.new
42
+
43
+ if @context_stack.last.is_a?( Hash )
44
+
45
+ raise 'The array! method requires a key in the context of a hash.' \
46
+ if key.nil?
47
+
48
+ @context_stack.last[ key ] = array
49
+
50
+ else
51
+
52
+ @context_stack.last.push( array );
53
+
54
+ end
55
+
56
+ @context_stack.push( array )
57
+ block.call( self )
58
+ @context_stack.pop
59
+
60
+ end
61
+
62
+ end
63
+
64
+ def value!( value = nil, &block )
65
+ #--------------------------------
66
+
67
+ raise 'The value! method can only be called in the context of ' +
68
+ 'an array.' \
69
+ unless @context_stack.last.is_a?( Array )
70
+
71
+ if block.nil?
72
+
73
+ @context_stack.last.push( value )
74
+
75
+ else
76
+
77
+ hash = Hash.new
78
+ @context_stack.last.push( hash )
79
+ @context_stack.push( hash )
80
+ block.call( self )
81
+ @context_stack.pop
82
+
83
+ end
84
+
85
+ end
86
+
87
+ def method_missing( key, *args, &block )
88
+ #---------------------------------------
89
+
90
+ raise 'A key-value pair cannot be added in the context of an array.' \
91
+ if @context_stack.last.is_a?( Array )
92
+
93
+ if block.nil?
94
+ @context_stack.last[ key ] = args.first
95
+ else
96
+ hash = Hash.new
97
+ @context_stack.last[ key ] = hash
98
+ @context_stack.push( hash )
99
+ block.call( self )
100
+ @context_stack.pop
101
+ end
102
+
103
+ end
104
+
105
+ def <<( json )
106
+ #-------------
107
+
108
+ hash = JSON[ json ]
109
+
110
+ @context_stack.last.is_a?( Array ) ?
111
+ @context_stack.last.push( hash ) :
112
+ @context_stack.last.merge!( hash )
113
+
114
+ end
115
+
116
+ def to_json
117
+ #----------
118
+ @pretty ?
119
+ JSON.pretty_generate( @context_stack.first ) :
120
+ @context_stack.first.to_json
121
+ end
122
+
123
+ end
124
+
125
+ end
@@ -0,0 +1,47 @@
1
+ require 'action_view/base'
2
+ require 'action_view/template'
3
+
4
+ module ActionView
5
+ #----------------
6
+
7
+ class Base
8
+ #---------
9
+
10
+ cattr_accessor :pretty_json
11
+ @@pretty_json = false
12
+
13
+ end
14
+
15
+ end
16
+
17
+ module ActionView
18
+ #----------------
19
+
20
+ module Template::Handlers
21
+ #------------------------
22
+
23
+ class Notator
24
+ #------------
25
+
26
+ # default format used by Notator
27
+ class_attribute :default_format
28
+ self.default_format = Mime::JSON
29
+
30
+ def call( template )
31
+ #-------------------
32
+
33
+ "json = ::Notator::JavascriptObjectNotation.new();" +
34
+ "(" + template.source + ");" +
35
+ "json.to_json;"
36
+
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+
45
+ ActionView::Template.
46
+ register_template_handler :notator,
47
+ ActionView::Template::Handlers::Notator.new
@@ -0,0 +1,3 @@
1
+ module Notator
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: notator
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - OnTerra Research Inc.
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-08-12 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: json
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 0.8.7
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rspec
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 2.0.0
47
+ type: :development
48
+ version_requirements: *id003
49
+ description: Notator facilitate the elegant assembly of JSON constructs using Ruby. It includes a Rails handler that allows for templates that render JSON responses.
50
+ email:
51
+ - open-source@onterraresearch.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - lib/blankslate.rb
60
+ - lib/notator/javascript_object_notation.rb
61
+ - lib/notator/template.rb
62
+ - lib/notator/version.rb
63
+ - lib/notator.rb
64
+ - LICENSE
65
+ has_rdoc: true
66
+ homepage: ""
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options: []
71
+
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 1.3.6
86
+ requirements: []
87
+
88
+ rubyforge_project: notator
89
+ rubygems_version: 1.5.3
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Notator facilitate the elegant assembly of JSON constructs using Ruby. It includes a Rails handler that allows for templates that render JSON responses.
93
+ test_files: []
94
+