rack-app 3.0.0.gamma → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 15eb9ec10fdc0e02a3cb11addc8b894951d69440
4
- data.tar.gz: d5279dda2a8261405333f46e4eab83633fdaf8be
3
+ metadata.gz: 54ad60f95e02d3a3d1cdd12869c110ebd8936c20
4
+ data.tar.gz: 7acb5847ee8f8a9b12939572a316e0bd2e543b62
5
5
  SHA512:
6
- metadata.gz: 72b4effba0e6198d4996c994402f920d34abedeb336addb65bacdd6a5ad1e2d8074a446800df5efbc58e4b638c6719605ee547d9a501993c32d142c2170349e5
7
- data.tar.gz: 3b64ceccee914c068ffc5dc661ba8c12805853ab7c69e1165f8ba62dc7c340ad50335d76bbc1c19b2e35413d4799cec8816f0f0a8a76462a22e9a074944ef64d
6
+ metadata.gz: 434df103635f1c9376d83c6736652337156e685ded88130846b62371beac56a3b9dd620e730e1dbe52c5127985d7247c223a8f515061b9c097dc8625339262cf
7
+ data.tar.gz: 65712cce379e85a8183799f0d1aa0e37714c4e7e6642dce3005b83336812670afdaecd3f4431376d92f6f6d57d64e014a85ae7cce37a08c50da0c750a64d37c6
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.0.gamma
1
+ 3.0.0
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rack/app/cli'
3
+ Rack::App::CLI.start(ARGV)
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift(File.join(File.dirname(File.dirname(__FILE__)),'lib'))
3
+ require 'rack/app/cli'
4
+ Rack::App::CLI.start(ARGV)
@@ -7,6 +7,7 @@ class Rack::App
7
7
  require 'rack/app/version'
8
8
  require 'rack/app/constants'
9
9
 
10
+ require 'rack/app/cli'
10
11
  require 'rack/app/test'
11
12
  require 'rack/app/utils'
12
13
  require 'rack/app/params'
