apimaster 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (26) hide show
  1. data/apimaster.gemspec +2 -2
  2. data/lib/apimaster/generators/{application.rb → app_generator.rb} +6 -15
  3. data/lib/apimaster/generators/base.rb +55 -3
  4. data/lib/apimaster/generators/controller_generator.rb +34 -0
  5. data/lib/apimaster/generators/model_generator.rb +33 -0
  6. data/lib/apimaster/generators/scripts.rb +18 -15
  7. data/lib/apimaster/generators/templates/Gemfile +6 -3
  8. data/lib/apimaster/generators/templates/Rakefile +4 -18
  9. data/lib/apimaster/generators/templates/app/controllers/befores_controller.rb.erb +13 -0
  10. data/lib/apimaster/generators/templates/app/controllers/examples_controller.rb.erb +15 -0
  11. data/lib/apimaster/generators/templates/app/controllers/index_controller.rb.erb +4 -0
  12. data/lib/apimaster/generators/templates/app/models/example.rb.erb +15 -0
  13. data/lib/apimaster/generators/templates/app/tasks/stat.rake.erb +27 -0
  14. data/lib/apimaster/generators/templates/app/tasks/test.rake.erb +21 -0
  15. data/lib/apimaster/generators/templates/config/application.rb.erb +2 -4
  16. data/lib/apimaster/generators/templates/config/initializer.rb.erb +1 -1
  17. data/lib/apimaster/generators/templates/config/settings/mongoid.yml.erb +15 -56
  18. data/lib/apimaster/generators/templates/test.watchr +21 -0
  19. data/lib/apimaster/generators/templates/test/factory/example_factory.rb.erb +7 -0
  20. data/lib/apimaster/generators/templates/test/functional/examples_controller_test.rb.erb +20 -0
  21. data/lib/apimaster/generators/templates/test/functional_test.rb.erb +0 -1
  22. data/lib/apimaster/generators/templates/test/test_helper.rb.erb +60 -0
  23. data/lib/apimaster/generators/templates/test/unit/example_test.rb.erb +11 -0
  24. data/lib/apimaster/generators/templates/test/unit_test.rb.erb +10 -1
  25. data/lib/apimaster/mapper.rb +39 -23
  26. metadata +14 -3
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'apimaster'
3
- s.version = '0.0.2'
4
- s.date = '2012-07-06'
3
+ s.version = '0.0.3'
4
+ s.date = '2012-07-12'
5
5
  s.summary = "ApiMaster!"
6
6
  s.description = "A simple restful api framework."
7
7
  s.authors = ["Sun", "Zhang", "Li"]
@@ -1,23 +1,10 @@
1
- require 'rbconfig'
2
1
 
3
2
  module Apimaster::Generators
4
- class Application < Create
5
-
6
- DEFAULT_SHEBANG = File.join(RbConfig::CONFIG['bindir'],
7
- RbConfig::CONFIG['ruby_install_name'])
8
-
9
- default_options :shebang => DEFAULT_SHEBANG,
10
- :an_option => 'some_default'
11
-
3
+ class AppGenerator < Create
12
4
  attr_reader :app_name, :module_name
13
5
 
14
6
  def initialize(runtime_args, runtime_options = {})
15
- runtime_options[:source] = File.dirname(__FILE__) + '/templates'
16
- runtime_options[:destination] = Dir.pwd
17
- runtime_options[:collision] = :ask
18
- runtime_options[:stdout] = STDOUT
19
7
  super
20
- usage if args.empty?
21
8
  #@destination_root = args.shift
22
9
  #@app_name = File.basename(File.expand_path(@destination_root))
23
10
  @app_name = args[0]
@@ -46,14 +33,17 @@ module Apimaster::Generators
46
33
  m.template "config.ru.erb", "config.ru"
47
34
  m.template "gitignore", ".gitignore"
48
35
  m.template "lib/module.rb.erb", "lib/#{app_name}.rb"
