Class: Object

Inherits:
BasicObject
Defined in:
lib/ext/object.rb

Instance Method Summary collapse

Instance Method Details

#__mobj__parent(rent = :"__mobj__parent") ⇒ Object



103
104
105
106
107
108
# File 'lib/ext/object.rb', line 103

def __mobj__parent(rent = :"__mobj__parent")
  unless rent == :"__mobj__parent"
    @__mobj__parent = rent == self ? nil : rent
  end
  @__mobj__parent
end

#__mobj__parent?Boolean

Returns:



99
100
101
# File 'lib/ext/object.rb', line 99

def __mobj__parent?()
  !@__mobj__parent.nil?
end

#__mobj__reparentObject



95
96
97
# File 'lib/ext/object.rb', line 95

def __mobj__reparent()
  values.each { |v| v.__mobj__parent(self); v.__mobj__reparent } if respond_to? :values
end

#__mobj__rootObject



91
92
93
# File 'lib/ext/object.rb', line 91

def __mobj__root()
  __mobj__parent.nil? || __mobj__parent == self ? self : __mobj__parent.__mobj__root
end

#a?(*kls) ⇒ Boolean

Returns:



31
32
33
# File 'lib/ext/object.rb', line 31

def a?(*kls)
  kls.when.mt?.be!([Array]).any? { |k| is_a? k }
end

#attempt(value = :root) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ext/object.rb', line 110

def attempt(value=:root)
  Forwarder.new do |name, *args, &block|
    if self.methods(true).include? name ##//use respond to?
      self.__send__(name, *args, &block)
    elsif value.p?
      value.call([name] + args, &block)
    elsif value.h? && value.ki?(name)
      value[name].when.p?.call(*args, &block)
    else
      value == :root ? self : value
    end
  end
end

#c?Boolean

Returns:



43
# File 'lib/ext/object.rb', line 43

def c?() a? Array, Hash end

#f!Object



56
# File 'lib/ext/object.rb', line 56

def f!() to_s.scan(/[\d\.]+/).join.to_f end

#f?Boolean

Returns:



39
# File 'lib/ext/object.rb', line 39

def f?() a? Float end

#fals?Boolean

Returns:



18
# File 'lib/ext/object.rb', line 18

def fals?(*) nil end

#h?Boolean

Returns:



42
# File 'lib/ext/object.rb', line 42

def h?() a? Hash end

#i!Object

xxx strip out junk?



55
# File 'lib/ext/object.rb', line 55

def i!() to_s.scan(/[\d\.]+/).join.to_i end

#i?Boolean

Returns:



38
# File 'lib/ext/object.rb', line 38

def i?() a? Fixnum end

#iff?(value = nil, &block) ⇒ Boolean

Returns:



20
# File 'lib/ext/object.rb', line 20

def iff?(value = nil, &block) block ? instance_exec(value, &block) : value end

#iffn?(_ = nil) ⇒ Boolean

Returns:



21
# File 'lib/ext/object.rb', line 21

def iffn?(_=nil) nil end

#m?Boolean

Returns:



36
# File 'lib/ext/object.rb', line 36

def m?() a? Symbol end

#n?Boolean

Returns:



40
# File 'lib/ext/object.rb', line 40

def n?() a? Fixnum, Float end

#nil!Object



6
# File 'lib/ext/object.rb', line 6

def nil!(*) self end

#null!Object



5
# File 'lib/ext/object.rb', line 5

def null!(*) self end

#o?Boolean

Returns:



53
# File 'lib/ext/object.rb', line 53

def o?() !un? end

#p?Boolean

Returns:



35
# File 'lib/ext/object.rb', line 35

def p?() a? Proc end

#rand?Boolean

Returns:



79
80
81
# File 'lib/ext/object.rb', line 79

def rand?
  rand(1000000).odd?
end

#responds_to?(*any) ⇒ Boolean

Returns:



23
24
25
# File 'lib/ext/object.rb', line 23

def responds_to?(*any)
  any.flatten.select { |method| respond_to?(method) }.realize!
end

#responds_to_all?(*all) ⇒ Boolean

Returns:



27
28
29
# File 'lib/ext/object.rb', line 27

def responds_to_all?(*all)
  responds_to?(all) == all
end

#s!Object



57
# File 'lib/ext/object.rb', line 57

def s!() to_s end

#s?Boolean

Returns:



37
# File 'lib/ext/object.rb', line 37

def s?() a? String end

#symObject



83
84
85
86
87
88
89
# File 'lib/ext/object.rb', line 83

def sym()
  if respond_to?(:to_sym)
    to_sym
  else
    to_s.to_sym
  end
end

#tru?(t = true, _ = nil, &block) ⇒ Boolean

Returns:



17
# File 'lib/ext/object.rb', line 17

def tru?(t=true, _=nil, &block) block ? instance_exec(t, &block) : t end

#try?(default = nil, &block) ⇒ Boolean Also known as: ifnil

Returns:



124
125
126
127
128
129
130
131
132
# File 'lib/ext/object.rb', line 124

def try?(default=nil, &block)
  Forwarder.new do |name, *args, &fblock|
    if methods(true).include?(name)
      __send__(name, *args, &fblock)
    elsif h? && ki?(name)
      self[name]
    end || (block ? instance_exec(*[*default], &block) : default) || nil.null!
  end
end

#un?Boolean

Returns:



49
50
51
# File 'lib/ext/object.rb', line 49

def un?()
  x? || (s? && s !~ /\S/) || (c? && mt?) || (n? && z0?)
end

#up!(*args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ext/object.rb', line 60

def up!(*args)
  if a?
    each { |i| i.up!(*args) }
  else
    if responds_to_all? :assign_attributes, :save
      args.select(&:h?).each do |arg|
        assign_attributes(arg)
      end
      save(validate:false)
    elsif responds_to? :update_attribute
      args.select(&:h?).each do |arg|
        arg.each do |k, v|
          update_attribute(k, v)
        end
      end
    end
  end
end

#whenObject Also known as: if?



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ext/object.rb', line 136

def when
  iam = self
  Forwarder.new do |name, *args, &block|
    if (iam.respond_to?(name) || iam.methods.include?(name)) && (got = iam.__send__(name, *args, &block))
      thn = Forwarder.new do |name, *args, &block|
        if name.sym == :then
          thn
        else
          ret = __send__(name, *args, &block)
          ret.define_singleton_method(:else) { Forwarder.new { ret } }
          ret
        end
      end
    else
      ret = Forwarder.new do |name, *args, &block|
        if name.sym == :then
          els = Forwarder.new do |name|
            if name.sym == :else
              Forwarder.new { |name, *args, &block| __send__(name, *args, &block) }
            else
              els
            end
          end
        else
          iam
        end
      end
    end
  end
end

#wrap!(*args, &block) ⇒ Object Also known as: alter!, be!



8
9
10
# File 'lib/ext/object.rb', line 8

def wrap!(*args, &block)
  block ? instance_exec(*args, &block) : args.sequester!
end

#x?Boolean

Returns:



45
# File 'lib/ext/object.rb', line 45

def x?() a? NilClass, FalseClass end

#z0?Boolean

Returns:



47
# File 'lib/ext/object.rb', line 47

def z0?() respond_to?(:zero?) ? zero? : f!.zero? end

#zeno!Object



58
# File 'lib/ext/object.rb', line 58

def zeno!() z0? ? 1.0 : self end