pakyow-core 1.0.0.rc5 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/lib/pakyow/{environment/actions → actions}/dispatch.rb +0 -0
  4. data/lib/pakyow/{environment/actions → actions}/input_parser.rb +0 -0
  5. data/lib/pakyow/{environment/actions → actions}/logger.rb +0 -0
  6. data/lib/pakyow/{environment/actions → actions}/normalizer.rb +0 -0
  7. data/lib/pakyow/{app.rb → application.rb} +26 -26
  8. data/lib/pakyow/application/behavior/aspects.rb +51 -0
  9. data/lib/pakyow/application/behavior/endpoints.rb +43 -0
  10. data/lib/pakyow/application/behavior/frameworks.rb +47 -0
  11. data/lib/pakyow/application/behavior/helpers.rb +107 -0
  12. data/lib/pakyow/{environment → application}/behavior/initializers.rb +4 -4
  13. data/lib/pakyow/application/behavior/isolating.rb +71 -0
  14. data/lib/pakyow/application/behavior/operations.rb +49 -0
  15. data/lib/pakyow/application/behavior/pipeline.rb +60 -0
  16. data/lib/pakyow/application/behavior/plugins.rb +151 -0
  17. data/lib/pakyow/application/behavior/rescuing.rb +53 -0
  18. data/lib/pakyow/application/behavior/restarting.rb +72 -0
  19. data/lib/pakyow/application/behavior/sessions.rb +38 -0
  20. data/lib/pakyow/{behavior → application}/config.rb +1 -1
  21. data/lib/pakyow/{app → application}/connection.rb +4 -4
  22. data/lib/pakyow/{app → application}/connection/behavior/session.rb +1 -1
  23. data/lib/pakyow/{app → application}/connection/behavior/values.rb +1 -1
  24. data/lib/pakyow/{app → application}/connection/behavior/verifier.rb +1 -1
  25. data/lib/pakyow/{app → application}/connection/session/base.rb +1 -1
  26. data/lib/pakyow/{app → application}/connection/session/cookie.rb +2 -2
  27. data/lib/pakyow/application/helpers/app.rb +30 -0
  28. data/lib/pakyow/application/helpers/connection.rb +47 -0
  29. data/lib/pakyow/behavior/initializers.rb +3 -3
  30. data/lib/pakyow/behavior/input_parsing.rb +55 -0
  31. data/lib/pakyow/behavior/plugins.rb +3 -130
  32. data/lib/pakyow/behavior/restarting.rb +28 -46
  33. data/lib/pakyow/behavior/running.rb +127 -0
  34. data/lib/pakyow/behavior/silencing.rb +21 -0
  35. data/lib/pakyow/behavior/timezone.rb +19 -0
  36. data/lib/pakyow/behavior/verification.rb +1 -1
  37. data/lib/pakyow/behavior/verifier.rb +30 -0
  38. data/lib/pakyow/behavior/watching.rb +73 -0
  39. data/lib/pakyow/config.rb +168 -0
  40. data/lib/pakyow/environment.rb +31 -31
  41. data/lib/pakyow/helper.rb +9 -0
  42. data/lib/pakyow/loader.rb +7 -1
  43. data/lib/pakyow/plugin.rb +22 -21
  44. metadata +42 -42
  45. data/lib/pakyow/behavior/aspects.rb +0 -49
  46. data/lib/pakyow/behavior/endpoints.rb +0 -41
  47. data/lib/pakyow/behavior/frameworks.rb +0 -45
  48. data/lib/pakyow/behavior/helpers.rb +0 -108
  49. data/lib/pakyow/behavior/isolating.rb +0 -69
  50. data/lib/pakyow/behavior/operations.rb +0 -47
  51. data/lib/pakyow/behavior/pipeline.rb +0 -58
  52. data/lib/pakyow/behavior/rescuing.rb +0 -51
  53. data/lib/pakyow/behavior/sessions.rb +0 -36
  54. data/lib/pakyow/environment/behavior/config.rb +0 -172
  55. data/lib/pakyow/environment/behavior/input_parsing.rb +0 -57
  56. data/lib/pakyow/environment/behavior/plugins.rb +0 -23
  57. data/lib/pakyow/environment/behavior/restarting.rb +0 -54
  58. data/lib/pakyow/environment/behavior/running.rb +0 -129
  59. data/lib/pakyow/environment/behavior/silencing.rb +0 -23
  60. data/lib/pakyow/environment/behavior/timezone.rb +0 -21
  61. data/lib/pakyow/environment/behavior/verifier.rb +0 -32
  62. data/lib/pakyow/environment/behavior/watching.rb +0 -75
  63. data/lib/pakyow/helpers/app.rb +0 -28
  64. data/lib/pakyow/helpers/connection.rb +0 -45
@@ -3,7 +3,7 @@
3
3
  require "pakyow/support/extension"
4
4
 
5
5
  module Pakyow
6
- class App
6
+ class Application
7
7
  class Connection
8
8
  module Behavior
9
9
  module Session
@@ -4,7 +4,7 @@ require "pakyow/support/deep_dup"
4
4
  require "pakyow/support/extension"
5
5
 
6
6
  module Pakyow
7
- class App
7
+ class Application
8
8
  class Connection
9
9
  module Behavior
10
10
  module Values
@@ -4,7 +4,7 @@ require "pakyow/support/extension"
4
4
  require "pakyow/support/message_verifier"
5
5
 
6
6
  module Pakyow
7
- class App
7
+ class Application
8
8
  class Connection
9
9
  module Behavior
10
10
  module Verifier
@@ -3,7 +3,7 @@
3
3
  require "pakyow/support/indifferentize"
4
4
 
5
5
  module Pakyow
6
- class App
6
+ class Application
7
7
  class Connection
8
8
  module Session
9
9
  class Base < DelegateClass(Support::IndifferentHash)
@@ -5,10 +5,10 @@ require "base64"
5
5
  require "pakyow/support/indifferentize"
6
6
  require "pakyow/support/message_verifier"
7
7
 
8
- require "pakyow/app/connection/session/base"
8
+ require "pakyow/application/connection/session/base"
9
9
 
10
10
  module Pakyow
11
- class App
11
+ class Application
12
12
  class Connection
13
13
  module Session
14
14
  class Cookie < Base
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ module Pakyow
6
+ class Application
7
+ module Helpers
8
+ # Convenience methods for interacting with the app object.
9
+ #
10
+ module Application
11
+ extend Forwardable
12
+
13
+ attr_reader :app
14
+
15
+ # @!method config
16
+ # Delegates to {app}.
17
+ #
18
+ # @see Application#config
19
+ def_delegators :app, :config
20
+
21
+ # @!method path
22
+ # @return builds the path to a named route (see {Paths#path})
23
+ #
24
+ # @!method path_to
25
+ # @return builds the path to a route, following a trail of names (see {Paths#path_to})
26
+ def_delegators :"app.endpoints", :path, :path_to
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ module Pakyow
6
+ class Application
7
+ module Helpers
8
+ # Convenience methods for interacting with the connection object.
9
+ #
10
+ module Connection
11
+ extend Forwardable
12
+
13
+ attr_reader :connection
14
+
15
+ # @!method app
16
+ # Returns the current app.
17
+ #
18
+ # @see Application
19
+ # @!method logger
20
+ # Returns the logger.
21
+ #
22
+ # @see Request#logger
23
+ #
24
+ # @!method params
25
+ # Returns the request params.
26
+ #
27
+ # @see Request#params
28
+ #
29
+ # @!method session
30
+ # Returns the session.
31
+ #
32
+ # @see Request#session
33
+ #
34
+ # @!method :cookies
35
+ # Returns cookies.
36
+ #
37
+ # @see Request#:cookies
38
+ def_delegators :connection, :app, :logger, :params, :session, :cookies
39
+
40
+ # @!method operations
41
+ # Returns the operations lookup.
42
+ #
43
+ def_delegators :app, :operations
44
+ end
45
+ end
46
+ end
47
+ end
@@ -8,9 +8,9 @@ module Pakyow
8
8
  extend Support::Extension
9
9
 
10
10
  apply_extension do
11
- after "make" do
12
- Dir.glob(File.join(config.root, "config/initializers/application/**/*.rb")).each do |initializer|
13
- class_eval(File.read(initializer), initializer)
11
+ on "configure" do
12
+ Dir.glob(File.join(Pakyow.config.root, "config/initializers/environment/**/*.rb")).each do |initializer|
13
+ require initializer
14
14
  end
15
15
  end
16
16
  end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ require "pakyow/support/extension"
