mobj 3.1.0 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NTBkYzM1ZjkzNzcwYTJkM2E5YTJhMjcxNGYzMjllODdmZGUwMTUwOA==
4
+ OTI0OTdjNzQ4Y2E5NTE4YzdlOGI5YTc4N2UwMDY4MzM0MWI2ZGJhMA==
5
5
  data.tar.gz: !binary |-
6
- YjdjZmI4MTIwOTRkNDc3YmY3ODY3MmRjYWIzNzE5ODcwNDE0MDVjZQ==
6
+ ZTcwOWYzYTY0MGEyOGQ0NDM2YjY0YzA1NjI3Mjc1OWM5NTQzZTZjMQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NDdkMzFmZjNmZWQ1YzBiODM3YjUxYTM0MjVmYjAwNzNjNDY2N2RhMTE4NmM5
10
- MjJhNGJlM2EzNWI5MmQ0NWZiNjY4NmVhYjhlOGYwYzBkMmE4YzgzZTVlNjc1
11
- NWUxYTk2ZGU1YTVhZWY2NTBlZGZmNmNhNTU5MDhkMmRiMGQ1M2Q=
9
+ NWQ0MjJlMjkwOTc4MmIyNzZmNmJkMGQwMWIxOTMyZmFlMGQwNDllYjIyOTNl
10
+ NGNiNjg2NmI0ODNmMTEwYWYwNjAyZGE1OWU1ZGE5N2EyZTQ0ZWFmYzcxZTRk
11
+ YTBmZWM2MGM1NDkzOGMwYWIzMGZkNTUxNzcwYzg5ZDlkZGI3YTM=
12
12
  data.tar.gz: !binary |-
13
- NmU5NzgzODkxOGVkYmI5NDllMzJlMjdhZTUxZjBmNDhhMzNjYzg2M2E4OTNk
14
- Nzc1OTkxNmQ2N2M2N2RjYTI4MmRiODUwNmYzNTBhNjk0YzhlZDI4ODFkYzU5
15
- MDRkMjEwZjVlMzI2Y2Q2Mjg4YjY4ZGQwNjU2ZGEyODZiOTIyNTE=
13
+ ZjYwZjEyNDY3NzEwNDE1NWMwYmI5NWViZmFmNmQ3ZjRlZDdlZGVlNThlNGRh
14
+ NWNkZjNhYWEwMzQ0MGIzMjE2ZDMxMzk2ODVlMzYwMzcwMzI3ODYyNzVlMjY4
15
+ NThiNWYzOGU3ZTBmZDQ1MDJmOGExMGE5MzRmMTZkMzA1NjE2ZDE=
data/README.md CHANGED
@@ -127,7 +127,7 @@ foo.compact.size == 1 ? foo.compact.first : foo.compact
127
127
  ... becomes ...
128
128
 
129
129
  ```ruby
130
- foo.sequester
130
+ foo.sequester!
131
131
  ```
132
132
 
133
133
  Hash utils:
@@ -1,14 +1,27 @@
1
1
  module Mobj
2
2
 
3
3
  class ::Array
4
+
4
5
  alias_method :includes?, :include?
5
6
  alias_method :contains?, :include?
6
7
 
7
- def unempty?()
8
- !empty?
8
+ alias_method :mt?, :empty?
9
+ def filled?() !mt? end
10
+ alias_method :notempty?, :filled?
11
+ alias_method :full?, :filled?
12
+ alias_method :unempty?, :filled?
13
+
14
+ def ny?(method, *args)
15
+ any? { |o| o.send(method, *args) }
9
16
  end
10
17
 
11
- alias_method :notempty?, :unempty?
18
+ def no?(method, *args)
19
+ none? { |o| o.send(method, *args) }
20
+ end
21
+
22
+ def ll?(method, *args)
23
+ all? { |o| o.send(method, *args) }
24
+ end
12
25
 
13
26
  def msum(initial = 0.0, op = :+, &block)
14
27
  if block
@@ -31,7 +44,7 @@ module Mobj
31
44
  self
32
45
  end
33
46
 
34
- def sequester(crush = true)
47
+ def sequester!(crush = true)
35
48
  if crush
36
49
  compact.size <= 1 ? compact.first : self
37
50
  else
@@ -39,6 +52,10 @@ module Mobj
39
52
  end
40
53
  end
41
54
 
55
+ def realize!
56
+ empty? ? nil : self
57
+ end
58
+
42
59
  def return_first(&block)
43
60
  returned = nil
