lib/tataru/resource_dsl.rb in tataru-0.1.0 vs lib/tataru/resource_dsl.rb in tataru-0.2.0

- old
+ new

@@ -1,86 +1,36 @@ # frozen_string_literal: true module Tataru - module DoLater - # delayed expression - class Expression - def requested_resources - raise 'Abstract class' - end - end + # human representation of resources + class ResourceDsl + REPCLASS = Tataru::Representations::ResourceRepresentation - # 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 + def initialize(name, desc) + @properties = {} + @desc = desc + @fields = Set.new(@desc.mutable_fields + @desc.immutable_fields) + @name = name + @dependencies = Set.new end - # placeholder for member call - class MemberCallPlaceholder < ExternResourcePlaceholder - def initialize(expr, member) - @expr = expr - @member = member - end - - def requested_resources - @expr.requested_resources - end + def respond_to_missing?(name, *_args) + true if @fields.include? name 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 + return super unless @fields.include? name - def errors - (@resource_inst.states - @fields.keys).map do |x| - { missing_state: x } - end + resolver = Resolver.new(args[0]) + @dependencies += resolver.dependencies + @properties[name] = if resolver.representation.is_a? REPCLASS + resolver.representation.remote_id + else + resolver.representation + 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 + def representation + REPCLASS.new(@name, @desc, @properties) end end end