class CfnDsl::JSONable

This is the base class for just about everything useful in the DSL. It knows how to turn DSL Objects into the corresponding json, and it lets you create new built in function objects from inside the context of a dsl object.

Public Class Methods

external_parameters() click to toggle source
# File lib/cfndsl/jsonable.rb, line 123
def self.external_parameters
  CfnDsl::ExternalParameters.current
end

Public Instance Methods

as_json(_options = {}) click to toggle source

Use instance variables to build a json object. Instance variables that begin with a single underscore are elided. Instance variables that begin with two underscores have one of them removed.

# File lib/cfndsl/jsonable.rb, line 135
def as_json(_options = {})
  check_names
  hash = {}
  instance_variables.each do |var|
    name = var[1..]

    case name
    when /^__/
      # if a variable starts with double underscore, strip one off
      name = name[1..]
    when /^_/
      # Hide variables that start with single underscore
      name = nil
    end

    hash[name] = instance_variable_get(var) if name
  end
  hash
end
declare(&block) click to toggle source
# File lib/cfndsl/jsonable.rb, line 163
def declare(&block)
  instance_eval(&block) if block_given?
  self
end
external_parameters() click to toggle source
# File lib/cfndsl/jsonable.rb, line 127
def external_parameters
  self.class.external_parameters
end
ref_children() click to toggle source
# File lib/cfndsl/jsonable.rb, line 159
def ref_children
  instance_variables.map { |var| instance_variable_get(var) }
end
to_json(*args) click to toggle source
# File lib/cfndsl/jsonable.rb, line 155
def to_json(*args)
  as_json.to_json(*args)
end

Private Instance Methods

check_names() click to toggle source
# File lib/cfndsl/jsonable.rb, line 170
def check_names
  return if instance_variable_get('@Resources').nil?

  instance_variable_get('@Resources').each_key do |name|
    next unless name !~ /\A\p{Alnum}+\z/

    warn "Resource name: #{name} is invalid"
    exit 1
  end
end