Class: Woyo::WorldObject

Inherits:
Object
  • Object
show all
Includes:
Attributes, Evaluate
Defined in:
lib/woyo/world/world_object.rb

Direct Known Subclasses

Action, Character, Item, Location, Way, World

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Evaluate

#children, #evaluate, included

Methods included from Attributes

#attribute, #attributes, #define_attr, #define_attr!, #define_attr?, #define_attr_default, #define_attr_equals, #define_attr_methods, #dependent_changes, #exclusion, #exclusions, #group, #groups, #is, #is?, #track_changes

Constructor Details

#initialize(id, context: nil, &block) ⇒ WorldObject

Returns a new instance of WorldObject.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/woyo/world/world_object.rb', line 16

def initialize id, context: nil, &block
  @id = id ? id.to_s.downcase.to_sym : nil
  @context = context
  attributes :description, name: proc { id.to_s.capitalize.gsub('_',' ') }
  initialize_object
  evaluate &block
  # todo:
  #   creating attributes should register with change listener
  #   this will catch all attributes anytime they are created
  #   instead of just after initialize->evaluate
  track_changes
end

Instance Attribute Details

#_testObject

Returns the value of attribute _test.



12
13
14
# File 'lib/woyo/world/world_object.rb', line 12

def _test
  @_test
end

#contextObject (readonly)

Returns the value of attribute context.



11
12
13
# File 'lib/woyo/world/world_object.rb', line 11

def context
  @context
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/woyo/world/world_object.rb', line 11

def id
  @id
end

Instance Method Details

#attribute_changesObject



49
# File 'lib/woyo/world/world_object.rb', line 49

alias_method :attribute_changes, :changes

#attribute_clear_changesObject



39
# File 'lib/woyo/world/world_object.rb', line 39

alias_method :attribute_clear_changes, :clear_changes

#changesObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/woyo/world/world_object.rb', line 50

def changes
  all_changes = attribute_changes
  children.each do |child_type,type_children|
    child_type_changes = {}
    type_children.each do |child_id,child|
      child_changes = child.changes
      child_type_changes[child_id] = child_changes unless child_changes.empty?
    end
    all_changes[child_type] = child_type_changes unless child_type_changes.empty?
  end
  all_changes
end

#clear_changesObject



40
41
42
43
44
45
46
47
# File 'lib/woyo/world/world_object.rb', line 40

def clear_changes
  attribute_clear_changes
  children.each do |child_type,type_children|
    type_children.each do |child_id,child|
      child.clear_changes
    end
  end
end

#initialize_objectObject



29
# File 'lib/woyo/world/world_object.rb', line 29

def initialize_object ; end

#uidObject



31
32
33
34
35
36
37
# File 'lib/woyo/world/world_object.rb', line 31

def uid
  if @context
    @context.uid + '-' + id.to_s
  else
    id.to_s
  end
end