@@ -0,0 +1,48 @@
1
+ require 'rack/app'
2
+ require 'optparse'
3
+ class Rack::App::CLI
4
+
5
+ require 'rack/app/cli/command'
6
+ require 'rack/app/cli/default_commands'
7
+ require 'rack/app/cli/runner'
8
+
9
+ class << self
10
+
11
+ def start(argv)
12
+ runner.start(argv)
13
+ end
14
+
15
+ def runner
16
+ Rack::App::CLI::Runner.new(rack_app)
17
+ end
18
+
19
+ def rack_app
20
+ @rack_app ||= lambda {
21
+ context = {}
22
+ Kernel.__send__(:define_method, :run) { |app, *_| context[:app]= app }
23
+ config_ru_file_path = Rack::App::Utils.pwd('config.ru')
24
+ load(config_ru_file_path) if File.exist?(config_ru_file_path)
25
+ context[:app]
26
+ }.call
27
+ end
28
+
29
+ end
30
+
31
+ def merge!(cli)
32
+ commands.merge!(cli.commands)
33
+ self
34
+ end
35
+
36
+ def commands
37
+ @commands ||= {}
38
+ end
39
+
40
+ protected
41
+
42
+ def command(name, &block)
43
+ command_prototype = Class.new(Rack::App::CLI::Command)
44
+ command_prototype.class_exec(&block)
45
+ commands[name.to_s.to_sym]= command_prototype.new(name.to_s.to_sym)
46
+ end
47
+
48
+ end
@@ -0,0 +1,93 @@
1
+ class Rack::App::CLI::Command
2
+
3
+ require 'optparse'
4
+
5
+ class << self
6
+
7
+ def optparse_options
8
+ @options_parser_options ||= []
9
+ end
10
+
11
+ def description(message = nil)
12
+ @description = message unless message.nil?
13
+ @description || ''
14
+ end
15
+
16
+ alias desc description
17
+
18
+ def option(*args, &block)
19
+ optparse_options << {:args => args, :block => block}
20
+ end
21
+
22
+ alias on option
23
+
24
+ def action(&block)
25
+ define_method(:action, &block)
26
+ end
27
+
28
+ end
29
+
30
+ attr_reader :name
31
+
32
+ def initialize(name)
33
+ @name = name.to_s
34
+ @option_parser = OptionParser.new
35
+ attach_definitions!
36
+ update_banner!
37
+ end
38
+
39
+ def help_message
40
+ @option_parser.help
41
+ end
42
+
43
+ def description
44
+ self.class.description
45
+ end
46
+
47
+ def start(argv)
48
+ @option_parser.parse!(argv)
49
+ action(*argv)
50
+ rescue ArgumentError => ex
51
+ $stderr.puts(ex.message)
52
+ end
53
+
54
+ def action(*argv)
55
+ end
56
+
57
+ protected
58
+
59
+ def attach_definitions!
60
+ self.class.optparse_options.each do |h|
61
+ @option_parser.on(*h[:args]) do |*args|
62
+ instance_exec(*args,&h[:block])
63
+ end
64
+ end
65
+ end
66
+
67
+ def update_banner!
68
+
69
+ banner = @option_parser.banner
70
+ banner.sub!('[options]', "#{@name} [options]")
71
+
72
+ # [[:req, :a], [:opt, :b], [:rest, :c], [:keyreq, :d], [:keyrest, :e]]
73
+ (method(:action).parameters rescue []).each do |type, keyword|
74
+ case type
75
+ when :req
76
+ banner.concat(" <#{keyword}>")
77
+
78
+ when :opt
79
+ banner.concat(" [<#{keyword}>]")
80
+
81
+ when :rest, :keyrest
82
+ banner.concat(" [<#{keyword}> <#{keyword}> ...]")
83
+
84
+ end
85
+ end
86
+
87
+ banner.concat("\n\n")
88
+ banner.concat(description)
89
+ banner.concat("\n\n")
90
+
91
+ end
92
+
93
+ end
@@ -0,0 +1,3 @@
1
+ module Rack::App::CLI::DefaultCommands
2
+ require 'rack/app/cli/default_commands/list_commands'
3
+ end
@@ -0,0 +1,29 @@
1
+ module Rack::App::CLI::DefaultCommands::ListCommands
2
+
3
+ extend self
4
+
5
+ def get_message(known_commands)
6
+ puts_collection = []
7
+
8
+ add_header(puts_collection)
9
+
10
+ list_command_name = 'commands'
11
+ rjust = known_commands.keys.push(list_command_name).map(&:to_s).map(&:length).max + 3
12
+
13
+ puts_collection << [list_command_name.to_s.rjust(rjust), 'list all available command'].join(' ')
14
+ known_commands.sort_by { |name, _| name.to_s }.each do |name, command|
15
+ puts_collection << [name.to_s.rjust(rjust), command.description].join(' ')
16
+ end
17
+
18
+ puts_collection
19
+ end
20
+
21
+ protected
22
+
23
+ def add_header(puts_collection)
24
+ cmd_file_name = File.basename($0)
25
+ puts_collection << "Usage: #{cmd_file_name} <command> [options] <args>\n\n"
26
+ puts_collection << "Some useful #{cmd_file_name} commands are:"
27
+ end
28
+
29
+ end
@@ -0,0 +1,50 @@
1
+ class Rack::App::CLI::Runner
2
+
3
+ def initialize(app)
4
+ @cli = app.respond_to?(:cli) ? app.cli : Rack::App::CLI.new
5
+ end
6
+
7
+ def start(argv)
8
+ command_name = argv.shift
9
+ start_command_for(command_name,argv)
10
+ end
11
+
12
+ protected
13
+
14
+ def show_commands
15
+ $stdout.puts(Rack::App::CLI::DefaultCommands::ListCommands.get_message(commands))
16
+ end
17
+
18
+ def show_help_message(argv)
19
+ command_name = argv.shift
20
+ command = command_for(command_name)
21
+ command ? $stdout.puts(command.help_message) : show_commands
22
+ end
23
+
24
+ def start_command_for(command_name, argv)
25
+ case command_name.to_s
26
+
27
+ when 'commands'
28
+ show_commands
29
+
30
+ when 'help'
31
+ show_help_message(argv)
32
+
33
+ end
34
+
35
+ command = command_for(command_name)
36
+
37
+ command && command.start(argv)
38
+ end
39
+
40
+ def command_for(name)
41
+ return if name.nil?
42
+
43
+ commands[name.to_s.to_sym]
44
+ end
45
+
46
+ def commands
47
+ @cli.commands
48
+ end
49
+
50
+ end
@@ -30,7 +30,7 @@ class Rack::App::ErrorHandler
30
30
  end
31
31
 
32
32
  def parent(ex)
33
- handler = @handlers.find { |exception_class, handler| ex.class <= exception_class }
33
+ handler = @handlers.find { |exception_class, _| ex.class <= exception_class }
34
34
  return handler.nil? ? nil : handler.last
35
35
  end
36
36
 
@@ -1,44 +1,37 @@
1
- class Rack::App::Extension
1
+ module Rack::App::Extension
2
2
 
3
- require 'rack/app/extension/factory'
3
+ extend self
4
4
 
5
- class << self
5
+ def apply_extensions(app_class, applied_ext_names, apply_ext_names)
6
+ apply_ext_names.each do |extension_name|
7
+ extension_name = format_extension_name(extension_name)
6
8
 
7
- def names
8
- @names ||= []
9
- end
10
-
11
- def name(extension_name_alias)
12
- names << extension_name_alias.to_s.to_sym
13
- end
14
-
15
- def inherited(klass)
16
- klass.name(Rack::App::Utils.snake_case(klass.to_s.split('::').last).to_sym)
17
- end
9
+ next if applied_ext_names.include?(extension_name)
10
+ applied_ext_names << extension_name
18
11
 
19
- def includes
20
- @includes ||= []
21
- end
12
+ ext = find_extension_for(extension_name)
13
+ app_class.class_eval(&ext)
22
14
 
23
- def extends
24
- @extends ||= []
25
15
  end
16
+ nil
17
+ end
26
18
 
27
- def inheritances
28
- @on_inheritances ||= []
29
- end
19
+ def format_extension_name(extension_name)
20
+ extension_name.to_s.to_sym
21
+ end
30
22
 
31
- def include(endpoint_methods_module)
32
- includes << endpoint_methods_module
33
- end
23
+ def register(extension_name, &builder_block)
24
+ extensions[format_extension_name(extension_name)]= builder_block
25
+ end
34
26
 
35
- def extend(app_class_methods_module)
36
- extends << app_class_methods_module
37
- end
27
+ protected
38
28
 
39
- def on_inheritance(&block)
40
- inheritances << block
41
- end
29
+ def extensions
30
+ @extensions ||= {}
31
+ end
42
32
 
33
+ def find_extension_for(sym_name)
34
+ return extensions[sym_name.to_s.to_sym] || raise("Not registered extension name requested: #{sym_name}")
43
35
  end
36
+
44
37
  end
@@ -32,7 +32,7 @@ class Rack::App::FileServer
32
32
 
33
33
  def clean_path_info(env)
34
34
  path_info = ::Rack::Utils.unescape(env[::Rack::PATH_INFO])
35
- return clean_path_info = ::Rack::Utils.clean_path_info(path_info)
35
+ ::Rack::Utils.clean_path_info(path_info)
36
36
  end
37
37
 
38
38
  end
@@ -6,6 +6,7 @@ module Rack::App::SingletonMethods
6
6
  require 'rack/app/singleton_methods/rack_interface'
7
7
  require 'rack/app/singleton_methods/route_handling'
8
8
  require 'rack/app/singleton_methods/settings'
9
+ require 'rack/app/singleton_methods/extensions'
9
10
 
10
11
  include Rack::App::SingletonMethods::HttpMethods
11
12
  include Rack::App::SingletonMethods::Inheritance
@@ -13,5 +14,6 @@ module Rack::App::SingletonMethods
13
14
  include Rack::App::SingletonMethods::RackInterface
14
15
  include Rack::App::SingletonMethods::RouteHandling
15
16
  include Rack::App::SingletonMethods::Settings
17
+ include Rack::App::SingletonMethods::Extensions
16
18
 
17
19
  end
@@ -0,0 +1,18 @@
1
+ module Rack::App::SingletonMethods::Extensions
2
+
3
+ protected
4
+
5
+ def applied_extensions
6
+ @applied_extensions ||= []
7
+ end
8
+
9
+ def apply_extensions(*extension_names)
10
+ Rack::App::Extension.apply_extensions(self,applied_extensions,extension_names)
11
+ end
12
+
13
+ def extensions(*extensions_names)
14
+ apply_extensions(*extensions_names)
15
+ applied_extensions
16
+ end
17
+
18
+ end
@@ -12,7 +12,7 @@ module Rack::App::SingletonMethods::Inheritance
12
12
 
13
13
  child.serializer(&serializer.logic)
14
14
  child.headers.merge!(headers)
15
- child.__send__(:middlewares).push(*middlewares)
15
+ child.middlewares.push(*middlewares)
16
16
 
17
17
  on_inheritance.each do |block|
18
18
  block.call(self, child)
@@ -1,22 +1,20 @@
1
1
  module Rack::App::SingletonMethods::Mounting
2
2
 
3
- def mount(api_class, mount_prop={})
4
- mount_to_path = ::Rack::App::Utils.deep_dup(mount_prop[:to])
5
-
3
+ def mount(api_class, properties={})
6
4
  unless api_class.is_a?(Class) and api_class <= Rack::App
7
5
  raise(ArgumentError, 'Invalid class given for mount, must be a Rack::App')
8
6
  end
9
7
 
10
8
  duplication = ::Rack::App::Utils.deep_dup(api_class)
11
-
12
9
  duplication.on_mounted.each do |on_mount|
13
- duplication.class_exec(mount_prop,&on_mount)
10
+ duplication.class_exec(::Rack::App::Utils.deep_dup(properties), &on_mount)
14
11
  end
15
12
 
16
- merge_prop = {:namespaces => [@namespaces, mount_to_path].flatten}
13
+ cli.merge!(duplication.cli)
14
+ merge_prop = {:namespaces => [@namespaces, properties[:to]].flatten}
17
15
  router.merge_router!(duplication.router, merge_prop)
18
16
 
19
- return nil
17
+ nil
20
18
  end
21
19
 
22
20
  def mount_directory(directory_path, options={})
@@ -1,5 +1,11 @@
1
1
  module Rack::App::SingletonMethods::Settings
2
2
 
3
+ def cli(&block)
4
+ @cli ||= Rack::App::CLI.new
5
+ @cli.instance_exec(&block) unless block.nil?
6
+ @cli
7
+ end
8
+
3
9
  protected
4
10
 
5
11
  def serializer(&definition_how_to_serialize)
@@ -18,26 +24,6 @@ module Rack::App::SingletonMethods::Settings
18
24
  @headers
19
25
  end
20
26
 
21
- def extensions(*extensions)
22
- extensions.each do |ext|
23
-
24
- if ext.is_a?(Symbol)
25
- ext = Rack::App::Extension::Factory::find_for(ext)
26
- end
27
-
28
- if ext.is_a?(::Class) && ext < ::Rack::App::Extension
29
-
30
- ext.includes.each { |m| include(m) }
31
- ext.extends.each { |m| extend(m) }
32
- ext.inheritances.each { |block| on_inheritance(&block) }
33
-
34
- else
35
- raise("unsupported extension reference: #{ext.inspect}")
36
- end
37
-
38
- end
39
- end
40
-
41
27
  def error(*exception_classes, &block)
42
28
  @error_handler ||= Rack::App::ErrorHandler.new
43
29
  unless block.nil?
@@ -47,8 +33,6 @@ module Rack::App::SingletonMethods::Settings
47
33
  return @error_handler
48
34
  end
49
35
 
50
- private
51
-
52
36
  def middlewares(&block)
53
37
  @middlewares ||= []
54
38
  @middlewares << block unless block.nil?
@@ -21,6 +21,7 @@ module Rack::App::Utils::DeepDup
21
21
 
22
22
  def dup(register, object)
23
23
 
24
+ return object unless registrable?(object)
24
25
  return registered(object, register) if registered(object, register)
25
26
 
26
27
  case object
@@ -37,7 +38,7 @@ module Rack::App::Utils::DeepDup
37
38
  when Struct
38
39
  dup_struct(register, object)
39
40
 
40
- when NilClass, Symbol, Numeric, TrueClass, FalseClass
41
+ when NilClass, Symbol, Numeric, TrueClass, FalseClass, Method
41
42
  register_duplication(register, object, object)
42
43
 
43
44
  else
@@ -46,6 +47,13 @@ module Rack::App::Utils::DeepDup
46
47
  end
47
48
  end
48
49
 
50
+ def registrable?(object)
51
+ object.object_id
52
+ true
53
+ rescue NoMethodError
54
+ false
55
+ end
56
+
49
57
  def dup_array(register, object)
50
58
  duplication = dup_object(register, object)
51
59
  duplication.map! { |e| dup(register, e) }
@@ -73,16 +81,43 @@ module Rack::App::Utils::DeepDup
73
81
  end
74
82
 
75
83
  def dup_object(register, object)
76
- dup_instance_variables(register, object, register_duplication(register, object, object.dup))
84
+ dup_instance_variables(register, object, register_duplication(register, object, try_dup(object)))
77
85
  end
