Class: GH::Wrapper

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Case
Defined in:
lib/gh/wrapper.rb

Overview

Public: Simple base class for low level layers. Handy if you want to manipulate resources coming in from Github.

Examples

class IndifferentAccess
  def [](key) super.tap { |r| r.data.with_indifferent_access! } end
end

gh = IndifferentAccess.new
gh['users/rkh'][:name] # => "Konstantin Haase"

# easy to use in the low level stack
gh = Github.build do
  use GH::Cache
  use IndifferentAccess
  use GH::Normalizer
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend = nil, options = {}) ⇒ Wrapper

Public: Initialize a new Wrapper.

backend - layer to be wrapped options - config options



94
95
96
97
98
# File 'lib/gh/wrapper.rb', line 94

def initialize(backend = nil, options = {})
  backend, @options = normalize_options(backend, options)
  @options.each_pair { |key, value| public_send("#{key}=", value) if respond_to? "#{key}=" }
  setup(backend, @options)
end

Instance Attribute Details

#backendObject

Public: Get wrapped layer.



28
29
30
# File 'lib/gh/wrapper.rb', line 28

def backend
  @backend
end

#optionsObject (readonly)

Public: …



31
32
33
# File 'lib/gh/wrapper.rb', line 31

def options
  @options
end

Class Method Details

.[](key) ⇒ Object

Public: Retrieves resources from Github.



67
68
69
# File 'lib/gh/wrapper.rb', line 67

def self.[](key)
  new[key]
end

.wraps(klass = nil) ⇒ Object

Internal: Get/set default layer to wrap when creating a new instance.



85
86
87
88
# File 'lib/gh/wrapper.rb', line 85

def self.wraps(klass = nil)
  @wraps = klass if klass
  @wraps ||= Remote
end

Instance Method Details

#[](key) ⇒ Object

Public: Retrieves resources from Github.

By default, this method is delegated to the next layer on the stack and modify is called.



75
76
77
# File 'lib/gh/wrapper.rb', line 75

def [](key)
  generate_response key, fetch_resource(key)
end

#frontendObject

Internal: …



113
114
115
# File 'lib/gh/wrapper.rb', line 113

def frontend
  @frontend ? @frontend.frontend : self
end

#frontend=(value) ⇒ Object

Internal: …



108
109
110
# File 'lib/gh/wrapper.rb', line 108

def frontend=(value)
  @frontend = value
end

#generate_response(key, resource) ⇒ Object

Internal: …



80
81
82
# File 'lib/gh/wrapper.rb', line 80

def generate_response(key, resource)
  modify backend.generate_response(key, resource)
end

#inspectObject

Public: …



118
119
120
# File 'lib/gh/wrapper.rb', line 118

def inspect
  "#<#{self.class}: #{backend.inspect}>"
end

#load(data) ⇒ Object

Public: …



133
134
135
# File 'lib/gh/wrapper.rb', line 133

def load(data)
  modify backend.load(data)
end

#prefixed(key) ⇒ Object

Internal: …



123
124
125
# File 'lib/gh/wrapper.rb', line 123

def prefixed(key)
  prefix + "#" + identifier(key)
end

#resetObject

Public: …



128
129
130
# File 'lib/gh/wrapper.rb', line 128

def reset
  backend.reset if backend
end