opt_struct 0.8.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/opt_struct/class_methods.rb +23 -0
- data/lib/opt_struct/instance_methods.rb +40 -4
- data/opt_struct.gemspec +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 95ef4125935cd900bc9961dcc059a5b18b078073
|
|
4
|
+
data.tar.gz: afdf3250246a0f0d05cc1f2767f41a8aebdea3d3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d0554c00ebf9888e9dd29962892f2850632ea3b52bff837c80f7bcdd49ec819df2c1496326a56a61eb15d1bf269c96d7c9388a51d32fabe9500d7dead4cd3aff
|
|
7
|
+
data.tar.gz: fb206a2f54c3b8419686f3cab8b2cea8177670a51856fc3291b64fac2037ebc68e7b218d81a83f91e3f69ac673c0ae6b20f8441dbdece3def523aab6c0a0d813
|
|
@@ -88,6 +88,29 @@ module OptStruct
|
|
|
88
88
|
@expected_arguments ||= []
|
|
89
89
|
end
|
|
90
90
|
|
|
91
|
+
def init(meth = nil, &blk)
|
|
92
|
+
add_callback(:init, meth || blk)
|
|
93
|
+
end
|
|
94
|
+
alias_method :after_init, :init
|
|
95
|
+
|
|
96
|
+
def before_init(meth = nil, &blk)
|
|
97
|
+
add_callback(:before_init, meth || blk)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def around_init(meth = nil, &blk)
|
|
101
|
+
add_callback(:around_init, meth || blk)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def add_callback(name, callback)
|
|
105
|
+
@_callbacks ||= {}
|
|
106
|
+
@_callbacks[name] ||= []
|
|
107
|
+
@_callbacks[name] << callback
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def all_callbacks
|
|
111
|
+
@_callbacks
|
|
112
|
+
end
|
|
113
|
+
|
|
91
114
|
private
|
|
92
115
|
|
|
93
116
|
RESERVED_WORDS = %i(class defaults options fetch check_required_args check_required_keys)
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
module OptStruct
|
|
2
2
|
module InstanceMethods
|
|
3
3
|
def initialize(*arguments, **options)
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
with_init_callbacks do
|
|
5
|
+
@arguments = arguments
|
|
6
|
+
@options = options
|
|
7
|
+
check_required_args
|
|
8
|
+
check_required_keys
|
|
9
|
+
end
|
|
8
10
|
end
|
|
9
11
|
|
|
10
12
|
def fetch(*a, &b)
|
|
@@ -37,5 +39,39 @@ module OptStruct
|
|
|
37
39
|
end
|
|
38
40
|
end
|
|
39
41
|
end
|
|
42
|
+
|
|
43
|
+
def with_init_callbacks(&init_block)
|
|
44
|
+
callbacks = self.class.all_callbacks
|
|
45
|
+
return yield if callbacks.nil? || callbacks.empty?
|
|
46
|
+
|
|
47
|
+
around, before, after = [:around_init, :before_init, :init].map do |type|
|
|
48
|
+
callbacks.fetch(type) { [] }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
if around.any?
|
|
52
|
+
init = proc { run_befores_and_afters(before, after, &init_block) }
|
|
53
|
+
init = around.reduce(init) do |chain, callback|
|
|
54
|
+
proc { run_callback(callback, &chain) }
|
|
55
|
+
end
|
|
56
|
+
instance_exec(&init)
|
|
57
|
+
else
|
|
58
|
+
run_befores_and_afters(before, after, &init_block)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def run_befores_and_afters(before, after)
|
|
63
|
+
before.each { |cb| run_callback(cb) }
|
|
64
|
+
yield
|
|
65
|
+
after.each { |cb| run_callback(cb) }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def run_callback(callback, &to_yield)
|
|
69
|
+
case callback
|
|
70
|
+
when Symbol, String
|
|
71
|
+
send(callback, &to_yield)
|
|
72
|
+
when Proc
|
|
73
|
+
instance_exec(to_yield, &callback)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
40
76
|
end
|
|
41
77
|
end
|
data/opt_struct.gemspec
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: opt_struct
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.8.
|
|
4
|
+
version: 0.8.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Carl Zulauf
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2017-
|
|
11
|
+
date: 2017-11-11 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -85,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
85
85
|
version: '0'
|
|
86
86
|
requirements: []
|
|
87
87
|
rubyforge_project:
|
|
88
|
-
rubygems_version: 2.6.
|
|
88
|
+
rubygems_version: 2.6.13
|
|
89
89
|
signing_key:
|
|
90
90
|
specification_version: 4
|
|
91
91
|
summary: The Option Struct
|