summaryrefslogtreecommitdiffstats
path: root/lib/puppet/string/action.rb
blob: 4db9e97e21d9fea824e2ec2b825ac70f9c4ba91e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
require 'puppet/string'

class Puppet::String::Action
  attr_reader :name

  def initialize(string, name, attrs = {})
    name = name.to_s
    raise "'#{name}' is an invalid action name" unless name =~ /^[a-z]\w*$/

    @string = string
    @name      = name
    attrs.each do |k,v| send("#{k}=", v) end
  end

  def invoke(*args, &block)
    @string.method(name).call(*args,&block)
  end

  def invoke=(block)
    if @string.is_a?(Class)
      @string.define_method(@name, &block)
    else
      @string.meta_def(@name, &block)
    end
  end
end