Class: Pancake::Configuration::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pancake/configuration.rb

Direct Known Subclasses

PancakeConfig, Stack::Configuration

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pancake/configuration.rb', line 66

def method_missing(name, *args)
  if name.to_s =~ /(.*?)=$/
    set_actual_value($1.to_sym, args.first)
  else
    if defaults.keys.include?(name) # We don't want to trigger a default value if we're blindly setting it
      # If the default is a proc, do not cache it
      case defaults[name][:value]
      when Proc
        p = defaults[name][:value]
        instance_eval &p
      else
        val = defaults[name][:value]
        val = val.dup rescue val
        set_actual_value(name, val)
      end
    else
      nil
    end
  end
end

Class Method Details

.default(meth, *args, &block) ⇒ Object

Set a default for this configuration class Provide a field/method name and a value to set this to. If you don’t provide a value, and instead provide a block, then a proc will be called lazily when requested.

Example

config_klass = Pancake::Configuration.make
config_class.default :foo, :bar, "This foo is a bar"
config = config_class.new

:api: public



22
23
24
25
26
27
28
29
30
# File 'lib/pancake/configuration.rb', line 22

def default(meth, *args, &block)
  value, description = args
  if block
    description = value
    value = block
  end
  defaults[meth][:value]       = value
  defaults[meth][:description] = description || ""
end

.description_for(field) ⇒ Object

Provides aaccess to the description for a default setting

:api: public



35
36
37
# File 'lib/pancake/configuration.rb', line 35

def description_for(field)
  defaults.keys.include?(field) ? defaults[field][:description] : ""
end

Instance Method Details

#defaultsObject

Access to the configuration defaults via the instance. Defers to the clas smethod for defaults :api: public



49
50
51
# File 'lib/pancake/configuration.rb', line 49

def defaults
  self.class.defaults
end

#description_for(field) ⇒ Object

Access to the class descritpion for defaults :api: public



55
56
57
# File 'lib/pancake/configuration.rb', line 55

def description_for(field)
  self.class.description_for(field)
end

#singleton_classObject

access to the singleton class :api: private



42
43
44
# File 'lib/pancake/configuration.rb', line 42

def singleton_class # :nodoc:
  class << self; self; end
end

#valuesObject

Access to the currently set values for this configuration object :api: public



61
62
63
# File 'lib/pancake/configuration.rb', line 61

def values
  @values ||= {}
end