jsonbuilder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
File without changes
data/README ADDED
@@ -0,0 +1,62 @@
1
+ = json_builder
2
+
3
+ by nov <nov@cerego.com>
4
+
5
+ == Description
6
+
7
+ == Installation
8
+
9
+ git clone http://github.com/nov/json_builder.git
10
+ cd json_builder
11
+ rake install
12
+
13
+ === Archive Installation
14
+
15
+ rake install
16
+
17
+ === Gem Installation
18
+
19
+ gem install json_builder
20
+
21
+ == Features/Problems
22
+
23
+ JsonMarkup returns Hash, not Json text.
24
+ You need to call to_json for returned target.
25
+
26
+ USAGE:
27
+ def to_markup(markup, options = {})
28
+ markup = case format
29
+ when :xml
30
+ Builder::XmlMarkup.new
31
+ when :json
32
+ Builder::JsonMarkup.new
33
+ end
34
+ markup.user(
35
+ :id => id,
36
+ :url => url
37
+ )
38
+ markup.array_mode do
39
+ markup.images do
40
+ package.images.each do |image|
41
+ markup << image.to_markup(format, :only_url => true)
42
+ end
43
+ end
44
+ end
45
+ markup.target!
46
+ end
47
+
48
+ def to_xml(options = {})
49
+ self.to_markup(:xml, options)
50
+ end
51
+
52
+ def to_json(options = {})
53
+ self.to_markup(:json, options).to_json
54
+ end
55
+
56
+ == Synopsis
57
+
58
+ == Copyright
59
+
60
+ Author:: nov <nov@matake.jp>
61
+ Copyright:: Copyright (c) 2009 nov
62
+ License:: MIT License
@@ -0,0 +1,133 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rake/packagetask'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/rdoctask'
8
+ require 'rake/contrib/rubyforgepublisher'
9
+ require 'rake/contrib/sshpublisher'
10
+ require 'fileutils'
11
+ include FileUtils
12
+
13
+ NAME = "jsonbuilder"
14
+ AUTHOR = "nov"
15
+ EMAIL = "nov@matake.jp"
16
+ DESCRIPTION = "Builder::XmlMarkup like JsonBuilder (Builder::JsonMarkup)"
17
+ RUBYFORGE_PROJECT = NAME
18
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
19
+ BIN_FILES = %w( )
20
+
21
+ $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/lib"
22
+ require 'lib/json_builder'
23
+ VERS = Builder::JsonMarkup::Version.to_version
24
+ CLEAN.include ['*.gem', '.config']
25
+ RDOC_OPTS = [
26
+ "--title", "#{NAME} documentation",
27
+ "--charset", "utf-8",
28
+ "--opname", "index.html",
29
+ "--line-numbers",
30
+ "--main", "README",
31
+ "--inline-source",
32
+ ]
33
+
34
+ task :default => [:spec]
35
+ task :package => [:clean]
36
+
37
+ Rake::TestTask.new("spec") do |t|
38
+ t.libs << "spec"
39
+ t.pattern = "spec/**/*_spec.rb"
40
+ t.verbose = true
41
+ end
42
+
43
+ spec = Gem::Specification.new do |s|
44
+ s.name = NAME
45
+ s.version = VERS
46
+ s.platform = Gem::Platform::RUBY
47
+ s.has_rdoc = true
48
+ s.extra_rdoc_files = ["README", "ChangeLog"]
49
+ s.rdoc_options += RDOC_OPTS + ['--exclude', '^(examples)/']
50
+ s.summary = DESCRIPTION
51
+ s.description = DESCRIPTION
52
+ s.author = AUTHOR
53
+ s.email = EMAIL
54
+ s.homepage = HOMEPATH
55
+ s.executables = BIN_FILES
56
+ s.rubyforge_project = RUBYFORGE_PROJECT
57
+ s.require_path = "lib"
58
+ #s.autorequire = ""
59
+ s.test_files = Dir["spec/*_spec.rb"]
60
+ s.files = %w(README ChangeLog Rakefile) +
61
+ Dir.glob("{spec,lib}/**/*") +
62
+ Dir.glob("examples/**/*.rb")
63
+ end
64
+
65
+ Rake::GemPackageTask.new(spec) do |p|
66
+ p.need_tar = true
67
+ p.gem_spec = spec
68
+ end
69
+
70
+ desc "Install"
71
+ task :install do
72
+ name = "#{NAME}-#{VERS}.gem"
73
+ sh %{rake package}
74
+ sh %{sudo gem install pkg/#{name}}
75
+ end
76
+
77
+ desc "Uninstall"
78
+ task :uninstall => [:clean] do
79
+ sh %{sudo gem uninstall #{NAME}}
80
+ end
81
+
82
+ Rake::RDocTask.new do |rdoc|
83
+ rdoc.rdoc_dir = 'html'
84
+ rdoc.options += RDOC_OPTS
85
+ rdoc.template = "resh"
86
+ #rdoc.template = "#{ENV['template']}.rb" if ENV['template']
87
+ if ENV['DOC_FILES']
88
+ rdoc.rdoc_files.include(ENV['DOC_FILES'].split(/,\s*/))
89
+ else
90
+ rdoc.rdoc_files.include('README', 'ChangeLog')
91
+ rdoc.rdoc_files.include('lib/**/*.rb')
92
+ end
93
+ end
94
+
95
+ desc "Publish to RubyForge"
96
+ task :rubyforge => [:rdoc, :package] do
97
+ require 'rubyforge'
98
+ Rake::RubyForgePublisher.new(RUBYFORGE_PROJECT, 'nov').upload
99
+ end
100
+
101
+ # rake release VERSION=x.y.z
102
+ desc 'Package and upload the release to rubyforge.'
103
+ task :release => [:clean, :package] do |t|
104
+ v = ENV["VERSION"] or abort "Must supply VERSION=x.y.z"
105
+ abort "Versions don't match #{v} vs #{VERS}" unless v == VERS
106
+ pkg = "pkg/#{NAME}-#{VERS}"
107
+
108
+ require 'rubyforge'
109
+ rf = RubyForge.new.configure
110
+ puts "Logging in"
111
+ rf.login
112
+
113
+ c = rf.userconfig
114
+ c["preformatted"] = true
115
+
116
+ files = [
117
+ "#{pkg}.tgz",
118
+ "#{pkg}.gem"
119
+ ].compact
120
+
121
+ puts "Releasing #{NAME} v. #{VERS}"
122
+ rf.add_release RUBYFORGE_PROJECT, NAME, VERS, *files
123
+ end
124
+
125
+ desc 'Show information about the gem.'
126
+ task :debug_gem do
127
+ puts spec.to_ruby
128
+ end
129
+
130
+ desc 'Update gem spec'
131
+ task :gemspec do
132
+ open("#{NAME}.gemspec", 'w').write spec.to_ruby
133
+ end
@@ -0,0 +1,129 @@
1
+ module Builder
2
+
3
+ class XmlMarkup
4
+ def array_mode(&block)
5
+ yield(self)
6
+ end
7
+ end
8
+
9
+ class JsonMarkup
10
+
11
+ module Version
12
+ MAJOR = 0
13
+ MINOR = 0
14
+ REVISION = 1
15
+ class << self
16
+ def to_version
17
+ "#{MAJOR}.#{MINOR}.#{REVISION}"
18
+ end
19
+
20
+ def to_name
21
+ "#{MAJOR}_#{MINOR}_#{REVISION}"
22
+ end
23
+ end
24
+ end
25
+
26
+ def initialize(options = {})
27
+ # @default_content_key is used in such case: markup.key(value, :attr_key => attr_value)
28
+ # in this case, we need some key for value.
29
+ @default_content_key = (options[:default_content_key] || :content).to_sym
30
+ @include_root = options[:include_root]
31
+ @target = {}
32
+ @array_mode = false
33
+ end
34
+
35
+ def nil?
36
+ false
37
+ end
38
+
39
+ # NOTICE: you have to call this method to use array in json
40
+ def array_mode(&block)
41
+ @array_mode = true
42
+ yield(self)
43
+ @array_mode = false
44
+ end
45
+
46
+ def target!
47
+ if @include_root
48
+ @target
49
+ else
50
+ @target[@root]
51
+ end
52
+ end
53
+
54
+ # Do nothing
55
+ def comment!; end
56
+ def declare!; end
57
+ def instruct!; end
58
+ def comment!; end
59
+
60
+ def <<(_target)
61
+ if @array_mode
62
+ eval("#{current} ||= []")
63
+ eval("#{current} << _target")
64
+ else
65
+ eval("#{current} ||= {}")
66
+ eval("#{current}.merge!(_target)")
67
+ end
68
+ end
69
+
70
+ def text!(text)
71
+ if eval("#{current}").is_a?(Hash)
72
+ eval("#{current}.merge!({@default_content_key => text})")
73
+ else
74
+ eval("#{current} = text")
75
+ end
76
+ end
77
+ alias_method :cdata!, :text!
78
+
79
+ def tag!(key, *attrs, &block)
80
+ method_missing(key, *args, &block)
81
+ end
82
+
83
+ def method_missing(key, *args, &block)
84
+ key = args.first.is_a?(Symbol) ? "#{key}:#{args.shift}".to_sym : key.to_sym
85
+ args[0] = {@default_content_key => args[0]} if args.size > 1 && !args[0].is_a?(Hash)
86
+ unless @root
87
+ root(key, args, &block)
88
+ else
89
+ children(key, args, &block)
90
+ end
91
+ self
92
+ target!
93
+ end
94
+
95
+ private
96
+
97
+ def root(root, args, &block)
98
+ @root = root
99
+ @target[root] = {}
100
+ @path = [root]
101
+ set_args(args, &block)
102
+ yield(self) if block_given?
103
+ end
104
+
105
+ def children(key, args, &block)
106
+ @path.push(key)
107
+ set_args(args, &block)
108
+ @path.pop
109
+ end
110
+
111
+ def set_args(args, &block)
112
+ args.each do |arg|
113
+ case arg
114
+ when Hash
115
+ self << arg
116
+ else
117
+ eval("#{current} = arg")
118
+ end
119
+ end
120
+ yield(self) if block_given?
121
+ end
122
+
123
+ def current
124
+ "@target[:\"#{@path.join('"][:"')}\"]"
125
+ end
126
+
127
+ end
128
+
129
+ end
@@ -0,0 +1 @@
1
+ require 'builder/json_markup'
File without changes
@@ -0,0 +1,18 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+ $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
9
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'json_builder')
10
+
11
+ def be_a(klass)
12
+ be_is_a(klass)
13
+ end
14
+
15
+ def rand_string(length = 100)
16
+ chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
17
+ Array.new(length){ chars[rand(chars.size)] }.join
18
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonbuilder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - nov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-06 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Builder::XmlMarkup like JsonBuilder (Builder::JsonMarkup)
17
+ email: nov@matake.jp
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - ChangeLog
25
+ files:
26
+ - README
27
+ - ChangeLog
28
+ - Rakefile
29
+ - spec/builder
30
+ - spec/builder/json_markup_spec.rb
31
+ - spec/spec_helper.rb
32
+ - lib/builder
33
+ - lib/builder/json_markup.rb
34
+ - lib/json_builder.rb
35
+ has_rdoc: true
36
+ homepage: http://jsonbuilder.rubyforge.org
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --title
40
+ - jsonbuilder documentation
41
+ - --charset
42
+ - utf-8
43
+ - --opname
44
+ - index.html
45
+ - --line-numbers
46
+ - --main
47
+ - README
48
+ - --inline-source
49
+ - --exclude
50
+ - ^(examples)/
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project: jsonbuilder
68
+ rubygems_version: 1.3.1
69
+ signing_key:
70
+ specification_version: 2
71
+ summary: Builder::XmlMarkup like JsonBuilder (Builder::JsonMarkup)
72
+ test_files: []
73
+