44
61
  each { |item| break if (returned = block.call(item)) }
@@ -46,15 +63,16 @@ module Mobj
46
63
  end
47
64
 
48
65
  alias_method :earliest, :return_first
66
+ alias_method :first!, :return_first
49
67
 
50
68
  def msym() map(&:sym) end
51
69
 
52
70
  def apply(to)
53
71
  map do |m|
54
- if m.is_a?(Symbol)
72
+ if m.m?
55
73
  to.send(m)
56
74
  else
57
- m.to_s.walk(to)
75
+ m.s!.walk(to)
58
76
  end
59
77
  end
60
78
  end
@@ -8,32 +8,14 @@ module Mobj
8
8
  klass.superclass
9
9
  end
10
10
 
11
- def null!(*)
12
- self
13
- end
14
-
15
- def nil!(*)
16
- self
17
- end
11
+ def itself() self end
12
+ alias_method :self!, :itself
18
13
 
19
- def itself()
14
+ def p!(method=:puts)
15
+ send(method, self)
20
16
  self
21
17
  end
22
18
 
23
- def wrap(*args, &block)
24
- instance_exec(*args, &block)
25
- end
26
-
27
- alias_method :alter, :wrap
28
-
29
- def tru?(t=true, _=nil, &block)
30
- block ? instance_exec(t, &block) : t
31
- end
32
-
33
- def fals?(*)
34
- nil
35
- end
36
-
37
19
  end
38
20
 
39
21
 
@@ -19,6 +19,11 @@ module Mobj
19
19
  end
20
20
 
21
21
  def iff?(_=nil)
22
+ nil
23
+ end
24
+
25
+ def iffn?(value = nil, &block)
26
+ block ? instance_exec(value, &block) : value
22
27
  end
23
28
 
24
29
  end
@@ -4,6 +4,13 @@ module Mobj
4
4
 
5
5
  module HashEx
6
6
 
7
+ def filled?() !mt? end
8
+
9
+ alias_method :notempty?, :filled?
10
+ alias_method :noempty?, :filled?
11
+ alias_method :full?, :filled?
12
+ alias_method :unempty?, :filled?
13
+
7
14
  def apply!(obj)
8
15
  map do |key, value|
9
16
  obj.send(key, *value)
@@ -48,7 +55,7 @@ module Mobj
48
55
  value = if name[-1] == '=' && args.size == 1
49
56
  key = name[0...-1].sym
50
57
  key = key.to_s if key?(key.to_s)
51
- self[key] = args.sequester
58
+ self[key] = args.sequester!
52
59
  elsif name[-1] == '?'
53
60
  key = name[0...-1].sym
54
61
  !!self[key, key.to_s]
@@ -56,13 +63,13 @@ module Mobj
56
63
  key = name[0...-1].sym
57
64
  val = self[key.sym] || self[key.to_s]
58
65
  if !val && (block || args.unempty?)
59
- self[key] = val = (block ? block.call(*args) : args.sequester)
66
+ self[key] = val = (block ? block.call(*args) : args.sequester!)
60
67
  end
61
68
  super unless val
62
69
  else
63
70
  self[name.sym] || self[name.to_s]
64
71
  end
65
- value ||= args.sequester unless args.empty?
72
+ value ||= args.sequester! unless args.empty?
66
73
 
67
74
  return block ? block[value] : value
68
75
  end
@@ -75,12 +82,14 @@ module Mobj
75
82
  class ::Hash
76
83
  include HashEx
77
84
 
85
+ alias_method :mt?, :empty?
86
+
78
87
  alias_method :mlookup, :[]
79
88
  alias_method :mdef, :default
80
89
  alias_method :mfetch, :fetch
81
90
 
82
91
  def [](*fkeys)
83
- fkeys.map { |key| mlookup(key) || mfetch(key.sym) { mfetch(key.to_s) { mfetch(ki(key).sym) { mfetch(ki(key).to_s) { mdef(key) } } } } }.sequester
92
+ fkeys.map { |key| mlookup(key) || mfetch(key.sym) { mfetch(key.to_s) { mfetch(ki(key).sym) { mfetch(ki(key).to_s) { mdef(key) } } } } }.sequester!
84
93
  end
85
94
 
86
95
  def sym(recurse=false)
@@ -27,6 +27,11 @@ module Mobj
27
27
  end
28
28
 
29
29
  def iff?(_=nil)
30
+ self
31
+ end
32
+
33
+ def iffn?(value = nil, &block)
34
+ block ? instance_exec(value, &block) : value
30
35
  end
