Sha256: 223d0850b3bd9f9e2beff9286ea1c0a5a2a19961db6cd0f5bb43060dfd663079

Contents?: true

Size: 1.87 KB

Versions: 5

Compression:

Stored size: 1.87 KB

Contents

require "fog/core/deprecated_connection_accessors"

module Fog
  class Model
    extend Fog::Attributes::ClassMethods
    include Fog::Attributes::InstanceMethods
    include Fog::Core::DeprecatedConnectionAccessors

    attr_accessor :collection
    attr_reader :service

    def initialize(new_attributes = {})
      # TODO: Remove compatibility with old connection option
      attribs = new_attributes.clone
      @service = attribs.delete(:service)
      if @service.nil? && attribs[:connection]
        Fog::Logger.deprecation("Passing :connection option is deprecated, use :service instead [light_black](#{caller.first})[/]")
        @service = attribs[:connection]
      end
      merge_attributes(attribs)
    end

    def inspect
      Fog::Formatador.format(self)
    end

    def reload
      requires :identity

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

      return unless data

      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.reduce({}) 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_has_succeeded = false
      duration = Fog.wait_for(timeout, interval) do # Note that duration = false if it times out
        if reload
          reload_has_succeeded = true
          instance_eval(&block)
        else
          false
        end
      end
      if reload_has_succeeded
        return duration # false if timeout; otherwise {:duration => elapsed time }
      else
        raise Fog::Errors::Error, "Reload failed, #{self.class} #{identity} not present."
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
vagrant-cloudstack-1.1.0 vendor/bundle/gems/fog-core-1.30.0/lib/fog/core/model.rb
fog-core-1.30.0 lib/fog/core/model.rb
fog-core-1.29.0 lib/fog/core/model.rb
fog-core-1.28.0 lib/fog/core/model.rb
fog-core-1.27.4 lib/fog/core/model.rb