Sha256: ecc1e6e73f27562e0b946121024e238533994ec8c7f023136aa1faeb0955f7c3

Contents?: true

Size: 1.89 KB

Versions: 4

Compression:

Stored size: 1.89 KB

Contents

module PoolParty    
  module Resources
    
    # Wrap all the resources into a class package from 
    def classpackage_with_self(parent=self, &block)
      @cp = PoolParty::Resources::Classpackage.new(parent.options, parent, &block)
      @cp.instance_eval {@resources = parent.resources}
      parent.instance_eval {@resources = nil}
      @cp
    end
                
    class Classpackage < Resource
            
      default_options({
        :name => "custom"
      })
      
      def initialize(opts={}, parent=self, &block)
        # Take the options of the parents
        set_parent(parent, false) if parent
        set_vars_from_options(opts) unless opts.empty?
        self.instance_eval &block if block
        # store_block(&block)        
        loaded
      end
                        
      def to_string
        returning String.new do |output|
          output << "# #{name.sanitize}"
          output << "\nclass #{name.sanitize.downcase} {\n"
          output << resources_string_from_resources(resources)
          output << "\n}\n"
        end
      end
      
      def include_string
        "include #{name.sanitize.downcase}"
      end
      
      def name(*args)
        args.empty? ? (@name || parent.name || "custom_#{Time.now.to_i}") : @name ||= args.first
      end
      def printable?
        false
      end

    end
    
    def resources_string_from_resources(resources, pre="\t")
      @variables = resources.extract! {|name,resource| name == :variable}
      returning Array.new do |str|
        unless @variables.empty?
          str << "\n# Variables \n"
          @variables.each do |name, variable|
            str << variable.to_string("#{pre}")
          end          
        end
        
        resources.each do |type, resource|
          str << "\n#{pre*2}# #{type}\n"
          str << resource.to_string("#{pre*2}")
        end        
      end.join("\n")
    end
    
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
auser-poolparty-0.2.23 lib/poolparty/pool/resources/class_package.rb
auser-poolparty-0.2.24 lib/poolparty/pool/resources/class_package.rb
auser-poolparty-0.2.25 lib/poolparty/pool/resources/class_package.rb
auser-poolparty-0.2.26 lib/poolparty/pool/resources/class_package.rb