6
+ require "pakyow/support/indifferentize"
7
+
8
+ require "pakyow/connection/query_parser"
9
+ require "pakyow/connection/multipart_parser"
10
+
11
+ module Pakyow
12
+ module Behavior
13
+ module InputParsing
14
+ extend Support::Extension
15
+ using Support::Indifferentize
16
+
17
+ apply_extension do
18
+ class_state :input_parsers, default: {}
19
+
20
+ on "configure" do
21
+ Pakyow.parse_input "application/x-www-form-urlencoded" do |input, connection|
22
+ connection.params.parse(input.read)
23
+ end
24
+
25
+ Pakyow.parse_input "multipart/form-data" do |input, connection|
26
+ Connection::MultipartParser.new(
27
+ connection.params, boundary: connection.type_params[:boundary]
28
+ ).parse(input)
29
+ end
30
+
31
+ Pakyow.parse_input "application/json", rewindable: true do |input, connection|
32
+ values = JSON.parse(input.read)
33
+
34
+ if values.is_a?(Hash)
35
+ values.deep_indifferentize.each do |key, value|
36
+ connection.params.add(key, value)
37
+ end
38
+ end
39
+
40
+ values
41
+ end
42
+ end
43
+ end
44
+
45
+ class_methods do
46
+ def parse_input(type, rewindable: true, &block)
47
+ @input_parsers[type] = {
48
+ block: block,
49
+ rewindable: rewindable
50
+ }
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -1,146 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "pakyow/errors"
4
-
5
- require "pakyow/support/deep_dup"
6
3
  require "pakyow/support/extension"
7
4
 
8
- require "pakyow/plugin/lookup"
9
-
10
5
  module Pakyow
11
6
  module Behavior
12
7
  module Plugins
13
8
  extend Support::Extension
14
9
 
15
- using Support::DeepDup
16
-
17
- attr_reader :plugs
18
-
19
- def plug(name, instance = :default)
20
- @plugs.find { |plug|
21
- plug.class.plugin_name == name && plug.class.__object_name.namespace.parts.last == instance
22
- }
23
- end
24
-
25
10
  apply_extension do
26
- class_state :__plugs, default: [], inheritable: true
27
-
28
- # Create a dynamic helper that allows plugin helpers to be called in context of a specific plug.
29
- #
30
- on "initialize" do
31
- self.class.register_helper :passive, Module.new {
32
- Pakyow.plugins.keys.map.each do |plugin_name|
33
- define_method plugin_name do |plug = :default|
34
- app.plugs.send(plugin_name, plug).helper_caller(
35
- app.class.included_helper_context(self),
36
- @connection,
37
- self
38
- )
39
- end
40
- end
41
- }
42
- end
43
-
44
- # Setting priority to low gives the app a chance to do any pre-loading
45
- # that might affect how plugins are setup.
46
- #
47
- after "initialize", "load.plugins", priority: :low do
48
- @plugs = Plugin::Lookup.new([])
49
-
50
- self.class.__plugs.each do |plug|
51
- if self.class.includes_framework?(:presenter)
52
- require "pakyow/plugin/helpers/rendering"
53
- plug.register_helper :passive, Plugin::Helpers::Rendering
54
- end
55
-
56
- # Include frameworks from app.
57
- #
58
- plug.include_frameworks(
59
- *self.class.config.loaded_frameworks
60
- )
61
-
62
- # Copy settings from the app config.
63
- #
64
- plug.config.instance_variable_set(
65
- :@__settings, config.__settings.deep_dup.merge(plug.config.__settings)
66
- )
67
-
68
- # Copy defaults from the app config.
69
- #
70
- plug.config.instance_variable_set(
71
- :@__defaults, config.__defaults.deep_dup.merge(plug.config.__defaults)
72
- )
73
-
74
- # Copy groups from the app config.
75
- #
76
- plug.config.instance_variable_set(
77
- :@__groups, config.__groups.deep_dup.merge(plug.config.__groups)
78
- )
79
-
80
- # Override config values that require a specific value.
81
- #
82
- full_name = [plug.plugin_name]
83
- unless plug.__object_name.namespace.parts.last == :default
84
- full_name << plug.__object_name.namespace.parts.last
85
- end
86
-
87
- plug.config.name = full_name.join("_").to_sym
88
-
89
- # Create a dynamic helper that allows plugin helpers to be called in context of a specific plug.
90
- #
91
- plug.register_helper :passive, Module.new {
92
- Pakyow.plugins.keys.map.each do |plugin_name|
93
- define_method plugin_name do |instance_name = :default|
94
- app.parent.plugs.send(plugin_name, instance_name).helper_caller(
95
- app.class.included_helper_context(self),
96
- @connection,
97
- self
98
- )
99
- end
100
- end
101
- }
102
-
103
- # Finally, create the plugin instance.
104
- #
105
- @plugs << plug.new(self)
106
- end
107
-
108
- @plugs.finalize
109
- end
110
-
111
- after "boot" do
112
- @plugs.each(&:booted)
113
- end
11
+ class_state :plugins, default: {}
114
12
  end
115
13
 
116
14
  class_methods do
117
- attr_reader :__plugs
118
-
119
- def plug(plugin_name, at: "/", as: :default, &block)
120
- plugin_name = plugin_name.to_sym
121
-
122
- unless plugin = Pakyow.plugins[plugin_name]
123
- raise UnknownPlugin.new_with_message(
124
- plugin: plugin_name
125
- )
126
- end
127
-
128
- app = self
129
- plug = plugin.make(
130
- "plug",
131
- within: Support::ObjectNamespace.new(
132
- *__object_name.namespace.parts + [plugin_name, as]
133
- ),
134
- mount_path: at,
135
- ) do
136
- # Creates a connection subclass that other frameworks can safely extend.
137
- #
138
- isolate(app.isolated(:Connection))
139
-
140
- instance_exec(&block) if block
141
- end
142
-
143
- @__plugs << plug
15
+ def register_plugin(plugin_name, plugin_module)
16
+ @plugins[plugin_name] = plugin_module
144
17
  end
145
18
  end
146
19
  end
@@ -1,68 +1,50 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "fileutils"
4
-
5
- require "pakyow/support/silenceable"
6
- Pakyow::Support::Silenceable.silence_warnings do
7
- require "filewatcher"
8
- end
9
-
10
3
  require "pakyow/support/extension"
11
4
 
12
5
  module Pakyow
13
6
  module Behavior
14
- # Handles triggering restarts in the parent process.
15
- #
16
7
  module Restarting
17
8
  extend Support::Extension
18
9
 
19
10
  apply_extension do
20
- configurable :process do
21
- setting :watched_paths, []
22
- setting :excluded_paths, []
23
- setting :restartable, false
11
+ on "run" do
12
+ @respawn = false
24
13
 
25
- defaults :development do
26
- setting :restartable, true
14
+ # Other processes (e.g. apps) can touch this file to restart the server.
15
+ #
16
+ watch "./tmp/restart.txt" do
17
+ restart
27
18
  end
28
19
 
29
- defaults :prototype do
30
- setting :restartable, true
20
+ # Automatically bundle.
21
+ #
22
+ watch "./Gemfile" do
23
+ Bundler.with_clean_env do
24
+ Support::CLI::Runner.new(message: "Bundling").run("bundle install")
25
+ end
31
26
  end
32
- end
33
27
 
34
- after "initialize" do
35
- setup_for_restarting
36
- end
28
+ # Respawn when the bundle changes.
29
+ #
30
+ watch "./Gemfile.lock" do
31
+ respawn
32
+ end
37
33
 
38
- # Setting up for restarting even after the app fails to initialize lets
39
- # the developer fix the problem and let the server restart on its own.
40
- #
41
- after "rescue" do
42
- setup_for_restarting
34
+ # Respawn when something about the environment changes.
35
+ #
36
+ watch "#{Pakyow.config.environment_path}.rb" do
37
+ respawn
38
+ end
43
39
  end
44
40
  end
45
41
 
46
- private
47
-
48
- def setup_for_restarting
49
- if config.process.restartable
50
- config.process.watched_paths << File.join(config.src, "**/*.rb")
51
- config.process.watched_paths << File.join(config.lib, "**/*.rb")
52
-
53
- # FIXME: this doesn't need to be hardcoded, but instead determined
54
- # from the source location when registered with the environment
55
- config.process.watched_paths << "./config/application.rb"
56
-
57
- Thread.new do
58
- Filewatcher.new(
59
- config.process.watched_paths,
60
- exclude: config.process.excluded_paths
61
- ).watch do |_path, _event|
62
- FileUtils.mkdir_p "./tmp"
63
- FileUtils.touch "./tmp/restart.txt"
64
- end
65
- end
42
+ class_methods do
43
+ def respawn
44
+ # Set the respawn flag and stop the process manager.
45
+ # Pakyow will check the flag and respawn from the main thread.
46
+ #
47
+ @respawn = true; @process_manager.stop
66
48
  end
67
49
  end
68
50
  end