31
36
 
32
37
  def null?(*)
@@ -1,14 +1,80 @@
1
1
  module Mobj
2
2
 
3
3
  class ::Object
4
- alias_method :responds_to?, :respond_to?
4
+
5
+ def null!(*) self end
6
+ def nil!(*) self end
7
+
8
+ def wrap!(*args, &block)
9
+ block ? instance_exec(*args, &block) : args.sequester!
10
+ end
11
+
12
+ alias_method :alter!, :wrap!
13
+ alias_method :be!, :wrap!
14
+
15
+ alias_method :with!, :tap
16
+
17
+ def tru?(t=true, _=nil, &block) block ? instance_exec(t, &block) : t end
18
+ def fals?(*) nil end
19
+
20
+ def iff?(value = nil, &block) block ? instance_exec(value, &block) : value end
21
+ def iffn?(_=nil) nil end
22
+
23
+ def responds_to?(*any)
24
+ any.flatten.select { |method| respond_to?(method) }.realize!
25
+ end
26
+
27
+ def responds_to_all?(*all)
28
+ responds_to?(all) == all
29
+ end
30
+
31
+ def a?(*kls)
32
+ kls.when.mt?.be!([Array]).any? { |k| is_a? k }
33
+ end
34
+
35
+ def p?() a? Proc end
36
+ def m?() a? Symbol end
37
+ def s?() a? String end
38
+ def i?() a? Fixnum end
39
+ def f?() a? Float end
40
+ def n?() a? Fixnum, Float end
41
+
42
+ def h?() a? Hash end
43
+ def c?() a? Array, Hash end
44
+
45
+ def x?() a? NilClass, FalseClass end
46
+
47
+ def z0?() respond_to?(:zero?) ? zero? : f!.zero? end
48
+
49
+ def un?()
50
+ x? || (s? && s !~ /\S/) || (c? && mt?) || (n? && z0?)
51
+ end
52
+
53
+ def o?() !un? end
5
54
 
6
55
  def i!() to_s.scan(/[\d\.]+/).join.to_i end #xxx strip out junk?
7
56
  def f!() to_s.scan(/[\d\.]+/).join.to_f end
8
- def z0?() respond_to?(:zero?) ? zero? : f!.zero? end
57
+ def s!() to_s end
9
58
  def zeno!() z0? ? 1.0 : self end
10
59
 
11
- alias_method :n?, :nil?
60
+ def u!(*args)
61
+ if a?
62
+ each { |i| i.u!(*args) }
63
+ else
64
+ if responds_to_all? :assign_attributes, :save
65
+ args.select(&:h?).each do |arg|
66
+ assign_attributes(arg)
67
+ end
68
+ save(validate:false)
69
+ elsif responds_to? :update_attribute
70
+ args.select(&:h?).each do |arg|
71
+ arg.each do |k, v|
72
+ update_attribute(k, v)
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
12
78
 
13
79
  def rand?
14
80
  rand(1000000).odd?
@@ -68,8 +134,9 @@ module Mobj
68
134
  alias_method :ifnil, :try?
69
135
 
70
136
  def when
137
+ iam = self
71
138
  Forwarder.new do |name, *args, &block|
72
- if methods.include?(name) && __send__(name, *args, &block)
139
+ if (iam.respond_to?(name) || iam.methods.include?(name)) && (got = iam.__send__(name, *args, &block))
73
140
  thn = Forwarder.new do |name, *args, &block|
74
141
  if name.sym == :then
75
142
  thn
@@ -80,7 +147,7 @@ module Mobj
80
147
  end
81
148
  end
82
149
  else
83
- Forwarder.new do |name|
150
+ ret = Forwarder.new do |name, *args, &block|
84
151
  if name.sym == :then
85
152
  els = Forwarder.new do |name|
86
153
  if name.sym == :else
@@ -90,7 +157,7 @@ module Mobj
90
157
  end
91
158
  end
92
159
  else
93
- self
160
+ iam
94
161
  end
95
162
  end
96
163
  end
@@ -99,10 +166,6 @@ module Mobj
99
166
 
100
167
  alias_method :if?, :when
101
168
 
102
- def iff?(value = nil, &block)
103
- block ? instance_exec(value, &block) : value
104
- end
105
-
106
169
  end
107
170
 
108
171
  end
@@ -30,6 +30,8 @@ module Mobj
30
30
 
31
31
  class ::String
32
32
 
33
+ alias_method :mt?, :empty?
34
+
33
35
  def cfmt
