argonaut 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/argonaut.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "argonaut/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "argonaut"
7
+ s.version = Argonaut::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Garrett Bjerkhoel"]
10
+ s.email = ["contact@garrettbjerkhoel.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Write a gem summary}
13
+ s.description = %q{JSON Builder}
14
+
15
+ s.rubyforge_project = "argonaut"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
data/examples.rb ADDED
@@ -0,0 +1,43 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require 'argonaut'
3
+
4
+ @ideas = [
5
+ {
6
+ :title => 'Garrett Bjerkhoel',
7
+ :comments => [
8
+ {
9
+ :id => 1,
10
+ :author => 'Jake',
11
+ :text => 'Sweet',
12
+ :created_at => Time.now,
13
+ :updated_at => Time.now
14
+ },
15
+ {
16
+ :id => 2,
17
+ :author => 'Jake',
18
+ :text => nil,
19
+ :created_at => Time.now,
20
+ :updated_at => Time.now
21
+ }
22
+ ]
23
+ }
24
+ ]
25
+
26
+
27
+ json = Argonaut::Generator.new do |doc|
28
+ doc.array_keys! ['ideas']
29
+ for idea in @ideas
30
+ doc.ideas do |i|
31
+ i.title idea[:title]
32
+ for comment in idea[:comments]
33
+ i.comments do |c|
34
+ c.id comment[:id]
35
+ c.author comment[:author]
36
+ c.text comment[:text]
37
+ c.created_at comment[:created_at]
38
+ c.updated_at comment[:updated_at]
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
data/lib/argonaut.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'json'
2
+ require 'argonaut/version'
3
+ require 'argonaut/generator'
4
+ require 'argonaut/template'
@@ -0,0 +1,53 @@
1
+ require 'json'
2
+
3
+ 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)}
6
+
7
+ def initialize(array_keys=[], &block)
8
+ @array_keys = array_keys.map(&:to_sym)
9
+ @hash = {}
10
+ block.call(self)
11
+ end
12
+
13
+ def array_keys!(array_keys)
14
+ @array_keys = array_keys.map(&:to_sym)
15
+ end
16
+
17
+ def method_missing(key, *values)
18
+ value = if block_given?
19
+ Generator.new(@array_keys) { |a| yield a }
20
+ else
21
+ values.size == 1 ? values.first : values
22
+ end
23
+
24
+ if @array_keys.include?(key) || @hash[key].is_a?(Array)
25
+ if @hash.include?(key)
26
+ @hash[key] << value
27
+ else
28
+ @hash[key] = [value]
29
+ end
30
+ elsif @hash[key].nil?
31
+ @hash[key] = value
32
+ else
33
+ @hash[key] = [@hash[key], value]
34
+ end
35
+ end
36
+
37
+ def respond_to?(method)
38
+ true
39
+ end
40
+
41
+ def to_json(extra=false)
42
+ @hash.to_json(extra)
43
+ end
44
+
45
+ def to_s
46
+ @hash.to_s
47
+ end
48
+
49
+ def inspect
50
+ @hash.inspect
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,20 @@
1
+ require 'action_view/base'
2
+ require 'action_view/template'
3
+
4
+ module ActionView
5
+ module Template::Handlers
6
+ class ArgonautBuilder < Template::Handler
7
+ include Compilable
8
+
9
+ self.default_format = Mime::JSON
10
+
11
+ def compile(template)
12
+ "doc = ::Argonaut::Generator.new do |json|" +
13
+ template.source +
14
+ "end;doc.to_json;"
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ ActionView::Template.register_template_handler :json_builder, ActionView::Template::Handlers::ArgonautBuilder
@@ -0,0 +1,3 @@
1
+ module Argonaut
2
+ VERSION = '0.0.1'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: argonaut
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Garrett Bjerkhoel
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-29 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: JSON Builder
23
+ email:
24
+ - contact@garrettbjerkhoel.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - argonaut.gemspec
36
+ - examples.rb
37
+ - lib/argonaut.rb
38
+ - lib/argonaut/generator.rb
39
+ - lib/argonaut/template.rb
40
+ - lib/argonaut/version.rb
41
+ has_rdoc: true
42
+ homepage: ""
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project: argonaut
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Write a gem summary
75
+ test_files: []
76
+