Module: Gamefic::Scriptable::Seeds

Included in:
Gamefic::Scriptable
Defined in:
lib/gamefic/scriptable/seeds.rb

Instance Method Summary collapse

Instance Method Details

#construct(name, klass, **opts) ⇒ void Also known as: attr_make, attr_seed

This method returns an undefined value.

Construct an entity.

This method adds an instance method for the entity and a class method to reference it with a proxy.

Parameters:



35
36
37
38
39
40
41
42
43
44
# File 'lib/gamefic/scriptable/seeds.rb', line 35

def construct name, klass, **opts
  ivname = "@#{name}"
  define_method(name) do
    return instance_variable_get(ivname) if instance_variable_defined?(ivname)

    instance_variable_set(ivname, make(klass, **unproxy(opts)))
  end
  seed { send(name) }
  define_singleton_method(name) { Proxy::Attr.new(name) }
end

#make(klass, **opts) ⇒ void Also known as: make_seed, seed_make

This method returns an undefined value.

Add an entity to be seeded when the narrative gets instantiated.

Parameters:



52
53
54
# File 'lib/gamefic/scriptable/seeds.rb', line 52

def make klass, **opts
  seed { make(klass, **unproxy(opts)) }
end

#seed(*methods, &block) ⇒ Object

Set methods and procs that get executed when a narrative gets initialized.

Examples:

class Example < Gamefic::Plot
  attr_reader :thing

  seed do
    @thing = make Entity, name: 'thing'
  end
end


22
23
24
25
# File 'lib/gamefic/scriptable/seeds.rb', line 22

def seed *methods, &block
  seeds.push(proc { methods.flatten.each { |method| send(method) } }) unless methods.empty?
  seeds.push block if block
end

#seedsArray<Proc>

Returns:



7
8
9
# File 'lib/gamefic/scriptable/seeds.rb', line 7

def seeds
  @seeds ||= []
end