Class: Terraframe::ScriptItem

Inherits:
Object
  • Object
show all
Defined in:
lib/terraframe/script_item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vars, context, &block) ⇒ ScriptItem

Returns a new instance of ScriptItem.



10
11
12
13
14
15
16
# File 'lib/terraframe/script_item.rb', line 10

def initialize(vars, context, &block)
  @fields = {}
  @context = context
  @vars = Hashie::Mash.new(vars)

  instance_eval &block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

DSL FUNCTIONS BELOW



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/terraframe/script_item.rb', line 26

def method_missing(method_name, *args, &block)
  if args.length == 1
    if args[0] == nil
      raise "Passed nil to '#{method_name}'. Generally disallowed, subclass ScriptItem if you need this."
    end

    @fields[method_name.to_sym] = args[0]
  else
    raise "Multiple fields passed to a scalar auto-argument '#{method_name}'."
  end
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



8
9
10
# File 'lib/terraframe/script_item.rb', line 8

def context
  @context
end

#fieldsObject (readonly)

Returns the value of attribute fields.



6
7
8
# File 'lib/terraframe/script_item.rb', line 6

def fields
  @fields
end

#varsObject (readonly)

Returns the value of attribute vars.



7
8
9
# File 'lib/terraframe/script_item.rb', line 7

def vars
  @vars
end

Instance Method Details

#id_of(resource_type, resource_name) ⇒ Object



42
43
44
# File 'lib/terraframe/script_item.rb', line 42

def id_of(resource_type, resource_name)
  output_of(resource_type, resource_name, :id)
end

#output_of(resource_type, resource_name, output_type) ⇒ Object



39
40
41
# File 'lib/terraframe/script_item.rb', line 39

def output_of(resource_type, resource_name, output_type)
  "${#{resource_type}.#{resource_name}.#{output_type}}"
end

#to_json(*a) ⇒ Object



18
19
20
21
22
23
# File 'lib/terraframe/script_item.rb', line 18

def to_json(*a)
  sanitized = @fields
  sanitized.delete("\#")

  sanitized.to_json(*a)
end