Method: CapistranoNotification::Base.var

Defined in:
lib/capistrano-notification.rb

.var(name, opts = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/capistrano-notification.rb', line 14

def self.var(name, opts = {})
  define_method name do |*args, &block|
    case args.size
    when 0
      unless block
        if value = instance_variable_get("@#{name}")
          value.is_a?(Proc) ? value.call(self) : value
        else
          default = opts[:default]
          send name, default.is_a?(Proc) ? default.call(self) : default
        end
      else
        instance_variable_set "@#{name}", block
      end
    when 1
      instance_variable_set "@#{name}", args.first
    else
      raise ArgumentError, "wrong number of arguments (#{args.size} for 0 or 1)"
    end
  end

  if opts[:required]
    validates_presence_of name
  end
end