mobj 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/mobj.rb +40 -19
  2. metadata +3 -4
@@ -8,6 +8,9 @@ module Mobj
8
8
  def null!() self end
9
9
  def nil!() self end
10
10
  def itself() self end
11
+ def alter(*args, &block) block[*[self, *args]] || self; end
12
+ alias_method :o!, :alter
13
+
11
14
  end
12
15
 
13
16
  class ::Fixnum
@@ -46,24 +49,26 @@ module Mobj
46
49
  self.__send__(name, *args, &block)
47
50
  elsif value.is_a?(Proc)
48
51
  value.call([name] + args, &block)
49
- elsif value.is_a?(Hash) && value.key?(name)
52
+ elsif value.is_a?(Hash) && value.ki?(name)
50
53
  value[name].when.is_a?(Proc).call(*args, &block)
51
54
  else
52
55
  value == :root ? self : value
53
56
  end
54
57
  end
55
58
  end
56
- alias_method :try!, :attempt
57
- alias_method :do?, :attempt
58
- alias_method :does?, :attempt
59
- alias_method :if!, :attempt
60
59
 
61
- def try?()
60
+ def try?(default=nil)
62
61
  Forwarder.new do |name, *args, &block|
63
- methods(true).include?(name) ? __send__(name, *args, &block) : nil.null!
62
+ if methods(true).include?(name)
63
+ __send__(name, *args, &block)
64
+ elsif is_a?(Hash) && ki?(name)
65
+ self[name]
66
+ end || default || nil.null!
64
67
  end
65
68
  end
66
69
 
70
+ alias_method :ifnil, :try?
71
+
67
72
  def when
68
73
  Forwarder.new do |name, *args, &block|
69
74
  if methods.include?(name) && __send__(name, *args, &block)
@@ -134,17 +139,13 @@ module Mobj
134
139
  self.__send__(name, *args, &block)
135
140
  elsif value.is_a?(Proc)
136
141
  value.call([name] + args, &block)
137
- elsif value.is_a?(Hash) && value.key?(name)
142
+ elsif value.is_a?(Hash) && value.ki?(name)
138
143
  value[name].when.is_a?(Proc).call(*args, &block)
139
144
  else
140
145
  value
141
146
  end
142
147
  end
143
148
  end
144
- alias_method :try!, :attempt
145
- alias_method :do?, :attempt
146
- alias_method :does?, :attempt
147
- alias_method :if!, :attempt
148
149
  end
149
150
 
150
151
  class ::Class
@@ -157,6 +158,9 @@ module Mobj
157
158
  alias includes? include?
158
159
  alias contains? include?
159
160
 
161
+ def unempty?() !empty? end
162
+ alias_method :notempty?, :unempty?
163
+
160
164
  def msum(initial = 0.0, op = :+, &block)
161
165
  map(&:to_f).inject(initial, block ? block : op)
162
166
  end
@@ -182,6 +186,14 @@ module Mobj
182
186
 
183
187
  module HashEx
184
188
 
189
+ def ki(name)
190
+ name.to_s[/(.*?)[?!=]?$/, 1]
191
+ end
192
+
193
+ def ki?(name)
194
+ [name.sym, name.to_s, ki(name).sym, ki(name).to_s].any?{ |k| key?(k) }
195
+ end
196
+
185
197
  def symvert(key_converter = :itself, value_converter = key_converter)
186
198
  each.with_object({}) do |(k,v),o|
187
199
  key = if key_converter.is_a?(Proc)
@@ -209,17 +221,26 @@ module Mobj
209
221
  end
210
222
 
211
223
  def method_missing(name, *args, &block)
212
- if name[-1] == '=' && args.size == 1
224
+ value = if name[-1] == '=' && args.size == 1
213
225
  key = name[0...-1].sym
214
226
  key = key.to_s if key?(key.to_s)
215
- return self[key] = args.sequester
227
+ self[key] = args.sequester
216
228
  elsif name[-1] == '?'
217
229
  key = name[0...-1].sym
218
- return !!self[key, key.to_s]
219
- elsif key?(name.sym) || key?(name.to_s)
220
- return self[name.sym] || self[name.to_s]
230
+ !!self[key, key.to_s]
231
+ elsif name[-1] == '!'
232
+ key = name[0...-1].sym
233
+ val = self[key.sym] || self[key.to_s]
234
+ if !val && (block || args.unempty?)
235
+ self[key] = val = (block ? block.call(*args) : args.sequester)
236
+ end
237
+ super unless val
238
+ else
239
+ self[name.sym] || self[name.to_s]
221
240
  end
222
- super
241
+ value ||= args.sequester unless args.empty?
242
+
243
+ return block ? block[value] : value
223
244
  end
224
245
  end
225
246
 
@@ -229,7 +250,7 @@ module Mobj
229
250
  alias :mlookup :[]
230
251
  alias :mdef :default
231
252
  def [](*fkeys)
232
- fkeys.map { |key| mlookup(key) || fetch(key.sym) { fetch(key.to_s) { mdef(key) } } }.sequester
253
+ fkeys.map { |key| mlookup(key) || fetch(key.sym) { fetch(key.to_s) { fetch(ki(key).sym) { fetch(ki(key).to_s) { mdef(key) }}}}}.sequester
233
254
  end
234
255
  end
235
256
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobj
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-24 00:00:00.000000000 Z
12
+ date: 2013-03-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -87,9 +87,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  version: '0'
88
88
  requirements: []
89
89
  rubyforge_project:
90
- rubygems_version: 1.8.24
90
+ rubygems_version: 1.8.23
91
91
  signing_key:
92
92
  specification_version: 3
93
93
  summary: Helpful utils and extentions
94
94
  test_files: []
95
- has_rdoc: