diff options
| author | Paul Buetow <paul@buetow.org> | 2025-02-15 10:50:00 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-02-15 10:50:00 +0200 |
| commit | 713a2081a9a3265d1e5218c1f88a293cd3b32a1c (patch) | |
| tree | 80bea88e31357d554e19ed4969b57c42b9c4fb9c /lib | |
| parent | 8729aafe5bca25d0f7e94bd3c7ee1e0f60f99035 (diff) | |
can detect invalid resource names
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/dslkeywords/resource.rb | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/lib/dslkeywords/resource.rb b/lib/dslkeywords/resource.rb index 6612428..884dec9 100644 --- a/lib/dslkeywords/resource.rb +++ b/lib/dslkeywords/resource.rb @@ -1,31 +1,47 @@ +require 'set' + require_relative 'keyword' module RCM # To track recource dependencies - module Dependency - # Only to have the resourcename[id] syntax available in the DSL - class SyntaxSugar - def initialize(name) - @name = name + module ResourceDependencies + def initialize(...) + super(...) + @valid_resources = Set.new + ObjectSpace.each_object(Class).each do |klass| + @valid_resources << klass.to_s.sub('RCM::', '').downcase.to_sym if klass < Resource end + end + # Only to have the resourcename[id] syntax available in the DSL + class SyntaxSugar + def initialize(name) = @name = name def [](other) = "#{@name}['#{other}']" end - def method_missing(method_name) = SyntaxSugar.new(method_name) + class NoSuchResource < StandardError; end + + def method_missing(method_name) + raise NoSuchResource, "No such resource: #{method_name}" unless @valid_resources.include?(method_name) + + SyntaxSugar.new(method_name) + end + def respond_to_missing? = true def depends_on(*others) - @depends_on = {} if @depends_on.nil? + @dependencies = {} if @dependencies.nil? others.each do |other| info "Registered dependency on #{other}" - @depends_on[other] = {} + @dependencies[other] = {} end end + + def dependencies = @dependencies.nil ? [] : @dependencies end # A resource is something concrete to be managed, e.g. a file, or a CRON job. class Resource < Keyword - include Dependency + include ResourceDependencies end end |
