Sha256: fbd85f2f63cfc484df91be4cb0dd4528f9ad54224c87b46de73ef7968f7d7814

Contents?: true

Size: 1.93 KB

Versions: 30

Compression:

Stored size: 1.93 KB

Contents

require 'forwardable'
require_relative './intrinsic_functions'

module Convection
  module DSL
    ##
    # Methods for defining DSL/Models
    ##
    module ClassHelpers
      def attribute(attribute_name)
        attr_writer attribute_name
        define_method(attribute_name) do |value = nil|
          instance_variable_set("@#{ attribute_name }", value) unless value.nil?
          instance_variable_get("@#{ attribute_name }")
        end
      end

      def list(attribute_name)
        define_method(attribute_name) do |value = nil|
          collection = instance_variable_get("@#{ attribute_name }")
          collection << value unless value.nil?
          collection
        end
      end
    end

    ##
    # Helper methods for creating templates
    ##
    module Helpers
      extend Forwardable
      include DSL::IntrinsicFunctions

      def_delegators :@template, :stack, :parameters, :mappings, :resources, :outputs

      class << self
        def included(mod)
          DSL::IntrinsicFunctions.mixers << mod

          mod.extend(DSL::ClassHelpers)
        end

        def method_name(cf_type)
          nodes = cf_type.split('::')
          nodes.shift # Remove AWS::

          ## Cammel-case to snake-case
          nodes.map! do |n|
            n.split(/([A-Z0-9])(?![A-Z0-9])(?<!$)/)
             .reject(&:empty?)
             .reduce('') { |a, e| (e.length == 1 && !a.empty?) ? a + "_#{e}" : a + e }
             .downcase
          end

          nodes.join('_').downcase
        end
      end

      ## Convert various casings to CamelCase
      def camel_case(str)
        str.downcase.split(/[\.\-_]/).map(&:capitalize).join
      end

      ## Convert various casings to snake_case
      def snake_case(str)
        str.downcase.split(/[\.\-_]/).join('_')
      end

      ## Convert various casings to SCREAMING_SNAKE_CASE
      def screaming_snake_case(str)
        str.upcase.split(/[\.\-_]/).join('_')
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
convection-2.3.1 lib/convection/dsl/helpers.rb
convection-2.3.0 lib/convection/dsl/helpers.rb
convection-2.2.29 lib/convection/dsl/helpers.rb
convection-2.2.28.pre.beta.2 lib/convection/dsl/helpers.rb
convection-2.2.28.pre.beta.1 lib/convection/dsl/helpers.rb
convection-2.2.27 lib/convection/dsl/helpers.rb
convection-2.2.26 lib/convection/dsl/helpers.rb
convection-2.2.25 lib/convection/dsl/helpers.rb
convection-2.2.24 lib/convection/dsl/helpers.rb
convection-2.2.23 lib/convection/dsl/helpers.rb
convection-2.2.22 lib/convection/dsl/helpers.rb
convection-2.2.21 lib/convection/dsl/helpers.rb
convection-2.2.20 lib/convection/dsl/helpers.rb
convection-2.2.19 lib/convection/dsl/helpers.rb
convection-2.2.18 lib/convection/dsl/helpers.rb
convection-2.2.17 lib/convection/dsl/helpers.rb
convection-2.2.16 lib/convection/dsl/helpers.rb
convection-2.2.15 lib/convection/dsl/helpers.rb
convection-2.2.14 lib/convection/dsl/helpers.rb
convection-2.2.13 lib/convection/dsl/helpers.rb