Class: Object

Inherits:
BasicObject
Defined in:
lib/overload/blank.rb,
lib/overload/object.rb,
lib/lux/config/config.rb,
lib/common/method_attr.rb,
lib/common/class_callbacks.rb,
lib/overload/raise_variants.rb

Overview

for controllers, execute from AppController to MainController class_callback :before before do

...

end before :method_name instance = new instance.class_callback :before, instance.class_callback :before, arg

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.class_attribute(name, default = nil, &block) ⇒ Object

Defines class variable

Raises:

  • (ArgumentError)


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

def Object.class_attribute name, default=nil, &block
  raise ArgumentError.new('name must be symbol') unless name.is_a?(Symbol)

  ivar = "@cattr_#{name}"
  instance_variable_set ivar, block || default

  define_singleton_method('%s=' % name) { |arg| send(name, arg) }
  define_singleton_method(name) do |arg=:_undefined|
    # define and set if argument given
    if arg != :_undefined
      instance_variable_set ivar, arg
      return arg
    end

    # find value and return
    ancestors.each do |klass|
      if klass.instance_variable_defined?(ivar)
        value = klass.instance_variable_get ivar
        return value.is_a?(Proc) ? instance_exec(&value) : value
      end
    end
  end
end

.class_callback(name, context = nil, arg = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/common/class_callbacks.rb', line 18

def self.class_callback name, context=nil, arg=nil
  ivar = "@ccallbacks_#{name}"

  unless context
    define_singleton_method(name) do |method_name=nil, &block|
      ref = caller[0].split(':in ').first

      self.instance_variable_set(ivar, {}) unless instance_variable_defined?(ivar)
      self.instance_variable_get(ivar)[ref] = method_name || block
    end

  else
    list = context.respond_to?(:const_missing) ? context.ancestors : context.class.ancestors
    list = list.slice 0, list.index(Object) if list.index(Object)

    list.reverse.each do |klass|
      if klass.instance_variable_defined?(ivar)
        mlist = klass.instance_variable_get(ivar).values
        mlist.each do |m|
          if m.is_a?(Symbol)
            context.send m
          else
            context.instance_exec arg, &m
          end
        end
      end
    end
  end
end

.const_missing(klass) ⇒ Object



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

def self.const_missing klass
  file  = klass.to_s.tableize.singularize
  paths = [
    'models',
    'lib',
    'lib/vendor',
    'vendor',
    file.split('_').last.pluralize
  ].map  { |it| './app/%s/%s.rb' % [it, file] }

  klass_file = paths.find { |it| File.exist?(it) } or
    raise NameError.new('Can not find and autoload class "%s", looked in %s' % [klass, paths.map{ |it| "\n#{it}" }.join('')])

  # puts '* autoload: %s from %s' % [file, klass_file]

  require klass_file

  Object.const_get(klass)
end

Instance Method Details

#andand(func = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/overload/object.rb', line 75

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

#blank?Boolean

Returns:

  • (Boolean)


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

def blank?
  !self
end

#class_callback(name, arg = nil) ⇒ Object



14
15
16
# File 'lib/common/class_callbacks.rb', line 14

def class_callback name, arg=nil
  Object.class_callback name, self, arg
end

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



34
35
36
37
38
39
40
# File 'lib/overload/object.rb', line 34

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:

  • (Boolean)


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

def empty?
  blank?
end

#instance_variables_hashObject



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

def instance_variables_hash
  Hash[instance_variables.map { |name| [name, instance_variable_get(name)] } ]
end

#is_array?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/overload/object.rb', line 47

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

#is_boolean?Boolean

Returns:

  • (Boolean)


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

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

#is_false?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/overload/object.rb', line 55

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

#is_hash?Boolean

this will capture plain Hash and HashWithIndifferentAccess

Returns:

  • (Boolean)


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

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

#is_numeric?Boolean

Returns:

  • (Boolean)


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

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

#is_string?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/overload/object.rb', line 51

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

#is_symbol?Boolean

Returns:

  • (Boolean)


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

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

#is_true?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/overload/object.rb', line 59

def is_true?
  self ? true :false
end

#method_attr(name = nil, &block) ⇒ Object



64
65
66
67
68
# File 'lib/common/method_attr.rb', line 64

def method_attr name=nil, &block
  return MethodAttributes.get(self).or({}) if name.nil?

  MethodAttributes.define self, name, &block
end

#or(_or) ⇒ Object



25
26
27
# File 'lib/overload/object.rb', line 25

def or _or
  self.blank? || self == 0 ? _or : self
end

#present?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/overload/blank.rb', line 13

def present?
  !blank?
end

#r(what) ⇒ Object

raise object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/overload/raise_variants.rb', line 6

def r what
  opath = what.class.ancestors
  out   = opath.join("\n> ")

  data = what.is_a?(Hash) ? JSON.pretty_generate(what) : what.ai(plain:true)
  out = [data, out, ''].join("\n\n-\n\n")

  # unique_methods = what.methods - what.class.ancestors[0].instance_methods
  # raise unique_methods

  raise LocalRaiseError.new out
end

#reload!Object



166
167
168
# File 'lib/lux/config/config.rb', line 166

def reload!
  Lux::Config.live_require_check!
end

#rm(object) ⇒ Object

unique methods for object includes methods from modules



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/overload/raise_variants.rb', line 27

def rm object
  dump = []

  dump.push ['Class', 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', _]
      break
    end
  end

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

  r dump
end

#rr(what) ⇒ Object

better console log dump



20
21
22
23
# File 'lib/overload/raise_variants.rb', line 20

def rr what
  src = caller[0].sub(Lux.root.to_s+'/', '').sub(Lux.fw_root.to_s, 'lux-fw').split(':in `').first
  ap ['--- START (%s) %s ---' % [what.class, src], what, '--- END ---']
end

#rr!(what) ⇒ Object



51
52
53
54
# File 'lib/overload/raise_variants.rb', line 51

def rr! what
  print "\e[H\e[2J\e[3J" # clear osx screen :)
  rr what
end

#rr?(instance, m) ⇒ Boolean

show method info show User, :secure_hash

Returns:

  • (Boolean)


58
59
60
61
62
63
64
# File 'lib/overload/raise_variants.rb', line 58

def rr? instance, m
  el = instance.class.instance_method(m)
  puts el.source_location.join(':').yellow
  puts '-'
  puts el.source if el.respond_to?(:source)
  nil
end

#try(*args) ⇒ Object



29
30
31
32
# File 'lib/overload/object.rb', line 29

def try *args
  return nil if self.class == NilClass
  self.send(*args)
end