Sha256: 9332772f980a9bf0f03a7da69aff7d6756f99f63ff649dd9ff969e4b5835ac5f

Contents?: true

Size: 1.8 KB

Versions: 6

Compression:

Stored size: 1.8 KB

Contents

class Object

  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

  ###

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

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

  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

  # this will capture plain Hash and HashWithIndifferentAccess
  def is_hash?
    self.class.to_s.index('Hash') ? true : false
  end

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

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

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

  def is_true?
    self ? true :false
  end

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

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

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

  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

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lux-fw-0.5.37 ./lib/overload/object.rb
lux-fw-0.5.36 ./lib/overload/object.rb
lux-fw-0.5.35 ./lib/overload/object.rb
lux-fw-0.5.34 ./lib/overload/object.rb
lux-fw-0.5.33 ./lib/overload/object.rb
lux-fw-0.5.32 ./lib/overload/object.rb