Module: RestCore::Wrapper

Includes:
RestCore
Included in:
Builder, Cache
Defined in:
lib/rest-core/wrapper.rb

Defined Under Namespace

Modules: DefaultEngine

Constant Summary

Constant Summary

Constants included from RestCore

ASYNC, CLIENT, DRY, FAIL, HIJACK, LOG, PROMISE, REQUEST_HEADERS, REQUEST_METHOD, REQUEST_PATH, REQUEST_PAYLOAD, REQUEST_QUERY, REQUEST_URI, RESPONSE_BODY, RESPONSE_HEADERS, RESPONSE_KEY, RESPONSE_SOCKET, RESPONSE_STATUS, TIMER, Universal, VERSION

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from RestCore

eagerload, id

Instance Attribute Details

- (Object) default_engine



22
23
24
# File 'lib/rest-core/wrapper.rb', line 22

def default_engine
  @default_engine ||= self.class.default_engine
end

- (Object) middles (readonly)

Returns the value of attribute middles



20
21
22
# File 'lib/rest-core/wrapper.rb', line 20

def middles
  @middles
end

- (Object) wrapped (readonly)

Returns the value of attribute wrapped



20
21
22
# File 'lib/rest-core/wrapper.rb', line 20

def wrapped
  @wrapped
end

Class Method Details

+ (Object) included(mod)



13
14
15
16
17
18
# File 'lib/rest-core/wrapper.rb', line 13

def self.included mod
  mod.send(:extend, DefaultEngine)
  class << mod
    attr_writer :default_engine
  end
end

+ (Object) partial_deep_copy(obj)



64
65
66
67
68
69
70
71
# File 'lib/rest-core/wrapper.rb', line 64

def partial_deep_copy obj
  case obj
    when Array; obj.map{ |o| partial_deep_copy(o) }
    when Hash ; obj.inject({}){ |r, (k, v)| r[k] = partial_deep_copy(v); r }
    when Numeric, Symbol, TrueClass, FalseClass, NilClass; obj
    else begin obj.dup; rescue TypeError; obj; end
  end
end

Instance Method Details

- (Object) initialize(&block)



26
27
28
29
30
31
# File 'lib/rest-core/wrapper.rb', line 26

def initialize &block
  @engine    = nil
  @middles ||= []
  instance_eval(&block) if block_given?
  @wrapped ||= to_app
end

- (Object) members



41
42
43
44
45
46
47
48
49
50
# File 'lib/rest-core/wrapper.rb', line 41

def members
  middles.map{ |(middle, args, block)|
    if middle.public_method_defined?(:wrapped)
      # TODO: this is hacky... try to avoid calling new!
      middle.members + middle.new(Dry.new, *args, &block).members
    else
      middle.members
    end if middle.respond_to?(:members)
  }.flatten.compact
end

- (Object) run(engine)



37
38
39
# File 'lib/rest-core/wrapper.rb', line 37

def run engine
  @engine = engine
end

- (Object) to_app(engine = @engine || default_engine)



52
53
54
55
56
57
58
59
60
61
# File 'lib/rest-core/wrapper.rb', line 52

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

- (Object) use(middle, *args, &block)



33
34
35
# File 'lib/rest-core/wrapper.rb', line 33

def use middle, *args, &block
  middles << [middle, args, block]
end