Class: Compressor
- Inherits:
-
Object
- Object
- Compressor
- Defined in:
- lib/compressor.rb
Overview
docs
Instance Attribute Summary collapse
-
#type_name ⇒ Object
readonly
Returns the value of attribute type_name.
Instance Method Summary collapse
- #compress ⇒ Object
- #content_for(property) ⇒ Object
- #include_properties ⇒ Object
-
#initialize(type_name) ⇒ Compressor
constructor
A new instance of Compressor.
- #remove_comments ⇒ Object
- #remove_newlines ⇒ Object
- #shared_name(property) ⇒ Object
- #shared_path ⇒ Object
- #shared_property(property) ⇒ Object
- #shared_property?(property) ⇒ Boolean
- #specific_name(property) ⇒ Object
- #specific_property(property) ⇒ Object
- #specific_property?(property) ⇒ Boolean
- #type_file ⇒ Object
- #type_path ⇒ Object
- #type_text ⇒ Object
Constructor Details
#initialize(type_name) ⇒ Compressor
Returns a new instance of Compressor.
7 8 9 |
# File 'lib/compressor.rb', line 7 def initialize(type_name) @type_name = type_name end |
Instance Attribute Details
#type_name ⇒ Object (readonly)
Returns the value of attribute type_name.
5 6 7 |
# File 'lib/compressor.rb', line 5 def type_name @type_name end |
Instance Method Details
#compress ⇒ Object
81 82 83 84 85 86 |
# File 'lib/compressor.rb', line 81 def compress remove_comments include_properties # remove_newlines @type_text end |
#content_for(property) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/compressor.rb', line 51 def content_for(property) content = if specific_property?(property) specific_property(property) elsif shared_property(property) shared_property(property) else raise ArgumentError, "property #{property} not found" end content end |
#include_properties ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/compressor.rb', line 71 def include_properties properties = type_text.scan(/(?:parameter|property|incude_file)\s+:(\w+)/) properties.each do |property| property = property.first STDERR.puts "processing #{type_name}:#{property}..." regexp = /(?:property|parameter|incude_file)\s+:#{property}\s*$/ @type_text.gsub!(regexp, content_for(property)) end end |
#remove_comments ⇒ Object
62 63 64 |
# File 'lib/compressor.rb', line 62 def remove_comments type_text.gsub!(/#(?!\{.*\}).*/, '') end |
#remove_newlines ⇒ Object
66 67 68 69 |
# File 'lib/compressor.rb', line 66 def remove_newlines @type_text.gsub!("\n\n", "\n") # @type_text.gsub!(/(?<![,+-\{])\n/,";") end |
#shared_name(property) ⇒ Object
27 28 29 |
# File 'lib/compressor.rb', line 27 def shared_name(property) "#{shared_path}#{property}.rb" end |
#shared_path ⇒ Object
19 20 21 |
# File 'lib/compressor.rb', line 19 def shared_path 'lib/puppet/type/shared/' end |
#shared_property(property) ⇒ Object
43 44 45 |
# File 'lib/compressor.rb', line 43 def shared_property(property) File.open(shared_name(property), 'rb', &:read) end |
#shared_property?(property) ⇒ Boolean
35 36 37 |
# File 'lib/compressor.rb', line 35 def shared_property?(property) File.exist?(shared_name(property)) end |
#specific_name(property) ⇒ Object
31 32 33 |
# File 'lib/compressor.rb', line 31 def specific_name(property) "#{type_path}#{property}.rb" end |
#specific_property(property) ⇒ Object
47 48 49 |
# File 'lib/compressor.rb', line 47 def specific_property(property) File.read(specific_name(property)) end |
#specific_property?(property) ⇒ Boolean
39 40 41 |
# File 'lib/compressor.rb', line 39 def specific_property?(property) File.exist?(specific_name(property)) end |
#type_file ⇒ Object
11 12 13 |
# File 'lib/compressor.rb', line 11 def type_file "lib/puppet/type/#{@type_name}.rb" end |
#type_path ⇒ Object
15 16 17 |
# File 'lib/compressor.rb', line 15 def type_path "lib/puppet/type/#{@type_name}/" end |
#type_text ⇒ Object
23 24 25 |
# File 'lib/compressor.rb', line 23 def type_text @type_text ||= File.open(type_file, 'rb', &:read) end |