Class: Object

Inherits:
BasicObject
Defined in:
lib/overload/blank.rb,
lib/overload/object.rb,
lib/overload/boolean.rb,
lib/overload/raise_variants.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.const_missing(klass, path = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/overload/object.rb', line 4

def self.const_missing klass, path=nil
  unless LUX_AUTO_LOAD.keys.first
    for file in `find ./app -type f -name '*.rb'`.split($/)
      klass_file = file.split('/').last.sub('.rb', '').classify
      LUX_AUTO_LOAD[klass_file] ||= [false, file]
    end
  end

  klass = klass.to_s if klass.class == Symbol
  path  = LUX_AUTO_LOAD[klass]
  error = %{Can't find and autoload module/class "%s"} % klass

  if path
    if path[0]
      raise NameError.new('%s, found file "%s" is not defineing it.' % [error, path[1]])
    else
      path[0] = true
      require path[1].sub('.rb', '')
      Object.const_get(klass)
    end
  else
    raise NameError.new('%s. Scanned all files in ./app folder' % error)
  end
end

Instance Method Details

#and(&block) ⇒ Object



40
41
42
# File 'lib/overload/object.rb', line 40

def and &block
  block.call(self) if self.present?
end

#andand(func = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/overload/object.rb', line 107

def andand func=nil
  if present?
    if block_given?
      yield(self)
    else
      func ? send(func) : self
    end
  else
    block_given? || func ? nil : {}.to_hwia
  end
end

#blank?Boolean

Returns:



2
3
4
# File 'lib/overload/blank.rb', line 2

def blank?
  !self
end

#die(desc = nil, exp_object = nil) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/overload/object.rb', line 55

def die desc=nil, exp_object=nil
  desc ||= 'died without desc'
  desc = '%s: %s' % [exp_object.class, desc] if exp_object
  puts desc.red
  puts caller.slice(0, 10)
  raise desc
end

#empty?Boolean

Returns:



6
7
8
# File 'lib/overload/blank.rb', line 6

def empty?
  blank?
end

#instance_variables_hashObject



119
120
121
122
123
# File 'lib/overload/object.rb', line 119

def instance_variables_hash
  vars = instance_variables - [:@current]
  vars = vars.reject { |var| var[0,2] == '@_' }
  Hash[vars.map { |name| [name, instance_variable_get(name)] }]
end

#is!(value = :_nil) ⇒ Object

value should be Float value.is! Float



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/overload/object.rb', line 127

def is! value = :_nil
  if value == :_nil
    if self.present?
      self
    else
      raise ArgumentError.new('Expected not not empty value')
    end
  elsif value == self.class
    self
  else
    if self.class == Class && self.superclass != Object
      if self.ancestors.include?(value)
        self
      else
        raise ArgumentError.new('There is no %s in %s ancestors in %s' % [value, self, caller[0]])
      end
    else
      raise ArgumentError.new('Expected %s but got %s in %s' % [value, self.class, caller[0]])
    end
  end
end

#is?(value = nil) ⇒ Boolean

value can be nil but if defined should be Float value.is? Float

Returns:



151
152
153
154
155
156
# File 'lib/overload/object.rb', line 151

def is? value = nil
  is! value
  true
rescue ArgumentError
  false
end

#is_a!(klass, error = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/overload/object.rb', line 96

def is_a! klass, error = nil
  ancestors.each { |kind| return true if kind == klass }

  if error
    message = error.class == String ? error : %[Expected "#{self}" to be of "#{klass}"]
    raise ArgumentError.new(message)
  else
    false
  end
end

#is_array?Boolean

Returns:



68
69
70
# File 'lib/overload/object.rb', line 68

def is_array?
  self.class.to_s.index('Array') ? true : false
end

#is_boolean?Boolean

Returns:



92
93
94
# File 'lib/overload/object.rb', line 92

def is_boolean?
  self.class == TrueClass || self.class == FalseClass
end

#is_false?Boolean

Returns:



76
77
78
# File 'lib/overload/object.rb', line 76

def is_false?
  self.class.name == 'FalseClass' ? true : false
end

#is_hash?Boolean

this will capture plain Hash and Hash With Indifferent Access

Returns:



64
65
66
# File 'lib/overload/object.rb', line 64

def is_hash?
  self.class.to_s.index('Hash') ? true : false
end

#is_numeric?Boolean

Returns:



84
85
86
# File 'lib/overload/object.rb', line 84

def is_numeric?
  Float(self) != nil rescue false
end

#is_string?Boolean

Returns:



72
73
74
# File 'lib/overload/object.rb', line 72

def is_string?
  self.class.to_s == 'String' ? true : false
end

#is_symbol?Boolean

Returns:



88
89
90
# File 'lib/overload/object.rb', line 88

def is_symbol?
  self.class.to_s == 'Symbol' ? true : false
end

#is_true?Boolean

Returns:



80
81
82
# File 'lib/overload/object.rb', line 80

def is_true?
  ['true', 'on', '1'].include?(to_s)
end

#nil(_or = nil, &block) ⇒ Object



36
37
38
# File 'lib/overload/object.rb', line 36

def nil _or = nil, &block
  self.nil? ? (block ? block.call : _or) : self
end

#or(_or = nil, &block) ⇒ Object

@foo.or(2)



32
33
34
# File 'lib/overload/object.rb', line 32

def or _or = nil, &block
  self.blank? || self == 0 ? (block ? block.call : _or) : self
end

#present?Boolean

Returns:



10
11
12
# File 'lib/overload/blank.rb', line 10

def present?
  !blank?
end

#r(what) ⇒ Object

raise object

Raises:

  • (StandardError)


3
4
5
6
7
8
9
10
11
12
# File 'lib/overload/raise_variants.rb', line 3

def r what
  if what.is_a?(Method)
    rr [:source_location, what.source_location.join(':')]
  else
    rr what
  end

  what = what.respond_to?(:to_jsonp) ? what.to_jsonp : what.inspect
  raise StandardError.new(what.nil? ? 'nil' : what)
end

#r?(object) ⇒ Boolean

unique methods for object includes methods from modules

Returns:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/overload/raise_variants.rb', line 33

def r? object
  dump = []

  dump.push 'Class: %s' % object.class

  instance_unique = object.methods - object.class.ancestors[0].instance_methods
  class_unique    = object.methods

  object.class.ancestors.drop(1).each do |_|
    class_unique -= _.instance_methods

    if _.class != Module
      dump.push 'Parent Class: %s' % _
      break
    end
  end

  dump.push ['Instance uniqe', instance_unique.sort] if instance_unique[0]
  dump.push ['Uniqe from parent', class_unique.sort.join(', ')]
  dump.push ['Uniqe from parent simple', object.class.instance_methods(false)]

  rr dump
end

#rr(what, as_jsonp = false) ⇒ Object

better console log dump



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/overload/raise_variants.rb', line 15

def rr what, as_jsonp = false
  klass = what.class
  klass = '%s at %s' % [klass, what.source_location.join(':').sub(Lux.root.to_s, '.')] if klass == Method
  from = caller[0].include?('raise_variants.rb') ? caller[1] : caller[0]
  from = from.sub(Lux.root.to_s+'/', './').split(':in ').first
  header = '--- START (%s) %s - %s ---' % [klass, from, Lux.current.request.url]
  as_jsonp = true if ['HashWia', 'Hash'].include?(what.class.to_s)
  if as_jsonp
    puts header
    puts what.to_jsonp
    puts '--- END ---'
  else
    ap [header, what, '--- END ---']
  end
end

#to_bObject



43
44
45
# File 'lib/overload/boolean.rb', line 43

def to_b
  !!::Boolean.parse(self)
end

#try(*args, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/overload/object.rb', line 44

def try *args, &block
  if self.class == NilClass
    nil
  elsif block_given?
    data = args.first.nil? ? self : self.send(*args)
    yield data
  else
    self.send(*args)
  end
end