Class: Gloo::Core::Factory

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

Instance Attribute Summary

Attributes inherited from Baseo

#name

Instance Method Summary collapse

Methods inherited from Baseo

#type_display

Constructor Details

#initializeFactory

Set up the object factory.



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

def initialize
  $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


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/gloo/core/factory.rb', line 117

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

  pn = Gloo::Core::Pn.new 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 ]
    $log.debug "Updating existing object: #{obj_name}"
    return self.update_existing pn, params[ :value ]
  end

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

#create_bool(name, value, parent) ⇒ Object

Helper shortcut to create a boolean child object.



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

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.



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

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

#create_int(name, value, parent) ⇒ Object

Helper shortcut to create an integer child object.



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

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.



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/gloo/core/factory.rb', line 142

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

  o = type.new
  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.



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

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.



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

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.



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

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.



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

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.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/gloo/core/factory.rb', line 169

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?
    $log.warn "Could not find type, '#{type_name}'"
    return nil
  end

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

  return t
end

#update_existing(pn, value) ⇒ Object

Find and Update an existing object.



158
159
160
161
162
# File 'lib/gloo/core/factory.rb', line 158

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