This repository was archived by the owner on Apr 24, 2018. It is now read-only.
File tree 5 files changed +82
-1
lines changed
5 files changed +82
-1
lines changed Original file line number Diff line number Diff line change 1
1
require 'rack'
2
+ require 'hashie/mash'
2
3
3
4
module Faces
4
5
module Strategies
Original file line number Diff line number Diff line change @@ -2,6 +2,12 @@ module Faces
2
2
module Strategies
3
3
class Default
4
4
include Faces ::Strategy
5
+
6
+ option :default_src
7
+
8
+ def src
9
+ options . default_src
10
+ end
5
11
end
6
12
end
7
13
end
Original file line number Diff line number Diff line change 1
1
module Faces
2
2
module Strategy
3
+ def self . included ( base )
4
+ base . extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def default_options
9
+ return @default_options if instance_variable_defined? ( :@default_options ) && @default_options
10
+ existing = superclass . respond_to? ( :default_options ) ? superclass . default_options : { }
11
+ @default_options = Faces ::Strategies ::Options . new ( existing )
12
+ end
13
+
14
+ def option ( name , value = nil )
15
+ default_options [ name ] = value
16
+ end
17
+
18
+ def args ( args = nil )
19
+ if args
20
+ @args = Array ( args )
21
+ return
22
+ end
23
+ existing = superclass . respond_to? ( :args ) ? superclass . args : [ ]
24
+ return ( instance_variable_defined? ( :@args ) && @args ) || existing
25
+ end
26
+ end
27
+
28
+ attr_reader :app , :options
29
+
30
+ def initialize ( app , *args , &block )
31
+ @app = app
32
+ @options = self . class . default_options . dup
33
+
34
+ options . deep_merge! ( args . pop ) if args . last . is_a? ( Hash )
35
+
36
+ self . class . args . each do |arg |
37
+ options [ arg ] = args . shift
38
+ end
39
+ end
40
+ end
41
+
42
+
43
+ module Strategies
44
+ class Options < Hashie ::Mash ; end
3
45
end
4
46
end
Original file line number Diff line number Diff line change 1
1
require 'spec_helper'
2
2
3
3
describe Faces ::Strategies ::Default do
4
+ let ( :app ) { Rack ::Builder . new do |b |
5
+ b . use Faces ::Strategies ::Default
6
+ b . run lambda { |env | [ 200 , { } , [ 'Not Found' ] ] }
7
+ end . to_app }
8
+
4
9
subject do
5
- Faces ::Strategies ::Default . new ( { } )
10
+ Faces ::Strategies ::Default . new ( app , { default_src : 'http://example.com' } )
11
+ end
12
+
13
+ describe '#src' do
14
+ it 'returns the default image source' do
15
+ expect ( subject . src ) . to eq ( 'http://example.com' )
16
+ end
6
17
end
7
18
end
Original file line number Diff line number Diff line change
1
+ require 'spec_helper'
2
+
3
+ describe Faces ::Strategy do
4
+ let ( :fresh_strategy ) { c = Class . new ; c . send :include , Faces ::Strategy ; c }
5
+
6
+ describe '.default_options' do
7
+ it 'returns the default options' do
8
+ fresh_strategy . option 'test' , 'correct'
9
+ expect ( fresh_strategy . default_options ) . to eq ( Faces ::Strategies ::Options . new ( { test : 'correct' } ) )
10
+ end
11
+ end
12
+
13
+ describe '.option' do
14
+ it 'sets an option for the strategy' do
15
+ expect ( fresh_strategy . default_options ) . to eq ( Faces ::Strategies ::Options . new ( ) )
16
+ fresh_strategy . option 'test' , 'correct'
17
+ expect ( fresh_strategy . default_options ) . to eq ( Faces ::Strategies ::Options . new ( { test : 'correct' } ) )
18
+ end
19
+ end
20
+
21
+ end
You can’t perform that action at this time.
0 commit comments