Module: Tap::Support::ConfigurableClass

Includes:
LazyAttributes
Defined in:
lib/tap/support/configurable_class.rb

Overview

ConfigurableClass defines class methods used by a Configurable class to declare configurations. In addition to registering key-value pairs, these methods generate readers and writers, much like attr_accessor.

class ConfigurableClass
  extend ConfigurableClass
  config :one, 'one'
end

ConfigurableClass.configurations.to_hash   # => {:one => 'one'}

c = ConfigurableClass.new
c.respond_to?('one')                       # => true
c.respond_to?('one=')                      # => true

Instance Attribute Summary collapse

Attributes included from LazyAttributes

#source_file

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LazyAttributes

#lazy_attr

Instance Attribute Details

#configurationsObject (readonly)

A ClassConfiguration holding the class configurations.



29
30
31
# File 'lib/tap/support/configurable_class.rb', line 29

def configurations
  @configurations
end

Class Method Details

.extended(base) ⇒ Object

Sets the source_file for base and initializes base.configurations.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tap/support/configurable_class.rb', line 32

def self.extended(base) # :nodoc:
  caller.each_with_index do |line, index|
    case line
    when /\/configurable.rb/ then next
    when Lazydoc::CALLER_REGEXP
      base.instance_variable_set(:@source_file, File.expand_path($1))
      break
    end
  end
  
  base.instance_variable_set(:@configurations, ClassConfiguration.new(base))
end

Instance Method Details

#inherited(child) ⇒ Object

When subclassed, the parent.configurations are duplicated and passed to the child class where they can be extended/modified without affecting the configurations of the parent class.



48
49
50
51
52
53
54
55
56
# File 'lib/tap/support/configurable_class.rb', line 48

def inherited(child) # :nodoc:
  unless child.instance_variable_defined?(:@source_file)
    caller.first =~ Lazydoc::CALLER_REGEXP
    child.instance_variable_set(:@source_file, File.expand_path($1)) 
  end
  
  child.instance_variable_set(:@configurations, ClassConfiguration.new(child, @configurations))
  super
end

#lazydoc(resolve = true) ⇒ Object

Returns the lazydoc for self.



59
60
61
62
# File 'lib/tap/support/configurable_class.rb', line 59

def lazydoc(resolve=true)
  Lazydoc.resolve_comments(configurations.code_comments) if resolve
  super
end

#load_config(path) ⇒ Object

Loads the contents of path as YAML. Returns an empty hash if the path is empty, does not exist, or is not a file.



66
67
68
69
70
# File 'lib/tap/support/configurable_class.rb', line 66

def load_config(path)
  # the last check prevents YAML from auto-loading itself for empty files
  return {} if path == nil || !File.file?(path) || File.size(path) == 0
  YAML.load_file(path) || {}
end