argonaut 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
- require 'json'
1
+ # require 'json'
2
+ require 'rubygems'
2
3
  require 'argonaut/version'
3
4
  require 'argonaut/generator'
4
- require 'argonaut/template'
5
+ require 'argonaut/template' if defined? Rails
@@ -1,8 +1,9 @@
1
+ require 'blankslate' unless defined? BlankSlate
1
2
  require 'json'
2
3
 
3
4
  module Argonaut
4
- class Generator
5
- instance_methods.each {|m| undef_method(m) unless %w(__id__ __send__ to_json to_xml instance_eval nil? is_a? class).include?(m.to_s)}
5
+ class Generator < BlankSlate
6
+ # instance_methods.each {|m| undef_method(m) unless %w(__id__ __send__ to_json to_xml instance_eval nil? is_a? class).include?(m.to_s)}
6
7
 
7
8
  def initialize(array_keys=[], &block)
8
9
  @array_keys = array_keys.map(&:to_sym)
@@ -13,6 +14,10 @@ module Argonaut
13
14
  def array_keys!(array_keys)
14
15
  @array_keys = array_keys.map(&:to_sym)
15
16
  end
17
+
18
+ def key(key, value)
19
+ method_missing(key.to_sym, value)
20
+ end
16
21
 
17
22
  def method_missing(key, *values)
18
23
  value = if block_given?
@@ -38,8 +43,9 @@ module Argonaut
38
43
  true
39
44
  end
40
45
 
41
- def to_json(extra=false)
42
- @hash.to_json(extra)
46
+ # Sometimes Rails adds a parameter
47
+ def to_json(filler=nil)
48
+ JSON.generate(@hash)
43
49
  end
44
50
 
45
51
  def to_s
@@ -1,3 +1,3 @@
1
1
  module Argonaut
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.0.2'.freeze
3
3
  end
@@ -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,22 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require 'argonaut'
3
+
4
+ doc = Argonaut::Generator.new do |json|
5
+ json.array_keys! ['results']
6
+ json.current_page 10
7
+ 10.times do |i|
8
+ json.results do |album|
9
+ album.album_id i
10
+ album.year i*10
11
+ album.artist 'Green Day'
12
+ album.album 'Dookie'
13
+ album.artwork do |artwork|
14
+ artwork.small 'url'
15
+ artwork.medium 'url'
16
+ artwork.large 'url'
17
+ end
18
+ end
19
+ end
20
+ json.pages 10
21
+ end
22
+ puts doc.to_json
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: argonaut
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Garrett Bjerkhoel
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-29 00:00:00 -08:00
18
+ date: 2010-12-30 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -38,6 +38,8 @@ files:
38
38
  - lib/argonaut/generator.rb
39
39
  - lib/argonaut/template.rb
40
40
  - lib/argonaut/version.rb
41
+ - lib/blankslate.rb
42
+ - template2.rb
41
43
  has_rdoc: true
42
44
  homepage: ""
43
45
  licenses: []