34
36
  colors = ANSI.colors.keys.join
35
37
  opts = ANSI.options.keys.join
@@ -9,6 +9,8 @@ module Mobj
9
9
  attr_accessor :root, :handler
10
10
  def initialize(root = nil, &handler) @root, @handler = root, handler end
11
11
  def method_missing(name, *args, &block) handler.call(name, *args, &block) end
12
+ def to_ary() [root, handler] end
13
+ def inspect() { class:self.class, root:root, handler:handler}.inspect end
12
14
  end
13
15
 
14
16
  class Token
@@ -16,15 +18,15 @@ module Mobj
16
18
  @type, @path, @options = type.to_sym, nil, {}
17
19
  tokens = []
18
20
  args.each do |arg|
19
- if arg.is_a? Hash
21
+ if arg.h?
20
22
  @options.merge!(arg)
21
- elsif arg.is_a? String
23
+ elsif arg.s?
22
24
  tokens << arg.sym
23
25
  else
24
26
  tokens << arg
25
27
  end
26
28
  end
27
- @path = tokens.sequester
29
+ @path = tokens.sequester!
28
30
  end
29
31
 
30
32
  def to_s() "#{@type.to_s.upcase}(#@path#{ " => #@options" unless @options.empty?})" end
@@ -33,18 +35,18 @@ module Mobj
33
35
  obj.__mobj__reparent
34
36
  if path == :* || obj.nil?
35
37
  obj
36
- elsif obj.is_a?(Array)
38
+ elsif obj.a?
37
39
  if path[0] == '*' && obj.respond_to?(path[1..-1].sym)
38
40
  obj.__send__(path[1..-1].sym)
39
41
  else
40
42
  obj.map { |o| extract(o, path) }
41
43
  end
42
- elsif path.is_a?(Array)
44
+ elsif path.a?
43
45
  path.map { |pth| obj[pth.sym] }
44
46
  elsif path[0] == '*' && obj.respond_to?(path[1..-1].sym)
45
47
  obj.__send__(path[1..-1].sym)
46
48
  elsif obj.respond_to?(:[]) && (obj.is_a?(Hash) || path.to_s =~ /[0-9.-]+/)
47
- if obj.is_a?(Hash)
49
+ if obj.h?
48
50
  obj[path.sym]
49
51
  else
50
52
  obj[path.to_s.to_i] if path.to_s =~ /^\d+$/
@@ -75,7 +77,7 @@ module Mobj
75
77
  else
76
78
  named = m.to_h.invert
77
79
  name = if named.empty?
78
- m.captures.empty? ? key : m.captures.sequester
80
+ m.captures.empty? ? key : m.captures.sequester!
79
81
  else
80
82
  named.find { |k, v| !k.nil? }.attempt(key).last
81
83
  end
@@ -87,7 +89,7 @@ module Mobj
87
89
  end
88
90
 
89
91
  def walk(obj, root = obj)
90
- obj, root = Circle.wrap(obj), Circle.wrap(root)
92
+ obj, root = Circle.wrap!(obj), Circle.wrap!(root)
91
93
  val = case @type
92
94
  when :literal
93
95
  @path.to_s
@@ -135,16 +137,16 @@ module Mobj
135
137
  end
136
138
 
137
139
  class Circle
138
- def self.wrap(wrapped)
140
+ def self.wrap!(wrapped)
139
141
  return wrapped if wrapped.is_a?(CircleHash) || wrapped.is_a?(CircleRay)
140
142
 
141
143
  if wrapped.is_a?(Array)
142
144
  circle = CircleRay.new
143
- wrapped.each_with_index { |item, i| circle[i] = wrap(item) }
145
+ wrapped.each_with_index { |item, i| circle[i] = wrap!(item) }
144
146
  circle
145
147
  elsif wrapped.is_a?(Hash)
146
148
  circle = CircleHash.new
147
- wrapped.each_pair { |key, val| circle[key] = wrap(val) }
149
+ wrapped.each_pair { |key, val| circle[key] = wrap!(val) }
148
150
  circle
149
151
  else
150
152
  wrapped
@@ -190,7 +192,7 @@ module Mobj
190
192
  end
191
193
 
192
194
  alias_method :lookup, :[]
193
- def [](*keys) keys.map { |key| self.lookup(key) }.sequester end
195
+ def [](*keys) keys.map { |key| self.lookup(key) }.sequester! end
194
196
  end
195
197
 
196
198
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobj
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mason Glaves
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-24 00:00:00.000000000 Z
11
+ date: 2013-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec