Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for symbolic name reference in extensions
- Loading branch information
Showing
9 changed files
with
110 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Rack::App::Extension::Factory | ||
|
||
extend self | ||
|
||
def all | ||
ObjectSpace.each_object(Class).select { |klass| klass < ::Rack::App::Extension } | ||
end | ||
|
||
def find_for(sym_name) | ||
return all.find{|extension_class| extension_class.names.include?(sym_name) } | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,43 @@ | ||
require 'spec_helper' | ||
require_relative 'extension_spec/example-rack_app_extension' | ||
describe Rack::App::Extension do | ||
|
||
class ExampleRackAppExtension < Rack::App::Extension | ||
|
||
module ClassMethods | ||
|
||
def hello | ||
'hello world' | ||
end | ||
|
||
end | ||
|
||
module EndpointMethods | ||
|
||
def sup | ||
'all good thanks!' | ||
end | ||
require 'rack/app/test' | ||
include Rack::App::Test | ||
|
||
end | ||
{ | ||
'when class explicitly used' => Example::RackAppExtension, | ||
'when used with default generated symbol' => :rack_app_extension | ||
}.each do |context_message,extension_value| | ||
context context_message do | ||
|
||
include EndpointMethods | ||
rack_app do | ||
|
||
extend ClassMethods | ||
extensions extension_value | ||
|
||
on_inheritance do |parent, child| | ||
child.instance_variable_set(:@dog, 'bark') | ||
end | ||
get '/' do | ||
sup | ||
end | ||
|
||
end | ||
end | ||
|
||
class SampleAppForExtensionTest < Rack::App | ||
it { expect(get('/').body).to eq 'all good thanks!' } | ||
|
||
extensions ExampleRackAppExtension | ||
it { expect(Class.new(rack_app).hello).to eq 'world'} | ||
|
||
get '/' do | ||
sup | ||
end | ||
end | ||
|
||
end | ||
context 'when unsupported extension reference given' do | ||
|
||
it 'should raise an error' do | ||
unsupported_extension_reference = Object.new | ||
|
||
describe Rack::App::Extension do | ||
let(:instance) { described_class } | ||
|
||
require 'rack/app/test' | ||
include Rack::App::Test | ||
|
||
rack_app SampleAppForExtensionTest do | ||
|
||
get '/' do | ||
sup | ||
expect{ | ||
rack_app{ extensions unsupported_extension_reference } | ||
}.to raise_error("unsupported extension reference: #{unsupported_extension_reference.inspect}") | ||
end | ||
|
||
end | ||
|
||
it { expect(get('/').body).to eq 'all good thanks!' } | ||
|
||
it { expect(rack_app.instance_variable_get(:@dog)).to eq 'bark' } | ||
|
||
end |
33 changes: 33 additions & 0 deletions
33
spec/rack/app/extension_spec/example-rack_app_extension.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Example | ||
class RackAppExtension < Rack::App::Extension | ||
|
||
module ClassMethods | ||
|
||
def hello | ||
'hello world' | ||
end | ||
|
||
end | ||
|
||
module EndpointMethods | ||
|
||
def sup | ||
'all good thanks!' | ||
end | ||
|
||
end | ||
|
||
include EndpointMethods | ||
|
||
extend ClassMethods | ||
|
||
on_inheritance do |parent, child| | ||
|
||
def child.hello | ||
'world' | ||
end | ||
|
||
end | ||
|
||
end | ||
end |