Class: Lomic::Lomic

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

Overview

This class helps clean up in-game class definitions

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLomic

Returns a new instance of Lomic.


82
83
84
85
86
87
88
89
# File 'lib/lomic/Lomic.rb', line 82

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

Class Method Details

.classNameObject


91
92
93
94
95
96
97
# File 'lib/lomic/Lomic.rb', line 91

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

.new_resource(symbol) ⇒ Object


54
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
# File 'lib/lomic/Lomic.rb', line 54

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


19
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
# File 'lib/lomic/Lomic.rb', line 19

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


48
49
50
51
52
# File 'lib/lomic/Lomic.rb', line 48

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

.var(symbols) ⇒ Object


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

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


99
100
101
# File 'lib/lomic/Lomic.rb', line 99

def className
  self.class.className
end