Class: CloudRailSi::ServiceCode::Create

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudrail_si/servicecode/commands/Create.rb

Instance Method Summary collapse

Instance Method Details

#execute(environment, parameters) ⇒ Object



11
12
13
14
15
16
17
18
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
47
48
49
50
51
52
53
# File 'lib/cloudrail_si/servicecode/commands/Create.rb', line 11

def execute(environment, parameters)
    Helper.assert(parameters.length >= 2 && Helper.is_var_address(parameters[0]) &&
        (Helper.is_string(parameters[1]) || Helper.is_var_address(parameters[1])))
    target_id = parameters[0]
    type = Helper.resolve(environment, parameters[1])
    Helper.assert(Helper.is_string(type))
    target_id_parts = environment.decode_variable_address(target_id)
    constructor_args = []
    (2..parameters.length - 1).each do |i|
        constructor_args.push(Helper.resolve(environment, parameters[i]))
    end
    if (type === 'String')
        new_object = ''

        (0..constructor_args.length - 1).each do |i|
            new_object += constructor_args[i].to_s
        end
    elsif (type === 'Number')
        raise Errors::InternalError.new('Create Number has too many arguments') if (constructor_args.length > 1)
        if (constructor_args.length === 1)
            if (Helper.is_number(constructor_args[0]))
                new_object = constructor_args[0]
            else
                raise Errors::InternalError.new('Create Number has an invalid argument type')
            end
        else
            new_object = 0
        end
    elsif (type === 'Object')
        raise Errors::InternalError.new('Create Object does not take constructor arguments') if (constructor_args.length != 0)
        new_object = {}
    elsif (type === 'Array')
        new_object = constructor_args.clone
    else
        if CloudRailSi::Types::Types.type_map.key?(type)
            constr = CloudRailSi::Types::Types.type_map[type]
            new_object = constr.new(*constructor_args)
        else
            assert(false)
        end
    end
    environment.set_variable(target_id_parts, new_object)
end

#get_identifierObject



7
8
9
# File 'lib/cloudrail_si/servicecode/commands/Create.rb', line 7

def get_identifier
    'create'
end