Class: Tap::Support::ClassConfiguration

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tap/support/class_configuration.rb

Overview

ClassConfiguration tracks configurations defined by a Configurable class.

Constant Summary collapse

DOC_TEMPLATE_PATH =

The path to the :doc template (see inspect)

File.join(config_templates_dir, 'doc.erb')
NODOC_TEMPLATE_PATH =

The path to the :nodoc template (see inspect)

File.join(config_templates_dir, 'nodoc.erb')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver, parent = nil) ⇒ ClassConfiguration

Generates a new ClassConfiguration for the receiver. If a parent is provided, configurations will be inherited from it.



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

def initialize(receiver, parent=nil)
  @receiver = receiver
  
  if parent != nil
    @map = parent.map.inject({}) do |hash, (key, config)|
      hash[key] = config.dup
      hash
    end
    @assignments = Assignments.new(parent.assignments)
  else
    @map = {}
    @assignments = Assignments.new
  end
end

Instance Attribute Details

#assignmentsObject (readonly)

An Assignments tracking the assignment of config keys to receivers



24
25
26
# File 'lib/tap/support/class_configuration.rb', line 24

def assignments
  @assignments
end

#mapObject (readonly)

A map of [key, Configuration] pairs



27
28
29
# File 'lib/tap/support/class_configuration.rb', line 27

def map
  @map
end

#receiverObject (readonly)

The Configurable class receiving new configurations



21
22
23
# File 'lib/tap/support/class_configuration.rb', line 21

def receiver
  @receiver
end

Instance Method Details

#[](key) ⇒ Object

Gets the config specified by key. The key is symbolized.



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

def [](key)
  map[key.to_sym]  
end

#[]=(key, config) ⇒ Object

Assigns the config to key. A nil config unassigns the configuration key. The key is symbolized.



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tap/support/class_configuration.rb', line 65

def []=(key, config)
  key = key.to_sym
  
  if config == nil
    assignments.unassign(key)
    map.delete(key)
  else
    assignments.assign(receiver, key) unless assignments.assigned?(key)
    map[key] = config
  end
end

#add(name, default = nil, attributes = {}) ⇒ Object

Initializes a Configuration using the inputs and sets using name as a key. Any existing config by the same name is overridden. Returns the new config.



49
50
51
# File 'lib/tap/support/class_configuration.rb', line 49

def add(name, default=nil, attributes={})
  self[name] = Configuration.new(name.to_sym, default, attributes)
end

#code_commentsObject

An array of config descriptions that are Comment objects.



134
135
136
137
138
139
140
# File 'lib/tap/support/class_configuration.rb', line 134

def code_comments
  code_comments = []
  values.each do |config| 
    code_comments << config.desc if config.desc.kind_of?(Lazydoc::Comment)
  end
  code_comments
end

#eachObject

Calls block once for each [receiver, key, config] in self, passing those elements as parameters in the order in which they were assigned.



105
106
107
108
109
# File 'lib/tap/support/class_configuration.rb', line 105

def each
  assignments.each do |receiver, key|
    yield(receiver, key, map[key])
  end
end

#each_pairObject

Calls block once for each [key, config] pair in self, passing those elements as parameters in the order in which they were assigned.



114
115
116
117
118
# File 'lib/tap/support/class_configuration.rb', line 114

def each_pair
  assignments.each do |receiver, key|
    yield(key, map[key])
  end
end

#empty?Boolean

True if map is empty.

Returns:

  • (Boolean)


98
99
100
# File 'lib/tap/support/class_configuration.rb', line 98

def empty?
  map.empty?
end

#inspect(template = :doc, target = "") ⇒ Object

Inspects the configurations using the specified template. Templates are used for format each [receiver, configurations] pair in self. See DEFAULT_TEMPLATE as a model. The results of each template cycle are pushed to target.

Two default templates are defined, :doc and :nodoc. These map to the contents of DOC_TEMPLATE_PATH and NODOC_TEMPLATE_PATH and correspond to the documented and undocumented Tap::Generator::Generators::ConfigGenerator templates.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/tap/support/class_configuration.rb', line 151

def inspect(template=:doc, target="")
  Lazydoc.resolve_comments(code_comments)
  
  template = case template
  when :doc then File.read(DOC_TEMPLATE_PATH)
  when :nodoc then File.read(NODOC_TEMPLATE_PATH)
  else template
  end
  
  templater = Templater.new(template)
  assignments.each_pair do |receiver, keys|
    next if keys.empty?
    
    # set the template attributes
    templater.receiver = receiver
    templater.configurations = keys.collect do |key|
      # duplicate config so that any changes to it
      # during templation will not propogate back
      # into self
      [key, map[key].dup]
    end.compact
    
    yield(templater) if block_given?
    target << templater.build
  end
  
  target
end

#instance_config(receiver = nil, store = {}) ⇒ Object

Initializes and returns a new InstanceConfiguration set to self and bound to the receiver, if specified.



122
123
124
# File 'lib/tap/support/class_configuration.rb', line 122

def instance_config(receiver=nil, store={})
  InstanceConfiguration.new(self, receiver, store)
end

#key?(key) ⇒ Boolean

Returns true if key is a config key.

Returns:

  • (Boolean)


78
79
80
# File 'lib/tap/support/class_configuration.rb', line 78

def key?(key)
  map.has_key?(key)
end

#keysObject

Returns all config keys.



83
84
85
# File 'lib/tap/support/class_configuration.rb', line 83

def keys
  map.keys
end

#ordered_keysObject

Returns config keys in order.



88
89
90
# File 'lib/tap/support/class_configuration.rb', line 88

def ordered_keys
  assignments.values
end

#remove(key) ⇒ Object

Removes the specified configuration.



54
55
56
# File 'lib/tap/support/class_configuration.rb', line 54

def remove(key)
  self[key] = nil
end

#to_hashObject

Returns a hash of the [key, config.default] pairs in self.



127
128
129
130
131
# File 'lib/tap/support/class_configuration.rb', line 127

def to_hash
  hash = {}
  each_pair {|key, config| hash[key] = config.default }
  hash
end

#valuesObject

Returns all mapped configs.



93
94
95
# File 'lib/tap/support/class_configuration.rb', line 93

def values 
  map.values
end