Sha256: 7e78eb70bcb356f6fb69b6247c0f8698e31ad97d52a3d443fd6fd15dc0e06006

Contents?: true

Size: 1.57 KB

Versions: 24

Compression:

Stored size: 1.57 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 = {})
      require 'multi_json'
      MultiJson.encode(attributes)
    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

24 entries across 24 versions & 5 rubygems

Version Path
brightbox-cli-0.18.1 lib/brightbox-cli/vendor/fog/lib/fog/core/model.rb
brightbox-cli-0.18.0 lib/brightbox-cli/vendor/fog/lib/fog/core/model.rb
brightbox-cli-0.17.5 lib/brightbox-cli/vendor/fog/lib/fog/core/model.rb
ktheory-fog-1.1.2 lib/fog/core/model.rb
brightbox-cli-0.17.4 lib/brightbox-cli/vendor/fog/lib/fog/core/model.rb
brightbox-cli-0.17.3 lib/brightbox-cli/vendor/fog/lib/fog/core/model.rb
brightbox-cli-0.17.2 lib/brightbox-cli/vendor/fog/lib/fog/core/model.rb
brightbox-cli-0.17.1 lib/brightbox-cli/vendor/fog/lib/fog/core/model.rb
brightbox-cli-0.17.0 lib/brightbox-cli/vendor/fog/lib/fog/core/model.rb
fog-1.1.2 lib/fog/core/model.rb
fog_tractical-1.1.4 lib/fog/core/model.rb
fog_tractical-1.1.3 lib/fog/core/model.rb
brightbox-cli-0.16.0 lib/brightbox-cli/vendor/fog/lib/fog/core/model.rb
fog-1.1.1 lib/fog/core/model.rb
fog-1.1.0 lib/fog/core/model.rb
brightbox-cli-0.15.0 lib/brightbox-cli/vendor/fog/lib/fog/core/model.rb
tag-fog-1.0.1 lib/fog/core/model.rb
brightbox-cli-0.14.1 lib/brightbox-cli/vendor/fog/lib/fog/core/model.rb
brightbox-cli-0.14.0 lib/brightbox-cli/vendor/fog/lib/fog/core/model.rb
fog-1.0.0 lib/fog/core/model.rb