./lib/overload/object.rb in lux-fw-0.5.37 vs ./lib/overload/object.rb in lux-fw-0.6.2
- old
+ new
@@ -1,47 +1,68 @@
class Object
+ LUX_AUTO_LOAD ||= {}
- 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] }
+ 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_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('')])
+ klass = klass.to_s if klass.class == Symbol
+ path = LUX_AUTO_LOAD[klass]
+ error = %{Can't find and autoload module/class "%s"} % klass
- # puts '* autoload: %s from %s' % [file, klass_file]
+ 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
- require klass_file
+ ###
- Object.const_get(klass)
+ # @foo.or(2)
+ def or _or = nil, &block
+ self.blank? || self == 0 ? (block ? block.call : _or) : self
end
- ###
+ def nil _or = nil, &block
+ self.nil? ? (block ? block.call : _or) : self
+ end
- def or _or
- self.blank? || self == 0 ? _or : self
+ def and &block
+ block.call(self) if self.present?
end
- def try *args
- return nil if self.class == NilClass
- self.send(*args)
+ 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
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
+ # this will capture plain Hash and Hash With Indifferent Access
def is_hash?
self.class.to_s.index('Hash') ? true : false
end
def is_array?
@@ -55,11 +76,11 @@
def is_false?
self.class.name == 'FalseClass' ? true : false
end
def is_true?
- self ? true :false
+ ['true', 'on', '1'].include?(to_s)
end
def is_numeric?
Float(self) != nil rescue false
end
@@ -70,22 +91,68 @@
def is_boolean?
self.class == TrueClass || self.class == FalseClass
end
+ 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
+
def andand func=nil
if present?
if block_given?
yield(self)
else
func ? send(func) : self
end
else
- block_given? || func ? nil : {}.h
+ block_given? || func ? nil : {}.to_hwia
end
end
def instance_variables_hash
- Hash[instance_variables.map { |name| [name, instance_variable_get(name)] } ]
+ vars = instance_variables - [:@current]
+ vars = vars.reject { |var| var[0,2] == '@_' }
+ Hash[vars.map { |name| [name, instance_variable_get(name)] }]
+ end
+
+ # value should be Float
+ # value.is! Float
+ 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
+
+ # value can be nil but if defined should be Float
+ # value.is? Float
+ def is? value = nil
+ is! value
+ true
+ rescue ArgumentError
+ false
end
end