Sha256: 39735382699a8570f8060b543f64952f30f5f1a5d5183c5135cdbf96b3f75e05

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 KB

Contents

require 'stackster/entry/entry_lister'

module Stackster
  class Entry
    attr_accessor :name, :attributes

    def initialize(args)
      @domain = 'stacks'
      @config = args[:config]
      @logger = @config.logger
      self.name = region_specific_name args[:name]
      create_domain
      get_attributes
    end

    def self.find(args)
      entry = Entry.new :name   => args[:name],
                        :config => args[:config]
      entry
    end

    def set_attributes(a)
      a.each { |attribute| set_attribute(attribute) }
    end

    def save
      attributes.merge!('Name' => name)
      attributes.each_pair do |k,v|
        @logger.debug "Setting attribute #{k}=#{v}"
      end
      sdb_connect.put_attributes('stacks', name, attributes, { :replace => attributes.keys })
      @logger.info "Save to SimpleDB successful."
    end

    def delete_attributes
      sdb_connect.delete('stacks', name)
      @logger.info "Delete from SimpleDB successful."
    end

    private

    def get_attributes
      u = {}
      attrs = sdb_connect.select("select * from stacks where itemName() = '#{name}'")
      if attrs[name]
        attrs[name].each_pair { |k, v| u[k] = v.first }
      end
      self.attributes = u
    end

    def region_specific_name(name)
      "#{name}-#{@config.region}"
    end

    def create_domain
      sdb_connect.create_domain @domain
    end

    def set_attribute(attribute)
      self.attributes = attributes.merge(attribute)
    end

    def sdb_connect
      @sdb_connect ||= AWS::SimpleDB.new :config => @config
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stackster-0.2.6 lib/stackster/entry.rb
stackster-0.2.5 lib/stackster/entry.rb
stackster-0.2.4 lib/stackster/entry.rb
stackster-0.2.3 lib/stackster/entry.rb