Sha256: b79d58bb6518e1414eb2f996636007ec6a55fd5c9f4b85a92af52d347d1a1a09

Contents?: true

Size: 1.57 KB

Versions: 16

Compression:

Stored size: 1.57 KB

Contents

module Rubix
  
  class Template < Model

    #
    # == Properties & Finding ==
    #

    attr_accessor :name
    
    def initialize properties={}
      super(properties)
      @name     = properties[:name]
      
      self.host_ids = properties[:host_ids]
      self.hosts    = properties[:hosts]

      self.host_group_ids = properties[:host_group_ids]
      self.host_groups    = properties[:host_groups]
    end

    #
    # == Validation ==
    #

    def validate
      raise ValidationError.new("A template must have at least one host group.") if host_group_ids.nil? || host_group_ids.empty?
      true
    end
    
    #
    # == Associations ==
    #

    include Associations::HasManyHosts
    include Associations::HasManyHostGroups

    #
    # == CRUD ==
    #
    
    def create_params
      {:host => name, :groups => host_group_params}
    end
    
    def update_params
      [create_params.merge(id_field => id)]
    end

    def destroy_params
      [{id_field => id}]
    end

    def self.get_params
      super().merge(:select_groups => :refer, :select_hosts => :refer)
    end

    def self.find_params options={}
      get_params.merge(:filter => {:host => options[:name], :hostid => options[:id]})
    end

    def self.build template
      new({
            :id       => (template[id_field] || template['hostid']).to_i,
            :name     => template['host'],
            :host_ids => template['hosts'].map { |host_info| host_info['hostid'].to_i },
            :host_group_ids => template['groups'].map { |group| group['groupid'].to_i }
          })
    end
    
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rubix-0.4.0 lib/rubix/models/template.rb
rubix-0.3.1 lib/rubix/models/template.rb
rubix-0.3.0 lib/rubix/models/template.rb
rubix-0.2.1 lib/rubix/models/template.rb
rubix-0.2.0 lib/rubix/models/template.rb
rubix-0.1.5 lib/rubix/models/template.rb
rubix-0.1.4 lib/rubix/models/template.rb
rubix-0.1.3 lib/rubix/models/template.rb
rubix-0.1.2 lib/rubix/models/template.rb
rubix-0.1.1 lib/rubix/models/template.rb
rubix-0.1.0 lib/rubix/models/template.rb
rubix-0.0.12 lib/rubix/models/template.rb
rubix-0.0.11 lib/rubix/models/template.rb
rubix-0.0.10 lib/rubix/models/template.rb
rubix-0.0.9 lib/rubix/models/template.rb
rubix-0.0.8 lib/rubix/models/template.rb