78
86
 
79
- def dup_instance_variables(register, object, duplicate)
87
+ def dup_instance_variables(register, object, duplication)
88
+ return duplication unless respond_to_instance_variables?(object)
89
+
80
90
  object.instance_variables.each do |instance_variable|
81
- value = object.instance_variable_get(instance_variable)
82
- duplicate.instance_variable_set(instance_variable, dup(register, value))
91
+ value = get_instance_variable(object, instance_variable)
92
+
93
+ set_instance_variable(duplication, instance_variable, dup(register, value))
83
94
  end
84
95
 
85
- return duplicate
96
+ return duplication
97
+ end
98
+
99
+ def get_instance_variable(object, instance_variable_name)
100
+ object.instance_variable_get(instance_variable_name)
101
+ rescue NoMethodError
102
+ object.instance_eval("#{instance_variable_name}")
103
+ end
104
+
105
+ def set_instance_variable(duplicate, instance_variable_name, value_to_set)
106
+ duplicate.instance_variable_set(instance_variable_name, value_to_set)
107
+ rescue NoMethodError
108
+ duplicate.instance_eval("#{instance_variable_name} = Marshal.load(#{Marshal.dump(value_to_set).inspect})")
109
+ end
110
+
111
+ def try_dup(object)
112
+ object.dup
113
+ rescue NoMethodError, TypeError
114
+ object
115
+ end
116
+
117
+ def respond_to_instance_variables?(object)
118
+ object.respond_to?(:instance_variables)
119
+ rescue NoMethodError
120
+ false
86
121
  end
87
122
 
88
123
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-app
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0.gamma
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-30 00:00:00.000000000 Z
11
+ date: 2016-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,7 +70,8 @@ description: Your next favourite rack based micro framework that is totally addi
70
70
  free! Have a cup of awesomeness with your to performance designed framework!
71
71
  email:
72
72
  - adamluzsi@gmail.com
73
- executables: []
73
+ executables:
74
+ - rack-app
74
75
  extensions: []
75
76
  extra_rdoc_files: []
76
77
  files:
@@ -86,13 +87,19 @@ files:
86
87
  - Rakefile
87
88
  - VERSION
88
89
  - Vagrantfile
90
+ - bin/rack-app
91
+ - exec/rack-app
89
92
  - lib/rack/app.rb
93
+ - lib/rack/app/cli.rb
94
+ - lib/rack/app/cli/command.rb
95
+ - lib/rack/app/cli/default_commands.rb
96
+ - lib/rack/app/cli/default_commands/list_commands.rb
97
+ - lib/rack/app/cli/runner.rb
90
98
  - lib/rack/app/constants.rb
91
99
  - lib/rack/app/endpoint.rb
92
100
  - lib/rack/app/endpoint/not_found.rb
93
101
  - lib/rack/app/error_handler.rb
94
102
  - lib/rack/app/extension.rb
95
- - lib/rack/app/extension/factory.rb
96
103
  - lib/rack/app/file_server.rb
97
104
  - lib/rack/app/instance_methods.rb
98
105
  - lib/rack/app/instance_methods/core.rb
@@ -109,6 +116,7 @@ files:
109
116
  - lib/rack/app/router/static.rb
110
117
  - lib/rack/app/serializer.rb
111
118
  - lib/rack/app/singleton_methods.rb
119
+ - lib/rack/app/singleton_methods/extensions.rb
112
120
  - lib/rack/app/singleton_methods/http_methods.rb
113
121
  - lib/rack/app/singleton_methods/inheritance.rb
114
122
  - lib/rack/app/singleton_methods/mounting.rb
@@ -137,9 +145,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
145
  version: '0'
138
146
  required_rubygems_version: !ruby/object:Gem::Requirement
139
147
  requirements:
140
- - - ">"
148
+ - - ">="
141
149
  - !ruby/object:Gem::Version
142
- version: 1.3.1
150
+ version: '0'
143
151
  requirements: []
144
152
  rubyforge_project:
145
153
  rubygems_version: 2.4.8
@@ -1,13 +0,0 @@
1
- module Rack::App::Extension::Factory
2
-
3
- extend self
4
-
5
- def all
6
- ObjectSpace.each_object(Class).select { |klass| klass < ::Rack::App::Extension }
7
- end
8
-
9
- def find_for(sym_name)
10
- return all.find{|extension_class| extension_class.names.include?(sym_name) }
11
- end
12
-
13
- end