mobj 1.5.4 → 1.6.7

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 +188 -13
  2. metadata +2 -2
@@ -1,8 +1,72 @@
1
1
  module Mobj
2
2
 
3
+
4
+ #magic init
5
+ #class Object
6
+ # alias_method :orig_method_missing, :method_missing
7
+ #
8
+ # def method_missing(m, *a, &b)
9
+ # klass = begin
10
+ # (self.is_a?(Module) ? self : self.class).const_get(m)
11
+ # rescue NameError
12
+ # end
13
+ #
14
+ # return klass.send(:parens, *a, &b) if klass.respond_to? :parens
15
+ # orig_method_missing m, *a, &b
16
+ # end
17
+ #end
18
+
19
+ #class Object
20
+ # alias_method :orig_method_missing, :method_missing
21
+ #
22
+ # def method_missing(m, *a, &b)
23
+ # begin
24
+ # l = eval(m.to_s, binding_n(1))
25
+ # rescue NameError
26
+ # else
27
+ # return l.call(*a) if l.respond_to? :call
28
+ # end
29
+ # orig_method_missing m, *a, &b
30
+ # end
31
+ #end
32
+ #
33
+ #def call_a_lambda_with_parenths(val)
34
+ # l = lambda {|v| p v }
35
+ # l(val)
36
+ #end
37
+ #
38
+ #call_a_lambda_with_parenths(6)
39
+
40
+ class ::BasicObject
41
+ def class
42
+ klass = class << self; self end
43
+ klass.superclass
44
+ end
45
+ end
46
+
47
+ class ::Fixnum
48
+ def delimit(delim = ',')
49
+ to_s.split('').reverse.each_slice(3).to_a.map(&:join).join(delim).reverse
50
+ end
51
+ end
52
+
53
+ class ::Float
54
+ def delimit(delim = ',')
55
+ "#{to_i.delimit(delim)}.#{to_s.to_s[/\.(\d+)$/, 1]}"
56
+ end
57
+ end
58
+
59
+ class Forwarder < ::BasicObject
60
+ attr_accessor :root, :handler
61
+ def initialize(root = nil, &handler) @root, @handler = root, handler end
62
+ def method_missing(name, *args, &block) handler.call(name, *args, &block) end
63
+ end
64
+
3
65
  class ::Object
4
66
  alias responds_to? respond_to?
5
67
  def sym() respond_to?(:to_sym) ? to_sym : to_s.to_sym end
68
+ def s() respond_to?(:to_s) ? to_s : inspect end
69
+ def str() respond_to?(:to_str) ? to_str : to_s end
6
70
  def mroot() mparent.nil? || mparent == self ? self : mparent.mroot end
7
71
  def reparent() values.each { |v| v.mparent(self); v.reparent } if respond_to? :values end
8
72
  def mparent(rent = :mparent)
@@ -11,6 +75,74 @@ module Mobj
11
75
  end
12
76
  @mparent
13
77
  end
78
+ def attempt(value=:root)
79
+ Forwarder.new do |name, *args, &block|
80
+ if self.methods(true).include? name
81
+ self.__send__(name, *args, &block)
82
+ elsif value.is_a?(Proc)
83
+ value.call([name] + args, &block)
84
+ elsif value.is_a?(Hash) && value.key?(name)
85
+ value[name].when.is_a?(Proc).call(*args, &block)
86
+ else
87
+ value == :root ? self : value
88
+ end
89
+ end
90
+ end
91
+ alias_method :try?, :attempt
92
+ alias_method :do?, :attempt
93
+ alias_method :does?, :attempt
94
+ alias_method :if!, :attempt
95
+
96
+ def when
97
+ Forwarder.new do |name, *args, &block|
98
+ if self.methods.include?(name) && self.__send__(name, *args, &block)
99
+ thn = Forwarder.new do |name, *args, &block|
100
+ if name.sym == :then
101
+ thn
102
+ else
103
+ ret = self.__send__(name, *args, &block)
104
+ ret.define_singleton_method(:else) { Forwarder.new { ret } }
105
+ ret
106
+ end
107
+ end
108
+ else
109
+ Forwarder.new do |name|
110
+ if name.sym == :then
111
+ els = Forwarder.new do |name|
112
+ if name.sym == :else
113
+ Forwarder.new { |name, *args, &block| self.__send__(name, *args, &block) }
114
+ else
115
+ els
116
+ end
117
+ end
118
+ else
119
+ self
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+ alias_method :if?, :when
126
+ end
127
+
128
+ class ::NilClass
129
+ def attempt(value=true)
130
+ Forwarder.new do |name, *args, &block|
131
+ if self.methods(true).include? name
132
+ self.__send__(name, *args, &block)
133
+ elsif value.is_a?(Proc)
134
+ value.call([name] + args, &block)
135
+ elsif value.is_a?(Hash) && value.key?(name)
136
+ value[name].when.is_a?(Proc).call(*args, &block)
137
+ else
138
+ value
139
+ end
140
+ end
141
+ end
142
+ alias_method :try?, :attempt
143
+ alias_method :do?, :attempt
144
+ alias_method :does?, :attempt
145
+ alias_method :if!, :attempt
14
146
  end
