Sha256: ae950a4beba04b6a02cac84e58d7fa72658207d0a5ea954730b66fb8d6b93d26

Contents?: true

Size: 1.74 KB

Versions: 14

Compression:

Stored size: 1.74 KB

Contents

module Fog
  class Model

    extend Fog::Attributes::ClassMethods
    include Fog::Attributes::InstanceMethods

    attr_accessor :collection, :connection

    def initialize(new_attributes = {})
      merge_attributes(new_attributes)
    end

    def inspect
      Thread.current[:formatador] ||= Formatador.new
      data = "#{Thread.current[:formatador].indentation}<#{self.class.name}"
      Thread.current[:formatador].indent do
        unless self.class.attributes.empty?
          data << "\n#{Thread.current[:formatador].indentation}"
          data << self.class.attributes.map {|attribute| "#{attribute}=#{send(attribute).inspect}"}.join(",\n#{Thread.current[:formatador].indentation}")
        end
      end
      data << "\n#{Thread.current[:formatador].indentation}>"
      data
    end

    def reload
      requires :identity

      return unless data = begin
        collection.get(identity)
      rescue Excon::Errors::SocketError
        nil
      end

      new_attributes = data.attributes
      merge_attributes(new_attributes)
      self
    end

    def to_json(options = {})
      Fog::JSON.encode(attributes)
    end

    def symbolize_keys(hash)
      return nil if hash.nil?
      hash.inject({}) do |options, (key, value)|
        options[(key.to_sym rescue key) || key] = value
        options
      end
    end

    def wait_for(timeout=Fog.timeout, interval=1, &block)
      reload
      retries = 3
      Fog.wait_for(timeout, interval) do
        if reload
          retries = 3
        elsif retries > 0
          retries -= 1
          sleep(1)
        elsif retries == 0
          raise Fog::Errors::Error.new("Reload failed, #{self.class} #{self.identity} went away.")
        end
        instance_eval(&block)
      end
    end

  end
end

Version data entries

14 entries across 14 versions & 8 rubygems

Version Path
fog-nirvanix-1.8.2 lib/fog/core/model.rb
fog-nirvanix-1.8.1 lib/fog/core/model.rb
fog-parser-fix-1.6.1 lib/fog/core/model.rb
fog-test-again-1.6.0 lib/fog/core/model.rb
fog-parser-fix-1.6.0 lib/fog/core/model.rb
ey-vendored-backup-3.0.27 vendor/gems/fog/lib/fog/core/model.rb
fog-sgonyea-1.8.1 lib/fog/core/model.rb
fog-1.8.0 lib/fog/core/model.rb
fog-maestrodev-1.7.0.20121114190951 lib/fog/core/model.rb
fog-1.7.0 lib/fog/core/model.rb
fog-1.6.0 lib/fog/core/model.rb
fog-1.5.0 lib/fog/core/model.rb
rackspace-fog-1.4.2 lib/rackspace-fog/core/model.rb
fog-1.4.0 lib/fog/core/model.rb