Module: Puppet::Interface::DocGen
Class Method Summary collapse
Instance Method Summary collapse
-
#attr_doc(name, &validate) ⇒ Object
The documentation attributes all have some common behaviours; previously we open-coded them across the set of six things, but that seemed wasteful - especially given that they were literally the same, and had the same bug hidden in them.
Class Method Details
.strip_whitespace(text) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/vendor/puppet/interface/documentation.rb', line 4 def self.strip_whitespace(text) text.gsub!(/[ \t\f]+$/, '') # We need to identify an indent: the minimum number of whitespace # characters at the start of any line in the text. # # Using split rather than each_line is because the later only takes a # block on Ruby 1.8.5 / Centos, and we support that. --daniel 2011-05-03 indent = text.split(/\n/).map {|x| x.index(/[^\s]/) }.compact.min if indent > 0 then text.gsub!(/^[ \t\f]{0,#{indent}}/, '') end return text end |
Instance Method Details
#attr_doc(name, &validate) ⇒ Object
The documentation attributes all have some common behaviours; previously we open-coded them across the set of six things, but that seemed wasteful - especially given that they were literally the same, and had the same bug hidden in them.
This feels a bit like overkill, but at least the common code is common now. –daniel 2011-04-29
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/vendor/puppet/interface/documentation.rb', line 28 def attr_doc(name, &validate) # Now, which form of the setter do we want, validated or not? get_arg = "value.to_s" if validate define_method(:"_validate_#{name}", validate) get_arg = "_validate_#{name}(#{get_arg})" end # We use module_eval, which I don't like much, because we can't have an # argument to a block with a default value in Ruby 1.8, and I don't like # the side-effects (eg: no argument count validation) of using blocks # without as metheds. When we are 1.9 only (hah!) you can totally # replace this with some up-and-up define_method. --daniel 2011-04-29 module_eval(<<-EOT, __FILE__, __LINE__ + 1) def #{name}(value = nil) self.#{name} = value unless value.nil? @#{name} end def #{name}=(value) @#{name} = Puppet::Interface::DocGen.strip_whitespace(#{get_arg}) end EOT end |