Skip to content
Snippets Groups Projects
Commit 342a7958 authored by Jeffrey Phillips Freeman's avatar Jeffrey Phillips Freeman :boom:
Browse files

Got defaults working but doesnt handle subclassing correctly yet.

parent 1a9b7572
No related branches found
No related tags found
No related merge requests found
...@@ -12,22 +12,23 @@ class GameObject < Publisher ...@@ -12,22 +12,23 @@ class GameObject < Publisher
include Lexicon include Lexicon
include Defaults include Defaults
attr_reader :short_desc, :game_object_id, :alt_names, :generic, :article, :sex, :show_in_look, :actions, :balance, :admin, :manager attr_reader :short_desc, :game_object_id, :alt_names, :generic, :article, :sex, :gender, :show_in_look, :actions, :balance, :admin, :manager
attr_accessor :container, :show_in_look, :actions, :pose, :visible, :comment, :movable, :quantity, :info attr_accessor :container, :show_in_look, :actions, :pose, :visible, :comment, :movable, :quantity, :info
attr_writer :plural attr_writer :plural
alias :room :container alias :room :container
alias :can? :respond_to? alias :can? :respond_to?
alias :goid :game_object_id alias :goid :game_object_id
Defaults::default(:gender) do default(:gender) do |this|
if @sex == 'm' if this.sex == 'm'
Lexicon::Gender::MASCULINE Lexicon::Gender::MASCULINE
elsif @sex == 'f' elsif this.sex == 'f'
Lexicon::Gender::FEMININE Lexicon::Gender::FEMININE
else else
Lexicon::Gender::NEUTER Lexicon::Gender::NEUTER
end end
end end
default(:visible) { true }
#Creates a new GameObject. Most of this long list of parameters is simply ignored at creation time, #Creates a new GameObject. Most of this long list of parameters is simply ignored at creation time,
#because they can all be set later. #because they can all be set later.
...@@ -51,7 +52,6 @@ class GameObject < Publisher ...@@ -51,7 +52,6 @@ class GameObject < Publisher
@sex = sex @sex = sex
#The article of the object ('a','an',etc) #The article of the object ('a','an',etc)
@article = article @article = article
@visible = true
#This is tricky. If @show_in_look is something #This is tricky. If @show_in_look is something
#other than false (or nil), then the object will #other than false (or nil), then the object will
#not show up in the list of objects, but rather this #not show up in the list of objects, but rather this
...@@ -83,6 +83,10 @@ class GameObject < Publisher ...@@ -83,6 +83,10 @@ class GameObject < Publisher
@admin = false @admin = false
load_defaults load_defaults
puts "checking gender for #{@name}"
puts @gender
puts "and defaults"
puts @@defaults
end end
def attributes def attributes
......
...@@ -15,7 +15,7 @@ module Defaults ...@@ -15,7 +15,7 @@ module Defaults
local_defaults.each do |default| local_defaults.each do |default|
attribute = default[:attribute] attribute = default[:attribute]
block = default[:block] block = default[:block]
if defined?(attribute).nil? if not instance_variable_defined?(attribute)
self.set_default(attribute, &block) self.set_default(attribute, &block)
end end
end end
...@@ -24,7 +24,8 @@ module Defaults ...@@ -24,7 +24,8 @@ module Defaults
private private
def set_default(attribute) def set_default(attribute)
self.set_instance_variable(attribute, yield) value = yield self
self.instance_variable_set(attribute, value)
end end
module ClassMethods module ClassMethods
...@@ -42,13 +43,33 @@ end ...@@ -42,13 +43,33 @@ end
class Foo class Foo
include Defaults include Defaults
attr_reader :sex
default(:bar) {"foobar"} default(:bar) {"foobar"}
default(:baz) { |this| this.instance_variable_get(:@bar).concat(" and baz") }
#default(:sex) {"m"}
default(:gender) do |this|
if this.sex == 'm'
"Lexicon::Gender::MASCULINE"
elsif this.sex == 'f'
"Lexicon::Gender::FEMININE"
else
"Lexicon::Gender::NEUTER"
end
end
def initialize def initialize
@dummy_var = "this is just so something exists"
@sex = 'm'
load_defaults load_defaults
end end
def bar def local_failbar
@failbar
end
def local_bar
@bar @bar
end end
...@@ -59,4 +80,57 @@ class Foo ...@@ -59,4 +80,57 @@ class Foo
def local_df def local_df
@@defaults @@defaults
end end
def class_vars_proxy
self.class.instance_variables
end
def local_baz
@baz
end
def local_sex
@sex
end
def local_gender
@gender
end
end end
puts "check_df"
puts Foo.check_df
puts "local_df"
foo = Foo.new
puts foo.local_df
puts "Defaults::defaults"
puts Defaults::defaults
#puts "foo.defaults"
#puts foo.defaults
puts "local_bar"
puts foo.local_bar
puts "local_failbar"
puts foo.local_failbar
puts "instance vars"
puts foo.instance_variables
puts "class vars"
puts Foo.instance_variables
puts "class vars proxy"
puts foo.class_vars_proxy
puts "local_baz"
puts foo.local_baz
puts "local_sex"
puts foo.local_sex
puts "local_gender"
puts foo.local_gender
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment