Class: GlooLang::Core::Factory

Inherits:
Baseo
  • Object
show all
Defined in:
lib/gloo_lang/core/factory.rb

Constant Summary

Constants inherited from Baseo

Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary

Attributes inherited from Baseo

#name

Instance Method Summary collapse

Methods inherited from Baseo

#type_display

Constructor Details

#initialize(engine) ⇒ Factory

Set up the object factory.



18
19
20
21
# File 'lib/gloo_lang/core/factory.rb', line 18

def initialize( engine )
  @engine = engine
  @engine.log.debug 'object factory intialized...'
end

Instance Method Details

#create(params) ⇒ Object

Create object with given name, type and value. One of either name or type is required. All values are optional when considered on their own. Parameter hash keys:

:name - the name of the object
:type - the name of the type
:value - the initial object value
:parent - the parent object
:squash_duplicates - if the object exists, use it rather
                     than creating a new one?  Default = true


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/gloo_lang/core/factory.rb', line 140

def create( params )
  objtype = find_type params[ :type ]
  return nil unless objtype

  pn = GlooLang::Core::Pn.new( @engine, params[ :name ] )
  parent = params[ :parent ]
  if parent.nil?
    parent = pn.get_parent
    obj_name = pn.name
  else
    obj_name = params[ :name ]
  end

  if pn.exists? && params[ :squash_duplicates ]
    @engine.log.debug "Updating existing object: #{obj_name}"
    return self.update_existing pn, params[ :value ]
  end

  @engine.log.debug "Creating new object: #{obj_name}"
  return create_new obj_name, params[ :value ], objtype, parent
end

#create_alias(name, value, parent) ⇒ Object

Helper shortcut to create an alias child object.



30
31
32
33
34
35
36
# File 'lib/gloo_lang/core/factory.rb', line 30

def create_alias( name, value, parent )
  params = { :name => name,
             :type => 'alias',
             :value => value,
             :parent => parent }
  create params
end

#create_bool(name, value, parent) ⇒ Object

Helper shortcut to create a boolean child object.



85
86
87
88
89
90
91
# File 'lib/gloo_lang/core/factory.rb', line 85

def create_bool( name, value, parent )
  params = { :name => name,
             :type => 'boolean',
             :value => value,
             :parent => parent }
  create params
end

#create_can(name, parent) ⇒ Object

Helper shortcut to create a container child object.



96
97
98
99
100
101
102
# File 'lib/gloo_lang/core/factory.rb', line 96

def create_can( name, parent )
  params = { :name => name,
             :type => 'container',
             :value => nil,
             :parent => parent }
  create params
end

#create_file(name, value, parent) ⇒ Object

Helper shortcut to create a file child object.



118
119
120
121
122
123
124
# File 'lib/gloo_lang/core/factory.rb', line 118

def create_file( name, value, parent )
  params = { :name => name,
             :type => 'file',
             :value => value,
             :parent => parent }
  create params
end

#create_int(name, value, parent) ⇒ Object

Helper shortcut to create an integer child object.



74
75
76
77
78
79
80
# File 'lib/gloo_lang/core/factory.rb', line 74

def create_int( name, value, parent )
  params = { :name => name,
             :type => 'integer',
             :value => value,
             :parent => parent }
  create params
end

#create_new(name, value, type, parent) ⇒ Object

Create a new object.



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/gloo_lang/core/factory.rb', line 165

def create_new( name, value, type, parent )
  unless parent
    @engine.log.error "Could not create object.  Bad path: #{name}"
    return nil
  end

  o = type.new( @engine )
  o.name = name
  o.set_value value
  parent.add_child( o )
  return o
end

#create_script(name, value, parent) ⇒ Object

Helper shortcut to create a script child object.



107
108
109
110
111
112
113
# File 'lib/gloo_lang/core/factory.rb', line 107

def create_script( name, value, parent )
  params = { :name => name,
             :type => 'script',
             :value => value,
             :parent => parent }
  create params
end

#create_string(name, value, parent) ⇒ Object

Helper shortcut to create a string child object.



52
53
54
55
56
57
58
# File 'lib/gloo_lang/core/factory.rb', line 52

def create_string( name, value, parent )
  params = { :name => name,
             :type => 'string',
             :value => value,
             :parent => parent }
  create params
end

#create_text(name, value, parent) ⇒ Object

Helper shortcut to create a text child object.



63
64
65
66
67
68
69
# File 'lib/gloo_lang/core/factory.rb', line 63

def create_text( name, value, parent )
  params = { :name => name,
             :type => 'text',
             :value => value,
             :parent => parent }
  create params
end

#create_untyped(name, value, parent) ⇒ Object

Helper shortcut to create a string child object.



41
42
43
44
45
46
47
# File 'lib/gloo_lang/core/factory.rb', line 41

def create_untyped( name, value, parent )
  params = { :name => name,
             :type => 'untyped',
             :value => value,
             :parent => parent }
  create params
end

#find_type(type_name) ⇒ Object

Find the object type by name. Return nil if the object type cannot be found or cannot be created.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/gloo_lang/core/factory.rb', line 192

def find_type( type_name )
  type_name = 'untyped' if type_name.nil? || type_name.strip.empty?
  t = @engine.dictionary.find_obj( type_name )

  if t.nil?
    @engine.log.warn "Could not find type, '#{type_name}'"
    return nil
  end

  unless t.can_create?
    @engine.log.error "'#{type_name}' cannot be created."
    return nil
  end

  return t
end

#update_existing(pn, value) ⇒ Object

Find and Update an existing object.



181
182
183
184
185
# File 'lib/gloo_lang/core/factory.rb', line 181

def update_existing( pn, value )
  o = pn.resolve
  o.set_value value
  return o
end