This repository has been archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classmethods.rb
154 lines (115 loc) · 3.24 KB
/
classmethods.rb
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require 'forwardable'
module Structable
module ClassMethods
extend Forwardable
# @return [Integer]
def length
_members.length
end
alias_method :size, :length
# @yield [name]
# @yieldparam [Symbol] name
# @yieldreturn [self]
# @return [Enumerator]
def each_member(&block)
return to_enum(__method__) unless block_given?
_members.each_key(&block)
self
end
alias_method :each_key, :each_member
# @param [Boolean] include_aliased
# @return [Array<Symbol>]
def members(include_aliased=false)
include_aliased ? all_members : autonyms
end
# @return [Array<Symbol>]
def all_members
_members.keys
end
alias_method :keys, :members
# @param [Symbol, String] name
def has_member?(name)
_members.has_key? name
end
alias_method :member?, :has_member?
alias_method :has_key?, :has_member?
alias_method :key?, :has_member?
# @param [Symbol, String] aliased
# @param [Symbol, String] original
# @return [self] to be accessible via other name
def alias_member(aliased, original)
aliased, original = aliased.to_sym, original.to_sym
raise "Already defined the alias '#{aliased}'" if _members[aliased]
_members[aliased] = original
alias_method aliased, original
alias_method :"#{aliased}=", :"#{original}="
end
# @param [Symbol, String] name
# @return [Symbol] identifier symbol
def autonym(name)
name = name.to_sym
if autonyms.include?(name)
name
else
aliases[name] || raise(NameError)
end
end
# @return [Array<Symbol>] original keys
def autonyms
_members.reject{|k, v|v.kind_of? Symbol}.keys
end
# @return [Hash] aliased_key => original_key
def aliases
_members.select{|k, v|v.kind_of? Symbol}
end
# @group Constructor
def_delegator :self, :new, :[]
# @param [#each_pair, #keys] pairs ex: Hash, Struct
# @return [ClassMethods] new instance
def for_pairs(pairs)
unless pairs.respond_to?(:each_pair) and pairs.respond_to?(:keys)
raise TypeError, 'not pairs object'
end
raise ArgumentError, "different members" unless (pairs.keys - keys).empty?
new.tap {|instance|
pairs.each_pair do |name, value|
instance[name] = value
end
}
end
alias_method :load_pairs, :for_pairs
# @endgroup
# @return [self]
def freeze
close
super
end
private
# @param [Symbol, String] name
# @return [nil]
def member(name)
name = name.to_sym
if _members.has_key? name
raise NameError, "Already defined the member '#{name}'"
end
define_method name do
_get! name
end
define_method :"#{name}=" do |value|
_set! name, value
end
_members[name] = {}
nil
end
def _members
self::Structable_MEMBERS
end
def assert_member(name)
raise NameError, "Unknown member '#{name}'" unless member?(name)
end
def inherited(subclass)
super subclass
subclass.const_set :Structable_MEMBERS, _members.dup
end
end
end