Module: ObjectLoader::ClassMethods

Defined in:
lib/object_loader/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#load_object(path, *arguments) ⇒ Object

Loads a compatible object.

Parameters:

  • path (String)

    The path to load the object from.

  • arguments (Array)

    Additional arguments to use when creating the object.

Returns:

  • (Object)

    The loaded object.

Since:

  • 1.0.0



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/object_loader/class_methods.rb', line 32

def load_object(path,*arguments)
  pending = ObjectLoader.load_blocks(path)

  pending_class, pending_block = pending.find do |klass,block|
    klass.ancestors.include?(self)
  end

  if (pending_class && pending_block)
    obj = pending_class.new(*arguments)
    obj.instance_eval(&pending_block)
    obj
  end
end

#load_object_block(path) ⇒ Proc

Loads a block for the Class.

Parameters:

  • path (String)

    The path to load the block from.

Returns:

  • (Proc)

    The block defined for the Class.

Since:

  • 1.0.0



14
15
16
# File 'lib/object_loader/class_methods.rb', line 14

def load_object_block(path)
  ObjectLoader.load_blocks(path)[self]
end

#object(*args) { ... } ⇒ Object

Creates a loadable object using the arguments and block.

Parameters:

  • args (Array)

    Additional arguments to pass to the Classes new method.

Yields:

  • [] The given block will be instance evaled into the newly created object when the object is loaded.

Since:

  • 1.0.0



58
59
60
61
62
63
64
65
66
67
# File 'lib/object_loader/class_methods.rb', line 58

def object(*args,&block)
  if (args.empty? && ObjectLoader.is_pending?)
    ObjectLoader.pending[self] = block
    return nil
  else
    new_object = self.new(*args)
    new_object.instance_eval(&block) if block
    return new_object
  end
end