Sha256: cc77605a665b726d158447f9c5183e2402bf1dacae70020de7939bf654f4691a

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module Tataru
  module DoLater
    # delayed expression
    class Expression
      def requested_resources
        raise 'Abstract class'
      end
    end

    # placeholder for extern resource
    class ExternResourcePlaceholder < Expression
      def initialize(name)
        @name = name
      end

      def respond_to_missing?
        true
      end

      def method_missing(name, *_args)
        super if name.nil?

        MemberCallPlaceholder.new(self, name)
      end

      def requested_resources
        [@name]
      end
    end

    # placeholder for member call
    class MemberCallPlaceholder < ExternResourcePlaceholder
      def initialize(expr, member)
        @expr = expr
        @member = member
      end

      def requested_resources
        @expr.requested_resources
      end
    end
  end

  # Resource DSL
  class ResourceDSL
    attr_reader :fields, :extern_refs

    def initialize(resource_inst)
      @resource_inst = resource_inst
      @fields = {}
      @extern_refs = {}
    end

    def respond_to_missing?
      true
    end

    def method_missing(name, *args, &block)
      if @resource_inst.respond_to?("#{name}_change_action")
        @fields[name] = args[0]
      elsif name.to_s.start_with?(/[a-z]/)
        @extern_refs[name] ||= DoLater::ExternResourcePlaceholder.new(name)
      else
        super
      end
    end

    def errors
      (@resource_inst.states - @fields.keys).map do |x|
        { missing_state: x }
      end
    end

    def dependencies
      deps = []
      @fields.each do |_name, info|
        next unless info.is_a? DoLater::Expression

        deps += info.requested_resources
      end
      deps.map(&:to_s).uniq
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tataru-0.1.0 lib/tataru/resource_dsl.rb