class Alienor::CoreObject

Attributes

hname[RW]
info[RW]
name[RW]
nb_indent[RW]
options[RW]
parent[RW]
source[RW]

Public Class Methods

add_dictionnary(dic_name) click to toggle source
# File lib/alienor/object.rb, line 18
def add_dictionnary(dic_name)
  attr_accessor dic_name
  self.dictionnaries << dic_name
end
define_branch(x, global = nil) click to toggle source

dynamic operations for a “branch” of this object global is the source class if this branch is “global” (meaning Source will hold a dictionnary for this branch)

# File lib/alienor/object.rb, line 30
def define_branch(x, global = nil)
  this_class_name = self.name.split('::')[-1]
  
  # compute context + x, eg Something::AlienorTest::Group
  this_context = self.name.split('::')[0..-2].join('::')
  x_class = (this_context == "") ? x.to_s.camelize : "#{this_context}::#{x.to_s.camelize}"
  
  # dictionnary name
  x_dic_name = x.to_s.pluralize # eg groups, entities
  
  # create dictionnary for that branch
  add_dictionnary x_dic_name.to_sym # eg :groups
  
  # create global dictionnary if global
  eval(global.name).add_dictionnary x_dic_name.to_sym if global # eg :entities
  
  # define method that creates new objects of a certain entity, eg def group(...)
  define_method "#{x}" do |name, hname, info={}, &block|
    conflict = eval("#{x}_conflict(name)") # this entry already exists
    obj = eval(x_class).new name, hname, self, source, info
    eval("@#{x_dic_name}")[name] = obj # feed parent dictionnary
    eval("@source.#{x_dic_name}")[name] = obj if global # feed source dictionnary if global
    block.call(obj) if block # block for creating other objects. Warning : yield(obj) if block_given? would not work here because within a define_method
    conflict ? nil : obj
  end
  
  # define method that evaluates conflict depending on branch being global or not
  # can be redefined if necessary
  define_method "#{x}_conflict" do |name|
    global ? eval("@source.#{x_dic_name}")[name] : eval("@#{x_dic_name}")[name]
  end
  
  # call named_parent definition in child class
  eval(x_class).define_named_parent this_class_name unless this_class_name == "Source"

end
define_named_parent(parent_class_name) click to toggle source

define a meaningful alias for method returning parent eg if Entity is a branch of Group, this will define in class Entity : def group; @parent; end

# File lib/alienor/object.rb, line 69
def define_named_parent(parent_class_name)
  #~ p "parent_class_name : #{parent_class_name}"
  define_method parent_class_name.underscore do
    @parent
  end
end
get_dic() click to toggle source
# File lib/alienor/object.rb, line 23
def get_dic
  #~ p "in #{self}"
  self.dictionnaries
end
new(name, hname, parent, source, info = {}) click to toggle source

name : name of the object hname : human name of the object source : the source object which contains global information info : optional infos, and options if any

# File lib/alienor/object.rb, line 84
def initialize(name, hname, parent, source, info = {})
  init_dics
  @name = name
  @hname = hname
  @parent = parent
  @source = source
  @info = info
  @options = info[:options]
end

Public Instance Methods

init_dics() click to toggle source
# File lib/alienor/object.rb, line 94
def init_dics
  self.dictionnaries.each do |d|
    self.instance_variable_set "@#{d}", {}
  end
end
template(tpl_name, dname = "") click to toggle source

make a string from a template dname is an optional path

# File lib/alienor/object.rb, line 102
def template (tpl_name, dname = "")
  name = File.join @source.paradigm_name, "templates", dname, tpl_name
  t = ERB.new File.read("#{name}.erb"), nil, '-' # trim_mode
  t.result(binding)
end
template_to_file(tpl_name, fname, dname = "") click to toggle source

make a file in the root directory from a template dname is an optional path (same for the template and the generated file)

# File lib/alienor/object.rb, line 110
def template_to_file (tpl_name, fname, dname = "")
  fw = File.open File.join(@source.root, dname, fname), "w"
  fw.puts template tpl_name, dname
  fw.close
end