forked from piotrmurach/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.rb
More file actions
105 lines (93 loc) · 2.4 KB
/
config.rb
File metadata and controls
105 lines (93 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# encoding: utf-8
require_relative 'config/property'
require_relative 'config/property_set'
module Github
class API
# A base class for constructing api configuration
class Config
# Defines a property on an object's class or instance
#
# @example
# class Configuration < Api::Config
# property :adapter, default: :net_http
# property :user, required: true
# end
#
# @param [Symbol] name
# the name of a property
#
# @param [#to_hash] options
# the extra options
#
# @return [self]
#
# @api public
def self.property(name, options = {})
self.property_set << Property.new(name, options)
update_subclasses(name, options)
self
end
def self.update_subclasses(name, options)
if defined?(@subclasses) && @subclasses
@subclasses.each { |klass| klass.property(name, options) }
end
end
# Check if property is defined
#
# @param [Symbol] name
# the name to check
#
# @return [Boolean]
#
# @api public
def self.property?(name)
property_set.include?(name)
end
class << self
attr_reader :property_set
end
instance_variable_set("@property_set", PropertySet.new(self))
def self.inherited(descendant)
super
(@subclasses ||= Set.new) << descendant
descendant.instance_variable_set('@property_set',
PropertySet.new(descendant, self.property_set.properties.dup))
end
def initialize(&block)
super(&block)
end
def property_names
self.class.property_set.properties.map(&:name)
end
def self.property_names
property_set.properties.map(&:name)
end
# Fetach all the properties and their values
#
# @return [Hash[Symbol]]
#
# @api public
def fetch(value = nil)
if value
self.class.property_set[value]
else
self.class.property_set.to_hash
end
end
# Provide access to properties
#
# @example
# config.call do |config|
# config.adapter = :net_http
# end
#
# @return [self]
#
# @api private
def call(&block)
block.call(self) if block_given?
self
end
end # Config
end # Api
end # Github