Skip to content

Commit

Permalink
fix again
Browse files Browse the repository at this point in the history
  • Loading branch information
dsh0416 committed Feb 9, 2024
1 parent 85b5f97 commit c76d03b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 63 deletions.
116 changes: 58 additions & 58 deletions lib/inori/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,71 +86,71 @@ def helper(name, &block)
define_method(name, &block)
end
end
end

# Constants of supported methods in route definition
METHODS = %w[ delete
get
head
post
put
connect
options
trace
copy
lock
mkcol
move
propfind
proppatch
unlock
report
mkactivity
checkout
merge
notify
subscribe
unsubscribe
patch
purge
websocket
eventsource].freeze

# Magics to fill DSL methods through dynamically class method definition
METHODS.each do |method|
define_singleton_method(method) do |*args, &block|
add_route(method.upcase.to_sym, args[0], block) # args[0]: path
# Constants of supported methods in route definition
METHODS = %w[ delete
get
head
post
put
connect
options
trace
copy
lock
mkcol
move
propfind
proppatch
unlock
report
mkactivity
checkout
merge
notify
subscribe
unsubscribe
patch
purge
websocket
eventsource].freeze

# Magics to fill DSL methods through dynamically class method definition
METHODS.each do |method|
define_singleton_method(method) do |*args, &block|
add_route(method.upcase.to_sym, args[0], block) # args[0]: path
end
end
end

singleton_class.send :alias_method, :ws, :websocket
singleton_class.send :alias_method, :es, :eventsource
singleton_class.send :alias_method, :ws, :websocket
singleton_class.send :alias_method, :es, :eventsource

private
private

def inherited(subclass)
super
def inherited(subclass)
super

subclass.class_initialize
end
subclass.class_initialize
end

# Implementation of route DSL
# @param [String] method HTTP method
# @param [String, Regexp] path path definition
# @param [Proc] block process to run when route matched
# @return [nil] nil
def add_route(method, path, block)
# Argument check
raise ArgumentError unless path.is_a? String

# Insert route to routes
route = Inori::Route.new(method, path, block)
route.middlewares = @scope_middlewares + @temp_middlewares
@routes[method] << route

# Clean up temp middleware
@temp_middlewares = []
nil
# Implementation of route DSL
# @param [String] method HTTP method
# @param [String, Regexp] path path definition
# @param [Proc] block process to run when route matched
# @return [nil] nil
def add_route(method, path, block)
# Argument check
raise ArgumentError unless path.is_a? String

# Insert route to routes
route = Inori::Route.new(method, path, block)
route.middlewares = @scope_middlewares + @temp_middlewares
@routes[method] << route

# Clean up temp middleware
@temp_middlewares = []
nil
end
end
end
end
6 changes: 3 additions & 3 deletions lib/inori/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
module Inori
# @return [String] inori environment
def self.env
ENV['INORI_ENV'] || 'development'
(ENV['INORI_ENV'] || 'development').to_sym
end
end

class String
# @return [TrueClass | FalseClass] if string is equal to production
def production?
self == 'production'
self == :production
end

# @return [TrueClass | FalseClass] if string is equal to development
def development?
self == 'development'
self == :development
end
end
4 changes: 2 additions & 2 deletions spec/inori/inori_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
RSpec.describe Inori do
describe 'env' do
it 'is development by default' do
expect(described_class.env).to eq('development')
expect(described_class.env).to eq(:development)
expect(described_class.env.development?).to be(true)
expect(described_class.env.production?).to be(false)
end

it 'is production after setting env' do
ENV['INORI_ENV'] = 'production'
expect(described_class.env).to eq('production')
expect(described_class.env).to eq(:production)
expect(described_class.env.development?).to be(false)
expect(described_class.env.production?).to be(true)
end
Expand Down

0 comments on commit c76d03b

Please sign in to comment.