15
147
 
16
148
  class ::Class
@@ -24,7 +156,13 @@ module Mobj
24
156
  alias contains? include?
25
157
 
26
158
  def values() self end
27
- def sequester(lim = 1) compact.size <= lim ? compact.first : self end
159
+ def sequester(crush = true)
160
+ if crush
161
+ compact.size <= 1 ? compact.first : self
162
+ else
163
+ size <= 1 ? first : self
164
+ end
165
+ end
28
166
  def return_first(&block)
29
167
  returned = nil
30
168
  each { |item| break if (returned = block.call(item)) }
@@ -32,6 +170,51 @@ module Mobj
32
170
  end
33
171
  end
34
172
 
173
+ module HashEx
174
+ def method_missing(name, *args, &block)
175
+ if name[-1] == '=' && args.size == 1
176
+ key = name[0...-1].sym
177
+ key = key.s if key?(key.s)
178
+ return self[key] = args.sequester
179
+ elsif name[-1] == '?'
180
+ key = name[0...-1].sym
181
+ return !!self[key, key.to_s]
182
+ elsif key?(name.sym) || key?(name.to_s)
183
+ return self[name.sym] || self[name.to_s]
184
+ end
185
+ super
186
+ end
187
+ end
188
+
189
+ class ::Hash
190
+ include HashEx
191
+
192
+ alias :mlookup :[]
193
+ alias :mdef :default
194
+ def [](*fkeys)
195
+ fkeys.map { |key| mlookup(key) || fetch(key.sym) { fetch(key.to_s) { mdef(key) } } }.sequester
196
+ end
197
+ end
198
+
199
+ module MatchEx
200
+ def to_hash
201
+ Hash[ names.map(&:sym).zip( captures ) ]
202
+ end
203
+
204
+ def method_missing(name, *args, &block)
205
+ if name[-1] == '?' && names.includes?(name[0...-1])
206
+ return to_hash[name[0...-1].sym]
207
+ elsif names.includes?(name.to_s)
208
+ return to_hash[name.sym]
209
+ end
210
+ super
211
+ end
212
+ end
213
+
214
+ class ::MatchData
215
+ include MatchEx
216
+ end
217
+
35
218
  class Token
36
219
  def initialize(type, *args)
37
220
  @type, @path, @options = type.to_sym, nil, {}
@@ -52,7 +235,7 @@ module Mobj
52
235
 
53
236
  def extract(obj, path)
54
237
  obj.reparent
55
- if path == :*
238
+ if path == :* || obj.nil?
56
239
  obj
57
240
  elsif obj.is_a?(Array)
58
241
  obj.map { |o| extract(o, path) }
@@ -109,6 +292,7 @@ module Mobj
109
292
 
110
293
  class ::String
111
294
  def ~@() "~#{self}" end
295
+ def -@() "^.#{self}" end
112
296
 
113
297
  def tokenize
114
298
  tokens = []
@@ -186,11 +370,6 @@ module Mobj
186
370
  val.mparent(self)
187
371
  keys.each { |key| store(key.sym, val) }
188
372
  end
189
-
190
- alias_method :lookup, :[]
191
- def [](*keys) keys.map { |key| self.lookup(key.sym) }.sequester end
192
-
193
- def method_missing(name, *args, &block) self.has_key?(name) ? self[name] : super(name, *args, &block) end
194
373
  end
195
374
 
196
375
  class CircleRay < Array
@@ -212,11 +391,7 @@ module Mobj
212
391
  end
213
392
 
214
393
  alias_method :lookup, :[]
215
- def [](*keys)
216
- keys.map do |key|
217
- self.lookup(key)
218
- end.sequester
219
- end
394
+ def [](*keys) keys.map { |key| self.lookup(key) }.sequester end
220
395
  end
221
-
222
396
  end
397
+
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: 1.5.4
4
+ version: 1.6.7
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-11-11 00:00:00.000000000 Z
12
+ date: 2012-11-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec