Class: Github::API::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/github_api2/api/config.rb,
lib/github_api2/api/config/property.rb,
lib/github_api2/api/config/property_set.rb

Overview

A base class for constructing api configuration

Direct Known Subclasses

Configuration

Defined Under Namespace

Classes: Property, PropertySet

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Config

Returns a new instance of Config.



64
65
66
# File 'lib/github_api2/api/config.rb', line 64

def initialize(&block)
  super(&block)
end

Class Attribute Details

.property_setObject (readonly)

Returns the value of attribute property_set.



52
53
54
# File 'lib/github_api2/api/config.rb', line 52

def property_set
  @property_set
end

Class Method Details

.inherited(descendant) ⇒ Object



57
58
59
60
61
62
# File 'lib/github_api2/api/config.rb', line 57

def self.inherited(descendant)
  super
  (@subclasses ||= Set.new) << descendant
  descendant.instance_variable_set('@property_set',
    PropertySet.new(descendant, self.property_set.properties.dup))
end

.property(name, options = {}) ⇒ self

Defines a property on an object’s class or instance

Examples:

class Configuration < Api::Config
  property :adapter, default: :net_http
  property :user, required: true
end

Parameters:

  • name (Symbol)

    the name of a property

  • options (#to_hash) (defaults to: {})

    the extra options

Returns:

  • (self)


27
28
29
30
31
# File 'lib/github_api2/api/config.rb', line 27

def self.property(name, options = {})
  self.property_set << Property.new(name, options)
  update_subclasses(name, options)
  self
end

.property?(name) ⇒ Boolean

Check if property is defined

Parameters:

  • name (Symbol)

    the name to check

Returns:

  • (Boolean)


47
48
49
# File 'lib/github_api2/api/config.rb', line 47

def self.property?(name)
  property_set.include?(name)
end

.property_namesObject



72
73
74
# File 'lib/github_api2/api/config.rb', line 72

def self.property_names
  property_set.properties.map(&:name)
end

.update_subclasses(name, options) ⇒ Object



33
34
35
36
37
# File 'lib/github_api2/api/config.rb', line 33

def self.update_subclasses(name, options)
  if defined?(@subclasses) && @subclasses
    @subclasses.each { |klass| klass.property(name, options) }
  end
end

Instance Method Details

#call(&block) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Provide access to properties

Examples:

config.call do |config|
  config.adapter = :net_http
end

Returns:

  • (self)


99
100
101
102
# File 'lib/github_api2/api/config.rb', line 99

def call(&block)
  block.call(self) if block_given?
  self
end

#fetch(value = nil) ⇒ Hash[Symbol]

Fetch all the properties and their values

Returns:



81
82
83
84
85
86
87
# File 'lib/github_api2/api/config.rb', line 81

def fetch(value = nil)
  if value
    self.class.property_set[value]
  else
    self.class.property_set.to_hash
  end
end

#property_namesObject



68
69
70
# File 'lib/github_api2/api/config.rb', line 68

def property_names
  self.class.property_set.properties.map(&:name)
end