36
+ m.template "app/tasks/test.rake.erb", "app/tasks/test.rake"
37
+ m.template "app/tasks/stat.rake.erb", "app/tasks/stat.rake"
49
38
  m.template "app/controllers/index_controller.rb.erb", "app/controllers/index_controller.rb"
39
+ m.template "app/controllers/befores_controller.rb.erb", "app/controllers/befores_controller.rb"
50
40
 
51
41
  # Test stubs
52
42
  m.template "test/test_helper.rb.erb", "test/test_helper.rb"
53
43
  m.template "test/functional_test.rb.erb", "test/functional/index_controller_test.rb"
54
44
  m.template "test/unit_test.rb.erb", "test/unit/#{app_name}_test.rb"
55
45
 
56
- %w(LICENSE Rakefile README.md Gemfile TODO).each do |file|
46
+ %w(LICENSE Rakefile README.md Gemfile TODO test.watchr).each do |file|
57
47
  m.template file, file
58
48
  end
59
49
  end
@@ -94,6 +84,7 @@ module Apimaster::Generators
94
84
  app/views
95
85
  app/models
96
86
  app/helpers
87
+ app/tasks
97
88
  bin
98
89
  config
99
90
  config/settings
@@ -34,6 +34,8 @@
34
34
  # +manifest+ method inside your generator subclass.
35
35
  #
36
36
  #
37
+ require 'rbconfig'
38
+
37
39
  module Apimaster::Generators
38
40
  class GeneratorsError < StandardError; end
39
41
  class UsageError < GeneratorsError; end
@@ -74,6 +76,12 @@ module Apimaster::Generators
74
76
  class Base
75
77
  include Options
76
78
 
79
+ DEFAULT_SHEBANG = File.join(RbConfig::CONFIG['bindir'],
80
+ RbConfig::CONFIG['ruby_install_name'])
81
+
82
+ default_options :shebang => DEFAULT_SHEBANG,
83
+ :an_option => 'some_default'
84
+
77
85
  # Declare default options for the generator. These options
78
86
  # are inherited to subclasses.
79
87
  default_options :collision => :ask, :quiet => false, :stdout => STDOUT
@@ -94,6 +102,10 @@ module Apimaster::Generators
94
102
  attr_reader :source_root, :destination_root, :args, :stdout
95
103
 
96
104
  def initialize(runtime_args, runtime_options = {})
105
+ runtime_options[:source] = File.dirname(__FILE__) + '/templates'
106
+ runtime_options[:destination] = Dir.pwd
107
+ runtime_options[:collision] = :ask
108
+ runtime_options[:stdout] = STDOUT
97
109
  runtime_options[:backtrace] = true
98
110
  @logger = SimpleLogger.new
99
111
  @args = runtime_args
@@ -171,9 +183,14 @@ module Apimaster::Generators
171
183
  # and a hash of parsed arguments, takes the generator as an option
172
184
  # or first remaining argument, and invokes the requested command.
173
185
  def run
174
- # Look up generator instance and invoke command on it.
175
- manifest.replay(self)
176
- after_generate
186
+ name = args.shift
187
+ if name
188
+ # Look up generator instance and invoke command on it.
189
+ manifest.replay(self)
190
+ after_generate
191
+ else
192
+ puts 'Undefined name.' unless options[:help]
193
+ end
177
194
  rescue => e
178
195
  puts e
179
196
  puts " #{e.backtrace.join("\n ")}\n" if options[:backtrace]
@@ -186,6 +203,41 @@ module Apimaster::Generators
186
203
  string.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }.gsub('/', '::')
187
204
  end
188
205
 
