Class: Lomic::LomicBase

Inherits:
Object
  • Object
show all
Defined in:
lib/lomic/LomicBase.rb

Overview

This class helps clean up in-game class definitions

Direct Known Subclasses

Rule

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLomicBase

Returns a new instance of LomicBase.



83
84
85
86
87
88
89
90
91
92
# File 'lib/lomic/LomicBase.rb', line 83

def initialize
  @init_used = {}
  if self.class.class_variable_defined? "@@__#{className}_inits__"
    inits = self.class.class_variable_get "@@__#{className}_inits__"
    inits.each do |var,val|
      instance_variable_set "@#{var}", val
      @init_used[var] = true
    end
  end
end

Class Method Details

.classNameObject



94
95
96
97
98
99
100
# File 'lib/lomic/LomicBase.rb', line 94

def self.className
  if self.name.include? '::'
    self.name.split('::')[1]
  else
    self.name
  end
end

.new_resource(symbol) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lomic/LomicBase.rb', line 55

def self.new_resource(symbol)
  name = symbol.keys[0]
  init_val = symbol[name]
  
  if init_val.instance_of? Array
    min,max,init = case init_val.size
      when 1 then [0,init_val[0],init_val[0]]
      when 2 then [init_val[0],init_val[1],init_val[1]]
      else [init_val[0],init_val[1],init_val[2]]
    end
  else
    min,max,init = [0,init_val,init_val]
  end
  self.new_var(name => init)
  self.new_var("#{name}min" => min)
  self.new_var("#{name}max" => max)
  
  # redefine the set method to enforce resource limits
  self.remove_method "#{name}="
  self.define_method "#{name}=" do |new_val|
    min = instance_variable_get "@#{name}min"
    max = instance_variable_get "@#{name}max"
    new_val = new_val < min ? min : (new_val > max ? max : new_val)
    
    instance_variable_set("@#{name}", new_val)
  end
end

.new_var(symbol) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lomic/LomicBase.rb', line 20

def self.new_var(symbol)
  name = symbol.keys[0]
  init_val = symbol[name]
  
  self.define_method name do
    getter = "@#{name}"
    getter += '?' if init_val.instance_of? TrueClass or init_val.instance_of? FalseClass
    val = instance_variable_get getter
    
    if val.nil? and @init_used[name].nil?
      inits = self.class.class_eval "@@__#{className}_inits__"
      val = inits[name]
      instance_variable_set("@#{name}", val)
      @init_used[name] = true
    end
    
    return val
  end
  
  self.define_method "#{name}=" do |new_val|
    instance_variable_set("@#{name}", new_val)
    @init_used[name] = true
  end

  class_eval "@@__#{self.className}_inits__ ||= {}"
  inits = class_eval "@@__#{self.className}_inits__"
  inits[name] = init_val
end

.resource(symbols) ⇒ Object



49
50
51
52
53
# File 'lib/lomic/LomicBase.rb', line 49

def self.resource(symbols)
  symbols.each { |name,init_val|
    self.new_resource(name => init_val)
  }
end

.var(symbols) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/lomic/LomicBase.rb', line 10

def self.var(symbols)
  if symbols.instance_of? Symbol
    self.new_var({symbols.to_sym => nil})
  else
    symbols.each { |name,init_val|
      self.new_var(name => init_val)
    }
  end
end

Instance Method Details

#classNameObject



102
103
104
# File 'lib/lomic/LomicBase.rb', line 102

def className
  self.class.className
end