Class: RestBuilder::Builder
- Inherits:
-
Object
- Object
- RestBuilder::Builder
- Defined in:
- lib/rest-builder/builder.rb
Instance Attribute Summary collapse
- #default_engine ⇒ Object
-
#middles ⇒ Object
readonly
Returns the value of attribute middles.
Instance Method Summary collapse
-
#initialize(&block) ⇒ Builder
constructor
A new instance of Builder.
- #members ⇒ Object
- #run(engine) ⇒ Object
- #to_app(engine = @engine || default_engine) ⇒ Object
- #to_client(*attrs) ⇒ Object
- #use(middle, *args, &block) ⇒ Object
Constructor Details
#initialize(&block) ⇒ Builder
Returns a new instance of Builder.
24 25 26 27 28 |
# File 'lib/rest-builder/builder.rb', line 24 def initialize &block @engine = nil @middles ||= [] instance_eval(&block) if block_given? end |
Instance Attribute Details
#default_engine ⇒ Object
32 33 34 |
# File 'lib/rest-builder/builder.rb', line 32 def default_engine @default_engine ||= self.class.default_engine end |
#middles ⇒ Object (readonly)
Returns the value of attribute middles.
30 31 32 |
# File 'lib/rest-builder/builder.rb', line 30 def middles @middles end |
Instance Method Details
#members ⇒ Object
44 45 46 47 48 |
# File 'lib/rest-builder/builder.rb', line 44 def members middles.map{ |(middle, _, _)| middle.members if middle.respond_to?(:members) }.flatten.compact end |
#run(engine) ⇒ Object
40 41 42 |
# File 'lib/rest-builder/builder.rb', line 40 def run engine @engine = engine end |
#to_app(engine = @engine || default_engine) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rest-builder/builder.rb', line 50 def to_app engine=@engine || default_engine # === foldr m.new app middles middles.reverse.inject(engine.new){ |app, (middle, args, block)| begin middle.new(app, *partial_deep_copy(args), &block) rescue ArgumentError => e raise ArgumentError.new("#{middle}: #{e}") end } end |
#to_client(*attrs) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rest-builder/builder.rb', line 61 def to_client *attrs fields = (members + attrs + [:config_engine]).uniq struct = build_struct(fields) client = Class.new(struct) client.const_set('Struct', struct) class_methods = build_class_methods client.const_set('ClassMethods', class_methods) client.singleton_class.send(:include, class_methods) client.send(:include, Client) client.builder = self client.pool_size = 0 # default to no pool client.pool_idle_time = 60 # default to 60 seconds client.event_source_class = EventSource client.promises = [] client.mutex = Mutex.new client end |
#use(middle, *args, &block) ⇒ Object
36 37 38 |
# File 'lib/rest-builder/builder.rb', line 36 def use middle, *args, &block middles << [middle, args, block] end |