206
+ def pluralize(word)
207
+ rules = {}
208
+ rules.store(/$/, 's')
209
+ rules.store(/s$/i, 's')
210
+ rules.store(/(ax|test)is$/i, '\1es')
211
+ rules.store(/(octop|vir)us$/i, '\1i')
212
+ rules.store(/(octop|vir)i$/i, '\1i')
213
+ rules.store(/(alias|status)$/i, '\1es')
214
+ rules.store(/(bu)s$/i, '\1ses')
215
+ rules.store(/(buffal|tomat)o$/i, '\1oes')
216
+ rules.store(/([ti])um$/i, '\1a')
217
+ rules.store(/([ti])a$/i, '\1a')
218
+ rules.store(/sis$/i, 'ses')
219
+ rules.store(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
220
+ rules.store(/(hive)$/i, '\1s')
221
+ rules.store(/([^aeiouy]|qu)y$/i, '\1ies')
222
+ rules.store(/(x|ch|ss|sh)$/i, '\1es')
223
+ rules.store(/(matr|vert|ind)(?:ix|ex)$/i, '\1ices')
224
+ rules.store(/(m|l)ouse$/i, '\1ice')
225
+ rules.store(/(m|l)ice$/i, '\1ice')
226
+ rules.store(/^(ox)$/i, '\1en')
227
+ rules.store(/^(oxen)$/i, '\1')
228
+ rules.store(/(quiz)$/i, '\1zes')
229
+ uncountables = %w(equipment information rice money species series fish sheep jeans)
230
+
231
+ result = word.to_s.dup
232
+
233
+ if word.empty? || uncountables.any? { |inflection| result =~ /\b#{inflection}\Z/i }
234
+ result
235
+ else
236
+ rules.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
237
+ result
238
+ end
239
+ end
240
+
189
241
  protected
190
242
  # Convenience method for generator subclasses to record a manifest.
191
243
  def record
@@ -0,0 +1,34 @@
1
+
2
+ module Apimaster::Generators
3
+ class ControllerGenerator < Create
4
+ attr_reader :app_name, :module_name, :name
5
+
6
+ def initialize(runtime_args, runtime_options = {})
7
+ super
8
+ @app_name = File.basename(File.expand_path('./'))
9
+ @module_name = camelize(app_name)
10
+ @name = args[0]
11
+ raise 'Undefined app name.' unless @app_name
12
+ end
13
+
14
+ def manifest
15
+ record do |m|
16
+ m.template "app/controllers/examples_controller.rb.erb", "app/controllers/#{pluralize name}_controller.rb"
17
+ m.template "test/functional/examples_controller_test.rb.erb", "test/functional/#{pluralize name}_controller_test.rb"
18
+ end
19
+ end
20
+
21
+ private
22
+ def banner
23
+ <<-EOS
24
+ Creates an Apimaster controller.
25
+
26
+ USAGE: apimaster controller your_controller_name"
27
+
28
+ NOTE: Without `_controller` suffix
29
+
30
+ EOS
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,33 @@
1
+
2
+ module Apimaster::Generators
3
+ class ModelGenerator < Create
4
+ attr_reader :app_name, :module_name, :name
5
+
6
+ def initialize(runtime_args, runtime_options = {})
7
+ super
8
+ @app_name = File.basename(File.expand_path('./'))
9
+ @module_name = camelize(app_name)
10
+ @name = args[0]
11
+ raise 'Undefined app name.' unless @app_name
12
+ end
13
+
14
+ def manifest
15
+ record do |m|
16
+ m.template "app/models/example.rb.erb", "app/models/#{name}.rb"
17
+ m.template "test/unit/example_test.rb.erb", "test/unit/#{name}_test.rb"
18
+ m.template "test/factory/example_factory.rb.erb", "test/factory/#{name}_factory.rb"
19
+ end
20
+ end
21
+
22
+ private
23
+ def banner
24
+ <<-EOS
25
+ Creates an Apimaster model.
26
+
27
+ USAGE: apimaster model your_model_name"
28
+
29
+ EOS
30
+ end
31
+
32
+ end
33
+ end
@@ -7,7 +7,9 @@ require File.dirname(__FILE__) + '/simple_logger'
7
7
 
8
8
  require File.dirname(__FILE__) + '/base'
9
9
  require File.dirname(__FILE__) + '/command'
10
- require File.dirname(__FILE__) + '/application'
10
+ require File.dirname(__FILE__) + '/app_generator'
11
+ require File.dirname(__FILE__) + '/model_generator'
12
+ require File.dirname(__FILE__) + '/controller_generator'
11
13
 
12
14
  module Apimaster::Generators
13
15
  module Scripts
@@ -25,7 +27,9 @@ module Apimaster::Generators
25
27
 
26
28
  def initialize
27
29
  @commands ||= {}
28
- register("new", Application)
30
+ register("new", AppGenerator)
31
+ register("model", ModelGenerator)
32
+ register("controller", ControllerGenerator)
29
33
  end
30
34
 
31
35
  def register name, klass
@@ -44,21 +48,20 @@ module Apimaster::Generators
44
48
  end
45
49
 
46
50
  # Look up generator instance and invoke command on it.
47
- if command = args.shift
48
- raise "Invalid command name: #{command}" unless commands.key?(command)
49
- commands[command].new(args).run
50
- else
51
- usage
52
- end
51
+ begin
52
+ command = args.shift
53
+ if command and commands.key?(command)
54
+ commands[command].new(args).run
55
+ else
56
+ raise "Invalid command name: #{command}"
57
+ end
58
+ rescue => e
59
+ stdout.puts e
60
+ stdout.puts " #{e.backtrace.join("\n ")}\n" if options[:backtrace]
61
+ raise SystemExit unless options[:no_exit]
53
62
  end
54
- rescue => e
55
- stdout.puts e
56
- stdout.puts " #{e.backtrace.join("\n ")}\n" if options[:backtrace]
57
- raise SystemExit unless options[:no_exit]
58
63
  end
59
64
 
60
- def usage
61
- stdout.puts "apimaster new your_app_name"
62
- end
65
+ end
63
66
  end
64
67
  end
@@ -1,4 +1,7 @@
1
- #source :gemcutter
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright (C) 2011-2012 AdMaster, Inc.
4
+
2
5
  source "https://rubygems.org"
3
6
 
4
7
  gem "sinatra"
@@ -6,6 +9,8 @@ gem "sinatra-contrib"
6
9
  gem "thin"
7
10
  gem "json"
8
11
  gem "rake"
12
+ gem "mongoid"
13
+ gem "bson_ext"
9
14
  gem "apimaster"
10
15
 
11
16
  group :development do
@@ -15,7 +20,5 @@ group :production do
15
20
  end
16
21
 
17
22
  group :test do
18
- gem "sqlite3"
19
23
  gem "rack-test"
20
24
  end
21
-
@@ -1,19 +1,5 @@
1
- require 'rake/gempackagetask'
2
- require 'rubygems/specification'
3
- require 'date'
4
- require 'bundler'
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright (C) 2011-2012 AdMaster, Inc.
5
4
 
6
- task :default => [:spec]
7
-
8
- require 'spec/rake/spectask'
9
- desc "Run specs"
10
- Spec::Rake::SpecTask.new do |t|
11
- t.spec_files = FileList['spec/**/*_spec.rb']
12
- t.spec_opts = %w(-fs --color)
13
- t.spec_opts << '--loadby' << 'random'
14
-
15
- t.rcov_opts << '--exclude' << 'spec,.bundle'
16
- t.rcov = ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true
17
- t.rcov_opts << '--text-summary'
18
- t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
19
- end
5
+ Dir["./app/tasks/**/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright (C) 2011-2012 AdMaster, Inc.
4
+
5
+ module <%= module_name %>
6
+ class BeforesController < Sinatra::Base
7
+
8
+ before do
9
+ # authorize
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module <%= module_name %>
2
+ class <%= pluralize camelize(name) %>Controller < Sinatra::Base
3
+
4
+ get '/<%= pluralize name %>/:id' do
5
+ <%= name %> = <%= camelize name %>.find params[:id]
6
+ json <%= name %>.to_hash(:get)
7
+ end
8
+
9
+ get '/<%= pluralize name %>' do
10
+ <%= pluralize name %> = <%= camelize name %>.all
11
+ json <%= pluralize name %>.to_hashes(:list)
12
+ end
13
+
14
+ end
15
+ end
@@ -1,3 +1,7 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright (C) 2011-2012 AdMaster, Inc.
4
+
1
5
  module <%= module_name %>
2
6
  class IndexController < Sinatra::Base
3
7
  get '/' do
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright (C) 2011-2012 AdMaster, Inc.
4
+
5
+ module <%= module_name %>
6
+ class <%= camelize name %> < Apimaster::Mapper
7
+ include Mongoid::Document
8
+
9
+ field :title, type: String
10
+ field :content, type: String
11
+
12
+ attr_options :title, accessor: [:list], required: [:post]
13
+ attr_options :content, accessor: [:get], required: [:post]
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ namespace :metric do
2
+
3
+ desc "project statistics"
4
+ task 'stat' do
5
+ puts "All:"
6
+ stat_files Dir.glob('**/*.{rb,slim,coffee,scss}')
7
+ puts "\nRuby:"
8
+ stat_files Dir.glob('**/*.rb') - Dir.glob('test/**/*.rb')
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def stat_files fs
15
+ c = 0
16
+ fc = 0
17
+ total_size = 0.0
18
+ fs.each do |f|
19
+ fc += 1
20
+ data = File.binread f
21
+ c += data.count "\n"
22
+ total_size += data.bytesize
23
+ end
24
+ puts "files: #{fc}"
25
+ puts "lines: #{c}"
26
+ puts "chars: #{total_size.to_i}"
27
+ end
@@ -0,0 +1,21 @@
1
+ %w[unit functional integration performance].each do |type|
2
+ desc "Run #{type} tests"
3
+ task "test:#{type}" do
4
+ require "./test/test_helper"
5
+ Dir.glob "./test/#{type}/*_test.rb" do |f|
6
+ require f
7
+ end
8
+ end
9
+
10
+ desc "Run single file tests"
11
+ task "test:#{type}:file" do
12
+ file = "./test/#{type}/#{ENV["name"]}_test.rb"
13
+ if File.exists? file
14
+ require "./test/test_helper"
15
+ require file
16
+ else
17
+ puts "Test file not found: #{file}"
18
+ end
19
+ end
20
+
21
+ end
@@ -7,12 +7,10 @@ module <%= module_name %>
7
7
 
8
8
  use Apimaster::Application
9
9
 
10
- before do
11
- # authorize
12
- end
13
-
14
10
  # controllers
11
+ use BeforesController
15
12
  use IndexController
13
+ use ExamplesController
16
14
  # use YourCountroller
17
15
 
18
16
  end
@@ -10,4 +10,4 @@ Apimaster::Setting.environment = ENV["RACK_ENV"]
10
10
  Apimaster::Setting.load_file(setting_files)
11
11
 
12
12
  # Database setting
13
- # Mongoid.load!("./config/settings/mongoid.yml")
13
+ Mongoid.load!("./config/settings/mongoid.yml")
@@ -1,66 +1,25 @@
1
- development:
1
+ production:
2
2
  sessions:
3
3
  default:
4
- database: mongoid
4
+ database: <%= app_name %>
5
+ username: user
6
+ username: user
7
+ password: password
5
8
  hosts:
6
9
  - localhost:27017
7
-
8
- # Tell Mongoid which environment this configuration is for.
9
- production:
10
- # This starts the session configuration settings. You may have as
11
- # many sessions as you like, but you must have at least 1 named
12
- # 'default'.
10
+ development:
13
11
  sessions:
14
- # Define the default session.
15
12
  default:
16
- # A session can have any number of hosts. Usually 1 for a single
17
- # server setup, and at least 3 for a replica set. Hosts must be
18
- # an array of host:port pairs. This session is single server.
19
- hosts:
20
- - flame.mongohq.com:27017
21
- # Define the default database name.
22
- database: mongoid
23
- # Since this database points at a session connected to MongoHQ, we must
24
- # provide the authentication details.
13
+ database: <%= app_name %>_develop
14
+ username: user
25
15
  username: user
26
16
  password: password
27
- # This defines a secondary session at a replica set.
28
- replica_set:
29
- # This configuration is a 3 node replica set.
30
17
  hosts:
31
- - dedicated1.myapp.com:27017
32
- - dedicated2.myapp.com:27017
33
- - dedicated3.myapp.com:27017
34
- database: mongoid
35
- # We can set session specific options, like reads executing
36
- # on secondary nodes, and defaulting the session to safe mode.
37
- options:
38
- consistency: :eventual
39
- safe: true
40
- # This defines a tertiary session at a Mongos fronted shard.
41
- shard:
42
- # This configuration is a Mongos shard server.
18
+ - localhost:27017
19
+ test:
20
+ sessions:
21
+ default:
22
+ database: <%= app_name %>_test
43
23
  hosts:
44
- - mongos.myapp.com:27017
45
- database: mongoid
46
- # This configuration shows an authenticated replica set via a uri.
47
- another:
48
- uri: mongodb://user:pass@59.1.22.1:27017,59.1.22.2:27017/mongoid
49
- # Here we put the Mongoid specific configuration options. These are explained
50
- # in more detail next.
51
- options:
52
- allow_dynamic_fields: false
53
- identity_map_enabled: true
54
- include_root_in_json: true
55
- include_type_for_serialization: true
56
- # Note this can also be true if you want to preload everything, but this is
57
- # almost never necessary. Most of the time set this to false.
58
- preload_models:
59
- - Canvas
60
- - Browser
61
- - Firefox
62
- scope_overwrite_exception: true
63
- raise_not_found_error: false
64
- skip_version_check: false
65
- use_activesupport_time_zone: false
66
- use_utc: true
24
+ - localhost:27017
25
+
@@ -0,0 +1,21 @@
1
+
2
+ #watch('^test.*/.*_test\.rb') { run tests }
3
+ watch('^test/functional/(.*)_test\.rb') { |m| run tests('functional', m[1]) }
4
+ watch('^test/unit/(.*)_test\.rb') { |m| run tests('unit', m[1]) }
5
+ # --------------------------------------------------
6
+ # Helpers
7
+ # --------------------------------------------------
8
+
9
+ def tests(type, file = nil)
10
+ cmd = "rake test:#{type}"
11
+ cmd << ":file name=#{file}" if file
12
+ end
13
+
14
+ def run(cmd)
15
+ now = Time.now.to_s
16
+ puts "", "=" * cmd.length, now, ""
17
+ puts cmd
18
+ puts "-" * cmd.length, ""
19
+ system cmd
20
+ end
21
+
@@ -0,0 +1,7 @@
1
+ class <%= camelize name %>Factory < BaseFactory
2
+
3
+ register <%= module_name %>::<%= camelize name %>
4
+
5
+ define :title, "Hello Title"
6
+ define :content, "This is hello content."
7
+ end
@@ -0,0 +1,20 @@
1
+ class <%= pluralize camelize(name) %>ControllerTest < FunctionalTestCase
2
+ def setup
3
+ <%= module_name %>::<%= camelize name %>.delete_all
4
+ end
5
+
6
+ def test_get_<%= name %>_by_id
7
+ <%= name %> = <%= camelize name %>Factory.post
8
+
9
+ get "/<%= pluralize name %>/#{<%= name %>._id}"
10
+ assert_equal 200, last_response.status
11
+ end
12
+
13
+ def test_get_<%= pluralize name %>
14
+ 3.times { <%= camelize name %>Factory.post }
15
+
16
+ get "/<%= pluralize name %>"
17
+ assert_equal 200, last_response.status
18
+ end
19
+
20
+ end
@@ -1,2 +1 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
1
 
@@ -1 +1,61 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright (C) 2011-2012 AdMaster, Inc.
1
4
 
5
+ ENV['RACK_ENV'] ||= "test"
6
+
7
+ require "minitest/autorun"
8
+ require "rack/test"
9
+ require "./config/boot"
10
+
11
+ class BaseFactory
12
+
13
+ def self.define name, value = nil
14
+ self.attrs[name] = value
15
+ end
16
+
17
+ def self.attrs
18
+ @attrs ||= {}
19
+ end
20
+
21
+ def self.attr key
22
+ @attrs ||= {}
23
+ @attrs[key]
24
+ end
25
+
26
+ def self.register name
27
+ @klass = name
28
+ end
29
+
30
+ def self.post data = {}
31
+ raise "Please register class first.`" unless @klass
32
+ @klass.post attrs.merge(data)
33
+ end
34
+
35
+ end
36
+
37
+ class TestCase < MiniTest::Unit::TestCase
38
+
39
+ end
40
+
41
+ class FunctionalTestCase < TestCase
42
+
43
+ include Rack::Test::Methods
44
+
45
+ def app
46
+ @app ||= Sinatra.new(<%= module_name %>::Application) {}
47
+ end
48
+
49
+ def body
50
+ last_response.body
51
+ end
52
+
53
+ def patch(uri, params = {}, env = {}, &block)
54
+ env = env.merge(:method => "PATCH", :params => params)
55
+ request(uri, env, &block)
56
+ end
57
+
58
+ end
59
+
60
+ Dir.glob "./test/factory/**/*_factory.rb" do |f| require f end
61
+ Dir.glob "./test/mock/**/*_mock.rb" do |f| require f end
@@ -0,0 +1,11 @@
1
+ class <%= camelize name %>Test < TestCase
2
+ def setup
3
+ <%= module_name %>::<%= camelize name %>.delete_all
4
+ end
5
+
6
+ def test_title
7
+ <%= name %> = <%= module_name %>::<%= camelize name %>.new
8
+ <%= name %>.title = 'hello'
9
+ assert_equal 'hello', <%= name %>.title
10
+ end
11
+ end
@@ -1,2 +1,11 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright (C) 2011-2012 AdMaster, Inc.
2
4
 
5
+ class <%= module_name %>Test < TestCase
6
+
7
+ def test_sample_method
8
+ assert 1, 1
9
+ end
10
+
11
+ end
@@ -4,34 +4,24 @@
4
4
 
5
5
  module Apimaster
6
6
  class Mapper
7
- # include Mongoid::Document
7
+ #include ::Mongoid::Document
8
8
 
9
9
  def post hash
10
- from_hash hash
11
- save
10
+ save_with_hash hash, :post
12
11
  end
13
12
 
14
13
  def put hash
15
- from_hash hash
16
- save
14
+ save_with_hash hash, :put
17
15
  end
18
16
 
19
17
  def patch hash
20
- from_hash hash
21
- save
18
+ save_with_hash hash, :patch
22
19
  end
23
20
 
24
- def to_hash accessor = :all
25
- record = {}
26
- fields = self.class.find_attrs_in_options(:accessor, accessor)
27
- fields.each do |field|
28
- if self.respond_to?(field)
29
- record[field] = self.send(field)
30
- else
31
- raise "Dataset #{self.class} has no method with the name of #{field}"
32
- end
33
- end
34
- record
21
+ def save_with_hash hash, method
22
+ from_hash hash, method
23
+ save
24
+ self
35
25
  end
36
26
 
37
27
  def from_hash(hash, method = :all)
@@ -39,7 +29,7 @@ module Apimaster
39
29
  attrs = [:required, :optional]
40
30
 
41
31
  attrs.each do |type|
42
- fields = self.class.get_attrs(type, method)
32
+ fields = self.class.find_attrs_in_options(type, method)
43
33
  fields.each do |field|
44
34
  if hash.has_key?(field)
45
35
  data[field] = hash[field]
@@ -62,25 +52,51 @@ module Apimaster
62
52
  self.send name, val
63
53
  end
64
54
 
55
+ def to_hash accessor = :all
56
+ record = {}
57
+ fields = self.class.find_attrs_in_options(:accessor, accessor)
58
+ fields.each do |field|
59
+ if self.respond_to?(field)
60
+ record[field] = self.send(field)
61
+ else
62
+ raise "Dataset #{self.class} has no method with the name of #{field}"
63
+ end
64
+ end
65
+ record
66
+ end
67
+
65
68
  class << self
66
69
 
67
70
  OPTION_TYPES = [:accessor, :required, :optional]
68
71
 
69
- @attr_options ||= {}
70
-
71
72
  # attr_options :url, accessor: [:get, :list]
72
73
  def attr_options name, options = {}
74
+ @attr_options ||= {}
73
75
  @attr_options[name] = options
74
76
  end
75
77
 
76
78
  # [:url, :name]
77
79
  def find_attrs_in_options type, option = :all
78
80
  raise "Unknown attribute options type: #{type}" unless OPTION_TYPES.include? type
81
+ @attr_options ||= {}
79
82
  @attr_options.select do |name, options|
80
- type_options = options.is_a?(Hash) and options.key?(type) ? options[type] : nil
81
- type_options.is_a?(Array) and (type_options.include?(option) or type_options.include(:all))
83
+ type_options = (options.is_a?(Hash) and options.key?(type)) ? options[type] : nil
84
+ type_options.is_a?(Array) and (type_options.include?(option) or type_options.include?(:all))
82
85
  end.keys
83
86
  end
87
+
88
+ def post hash
89
+ self.new.post hash
90
+ end
91
+
92
+ def to_hashes accessor = :all
93
+ result = []
94
+ self.each do |val|
95
+ result << val.to_hash(accessor)
96
+ end
97
+ result
98
+ end
99
+
84
100
  end
85
101
  end
86
102
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apimaster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-07-06 00:00:00.000000000 Z
14
+ date: 2012-07-12 00:00:00.000000000 Z
15
15
  dependencies: []
16
16
  description: A simple restful api framework.
17
17
  email: sunxiqiu@admaster.com.cn
@@ -24,14 +24,21 @@ files:
24
24
  - lib/apimaster/application.rb
25
25
  - lib/apimaster/controllers/errors.rb
26
26
  - lib/apimaster/error.rb
27
- - lib/apimaster/generators/application.rb
27
+ - lib/apimaster/generators/app_generator.rb
28
28
  - lib/apimaster/generators/base.rb
29
29
  - lib/apimaster/generators/command.rb
30
+ - lib/apimaster/generators/controller_generator.rb
30
31
  - lib/apimaster/generators/manifest.rb
32
+ - lib/apimaster/generators/model_generator.rb
31
33
  - lib/apimaster/generators/options.rb
32
34
  - lib/apimaster/generators/scripts.rb
33
35
  - lib/apimaster/generators/simple_logger.rb
36
+ - lib/apimaster/generators/templates/app/controllers/befores_controller.rb.erb
37
+ - lib/apimaster/generators/templates/app/controllers/examples_controller.rb.erb
34
38
  - lib/apimaster/generators/templates/app/controllers/index_controller.rb.erb
39
+ - lib/apimaster/generators/templates/app/models/example.rb.erb
40
+ - lib/apimaster/generators/templates/app/tasks/stat.rake.erb
41
+ - lib/apimaster/generators/templates/app/tasks/test.rake.erb
35
42
  - lib/apimaster/generators/templates/config/application.rb.erb
36
43
  - lib/apimaster/generators/templates/config/boot.rb.erb
37
44
  - lib/apimaster/generators/templates/config/initializer.rb.erb
@@ -46,9 +53,13 @@ files:
46
53
  - lib/apimaster/generators/templates/LICENSE
47
54
  - lib/apimaster/generators/templates/Rakefile
48
55
  - lib/apimaster/generators/templates/README.md
56
+ - lib/apimaster/generators/templates/test/factory/example_factory.rb.erb
57
+ - lib/apimaster/generators/templates/test/functional/examples_controller_test.rb.erb
49
58
  - lib/apimaster/generators/templates/test/functional_test.rb.erb
50
59
  - lib/apimaster/generators/templates/test/test_helper.rb.erb
60
+ - lib/apimaster/generators/templates/test/unit/example_test.rb.erb
51
61
  - lib/apimaster/generators/templates/test/unit_test.rb.erb
62
+ - lib/apimaster/generators/templates/test.watchr
52
63
  - lib/apimaster/generators/templates/TODO
53
64
  - lib/apimaster/generators/version.rb
54
65
  - lib/apimaster/helpers/headers.rb