yokunai 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b9719cad3db06c3cd86fbb4da7f7379c1d551f4
4
- data.tar.gz: ac3956b12822a711439af3829e5498d25c144603
3
+ metadata.gz: 7a9227b92fa543f6f76e9253a517fa657c520ad3
4
+ data.tar.gz: cda1a80b5cc638eb84518314d70805eb3d7b0987
5
5
  SHA512:
6
- metadata.gz: 65a27940b072e18723b397dd3ba09358c2f762fd106248927fa7f9ea30b5be71472b2ee1953ed41df6adc5c18629978e7cf0407854f3112b1bab9dd235516b48
7
- data.tar.gz: eb6205f88e4041db98d9f7889d6d1be666b8703549e0947bfb15b155a227db2334520127514c180bfa4e73e370a378590e7e42e4ea80542f7b18cbef8c8dca31
6
+ metadata.gz: 313194f9aea2eb56212f2639dec921f345795fa4329aaa1fb16f4ddef9d40c75441853d54ec933b4f267732b24f369d0c24f695b65afb7561b3f431361aaf317
7
+ data.tar.gz: fd857f2a8d6185271ae2157db8b74f7bbd99223129f444fd83060d770e4a69ca98a53d77ce06be7adfd0374c635e973b70fe57b8c2c9a4536045598798578c5e
@@ -1,4 +1,4 @@
1
- # よくない
1
+ # よくない [![Build Status](https://ci.blackieops.com/buildStatus/icon?job=yokunai-tests)](https://ci.blackieops.com/job/yokunai-tests/)
2
2
 
3
3
  It's not very good.
4
4
 
@@ -32,15 +32,6 @@ class HomeController < Yokunai::AbstractController
32
32
  end
33
33
  ```
34
34
 
35
- Add some configuration for where you want to keep things:
36
-
37
- ```yml
38
- # config/development.yml
39
- ---
40
- template_dir: web/views
41
- asset_dir: web/assets
42
- ```
43
-
44
35
  Now just make a rackup config so we can run something:
45
36
 
46
37
  ```ruby
