Class: Alienor::CoreObject

Inherits:
Object
  • Object
show all
Includes:
Indentation
Defined in:
lib/alienor/object.rb

Direct Known Subclasses

CoreSource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Indentation

#add_line, #indent, #init_indent, #unindent

Constructor Details

#initialize(name, hname, parent, source, info = {}) ⇒ CoreObject

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



84
85
86
87
88
89
90
91
92
# 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

Instance Attribute Details

#hnameObject

Returns the value of attribute hname.



78
79
80
# File 'lib/alienor/object.rb', line 78

def hname
  @hname
end

#infoObject

Returns the value of attribute info.



78
79
80
# File 'lib/alienor/object.rb', line 78

def info
  @info
end

#nameObject

Returns the value of attribute name.



78
79
80
# File 'lib/alienor/object.rb', line 78

def name
  @name
end

#nb_indentObject

Returns the value of attribute nb_indent.



78
79
80
# File 'lib/alienor/object.rb', line 78

def nb_indent
  @nb_indent
end

#optionsObject

Returns the value of attribute options.



78
79
80
# File 'lib/alienor/object.rb', line 78

def options
  @options
end

#parentObject

Returns the value of attribute parent.



78
79
80
# File 'lib/alienor/object.rb', line 78

def parent
  @parent
end

#sourceObject

Returns the value of attribute source.



78
79
80
# File 'lib/alienor/object.rb', line 78

def source
  @source
end

Class Method Details

.add_dictionnary(dic_name) ⇒ Object



18
19
20
21
# 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) ⇒ Object

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)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object

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



69
70
71
72
73
74
# 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_dicObject



23
24
25
26
# File 'lib/alienor/object.rb', line 23

def get_dic
  #~ p "in #{self}"
  self.dictionnaries
end

Instance Method Details

#init_dicsObject



94
95
96
97
98
# 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 = "") ⇒ Object

make a string from a template dname is an optional path



102
103
104
105
106
# 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 = "") ⇒ Object

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



110
111
112
113
114
# 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