@@ -56,6 +47,26 @@ run Yokunai::Application.new(
56
47
 
57
48
  Then just `bundle exec rackup` and you're gold, Ponyboy.
58
49
 
50
+ ## Configuration
51
+
52
+ You can configure Yokunai via a YAML file. Create `config/development.yml`
53
+ (where `development` is whatever environment you want to configure). Anything in
54
+ this file will be populated into the `Yokunai::Config` helper, so you can fetch
55
+ things easily with `Yokunai::Config.get("my_key")` anywhere in your app.
56
+
57
+ For example,
58
+
59
+ ```yml
60
+ # config/development.yml
61
+ ---
62
+ template_dir: web/htmls
63
+ some_secret_key: abc123
64
+ ```
65
+
66
+ Some values have defaults to make setup easier, feel free to override any of
67
+ them. An exhaustive list can be found [in the Config class
68
+ itself][config_defaults].
69
+
59
70
  ## Assets
60
71
 
61
72
  You probably have frontend assets, if this is a web page. There's a controller
@@ -70,3 +81,35 @@ ROUTES = {
70
81
 
71
82
  Now assets will be served out of the directory you set as your `asset_dir` in
72
83
  the YAML config.
84
+
85
+ ## Boot Hooks
86
+
87
+ If your app needs to do something on-boot (maybe seed a cache, ping an
88
+ orchestrator, etc.) then you can create a "boot hook" to do that. Just create a
89
+ PORO class with a `.run` method, which will be invoked when the app boots.
90
+
91
+ ```ruby
92
+ # lib/my_app/some_hook.rb
93
+ module MyApp
94
+ class SomeHook
95
+ def self.run
96
+ puts "Got hooked"
97
+ end
98
+ end
99
+ end
100
+ ```
101
+
102
+ Then pass the class in when you boot the app:
103
+
104
+ ```ruby
105
+ # config.ru
106
+ [...]
107
+
108
+ run Yokunai::Application(
109
+ routes: ...,
110
+ base_dir: ...,
111
+ hooks: [MyApp::SomeHook]
112
+ )
113
+ ```
114
+
115
+ [config_defaults]: ./lib/yokunai/config.rb
@@ -3,8 +3,14 @@ require "mimemagic"
3
3
  require "ostruct"
4
4
  require "yaml"
5
5
 
6
- # Core
6
+ # Setup
7
+ require "yokunai/version"
7
8
  require "yokunai/config"
9
+
10
+ # Boot hooks
11
+ require "yokunai/hooks/config_populator"
12
+
13
+ # Core
8
14
  require "yokunai/application"
9
15
  require "yokunai/render_context"
10
16
  require "yokunai/template"
@@ -8,8 +8,7 @@ module Yokunai
8
8
  DEFAULT_HEADERS = {
9
9
  "Content-Type" => "text/html",
10
10
  "Server" => "yokunai/1.0"
11
- }
12
-
11
+ }.freeze
13
12
 
14
13
  # --------------------------------------------------------------------------
15
14
  # Instance Methods
@@ -17,13 +16,12 @@ module Yokunai
17
16
 
18
17
  # @param env [Rack::Env] The Rack ENV
19
18
  # @param captures [MatchData] The named captures from the route regex
20
- def initialize(env, captures=nil)
19
+ def initialize(env, captures = nil)
21
20
  @env = env
22
21
  @captures = captures
23
22
  @templates = Yokunai::Template.new
24
23
  end
25
24
 
26
-
27
25
  # --------------------------------------------------------------------------
28
26
  # Default HTTP method handlers
29
27
  # --------------------------------------------------------------------------
@@ -52,7 +50,7 @@ module Yokunai
52
50
  unsupported_method
53
51
  end
54
52
 
55
- private
53
+ private
56
54
 
57
55
  def respond(code: 200, headers: {}, body: "", template: nil, context: {})
58
56
  if template
@@ -75,5 +73,9 @@ module Yokunai
75
73
  [405, {}, ["Error 405: Method not supported on this resource.\n"]]
76
74
  end
77
75
 
76
+ def request
77
+ @request ||= Rack::Request.new(@env)
78
+ end
79
+
78
80
  end
79
81
  end
@@ -3,28 +3,31 @@ module Yokunai
3
3
  # request to the appropriate controller for handling.
4
4
  class Application
5
5
 
6
+ # List of default hooks we run on app boot to set up the framework itself.
7
+ DEFAULT_HOOKS = [
8
+ Yokunai::Hooks::ConfigPopulator
9
+ ].freeze
10
+
6
11
  # @param route_map [Hash] A hash with path regexes as keys, and hashes as values with the controller to send matching requests to.
7
12
  # @param base_dir [String] The absolute base directory to use for various lookups.
8
- def initialize(route_map:, base_dir:)
13
+ # @param hooks [Array] An optional list of classes which will have their class-level `run` method called when the app boots.
14
+ def initialize(route_map:, base_dir:, hooks: [])
9
15
  @routes = route_map
10
16
  Yokunai::Config.base_dir = base_dir
11
- Yokunai::Config.populate(ENV["YOKUNAI_ENV"] || "development")
17
+ (DEFAULT_HOOKS + hooks).each(&:run)
12
18
  end
13
19
 
14
20
  # Route a request to the correct controller based on the given data.
15
21
  #
16
- # @param path [String] the domain-relative path being requested.
17
22
  # @param env [Rack::Env] the full Rack environment
18
23
  # @return [Array] a Rack-compatible response array.
19
24
  def call(env)
20
25
  route = @routes.map do |exp, meta|
21
26
  next unless matches = env["PATH_INFO"].match(exp)
22
- meta.merge({captures: matches})
27
+ meta.merge(captures: matches)
23
28
  end.compact.first
24
29
 
25
- unless route
26
- return Yokunai::ErrorsController.new(env).not_found
27
- end
30
+ return Yokunai::ErrorsController.new(env).not_found unless route
28
31
 
29
32
  request_method = env["REQUEST_METHOD"]
30
33
  if route[:methods].include?(request_method)
@@ -3,6 +3,12 @@ module Yokunai
3
3
  # `config.yml` (or whatever name is passed to `populate`).
4
4
  class Config
5
5
 
6
+ # Any key in this hash can be overridden in your app's `config/*.yml`.
7
+ DEFAULT_CONFIGURATION = {
8
+ "template_dir" => "web/views",
9
+ "asset_dir" => "web/assets"
10
+ }.freeze
11
+
6
12
  # Set the base directory path.
7
13
  #
8
14
  # @param base_dir [String]
@@ -25,7 +31,10 @@ module Yokunai
25
31
  # will use the value of YOKUNAI_ENV.
26
32
  def self.populate(name = nil)
27
33
  name ||= ENV["YOKUNAI_ENV"]
28
- @@config = YAML.load_file(File.join(@@base_dir, "config", "#{name}.yml"))
34
+ config_path = File.join(@@base_dir, "config", "#{name}.yml")
35
+ user_config = File.exist?(config_path) ? YAML.load_file(config_path) : {}
36
+
37
+ @@config = DEFAULT_CONFIGURATION.merge(user_config)
29
38
  end
30
39
 
31
40
  # Get the value of a config option
@@ -0,0 +1,12 @@
1
+ module Yokunai
2
+ module Hooks
3
+ class ConfigPopulator
4
+
5
+ # Seed the configuration class on boot.
6
+ def self.run
7
+ Yokunai::Config.populate(ENV["YOKUNAI_ENV"] || "development")
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -9,7 +9,7 @@ module Yokunai
9
9
  mime = Yokunai::Mime.detect_from_path(asset_file)
10
10
  asset_body = File.read(asset_file)
11
11
 
12
- respond(body: asset_body, headers: {"Content-Type" => mime})
12
+ respond(body: asset_body, headers: { "Content-Type" => mime })
13
13
  else
14
14
  respond_error(:not_found)
15
15
  end
@@ -5,7 +5,8 @@ module Yokunai
5
5
  FALLBACK_EMPTY_LAYOUT = "<%= partial %>".freeze
6
6
 
7
7
  def initialize(template_path: nil)
8
- @template_path = template_path || File.join(Yokunai::Config.base_dir, Yokunai::Config.get("template_dir"))
8
+ template_dir = File.join(Yokunai::Config.base_dir, Yokunai::Config.get("template_dir"))
9
+ @template_path = template_path || template_dir
9
10
  @raw_layout = get_layout
10
11
  end
11
12
 
@@ -19,9 +20,7 @@ module Yokunai
19
20
  return nil unless exist?(template)
20
21
 
21
22
  path = File.join(@template_path, template + ".erb")
22
- layout_context = context.merge({
23
- partial: ERB.new(File.read(path)).result(Yokunai::RenderContext.new(context).get_binding)
24
- })
23
+ layout_context = context.merge(partial: ERB.new(File.read(path)).result(Yokunai::RenderContext.new(context).get_binding))
25
24
 
26
25
  ERB.new(@raw_layout).result(Yokunai::RenderContext.new(layout_context).get_binding)
27
26
  end
@@ -0,0 +1,3 @@
1
+ module Yokunai
2
+ VERSION = "0.1.2".freeze
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yokunai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Blackie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-10 00:00:00.000000000 Z
11
+ date: 2017-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -52,10 +52,12 @@ files:
52
52
  - lib/yokunai/application.rb
53
53
  - lib/yokunai/config.rb
54
54
  - lib/yokunai/errors_controller.rb
55
+ - lib/yokunai/hooks/config_populator.rb
55
56
  - lib/yokunai/mime.rb
56
57
  - lib/yokunai/render_context.rb
57
58
  - lib/yokunai/static_controller.rb
58
59
  - lib/yokunai/template.rb
60
+ - lib/yokunai/version.rb
59
61
  homepage: https://github.com/alexblackie/yokunai
60
62
  licenses:
61
63
  - BSD-3-Clause