Sha256: a9c0b756eeef26879791ff29e9648ec1a18e6b2bed752090f45070e341e409f3

Contents?: true

Size: 1.7 KB

Versions: 22

Compression:

Stored size: 1.7 KB

Contents

module Maestrano
  module API
    module Operation
      module Update
        def save(opts={})
          values = serialize_params(self).merge(opts)

          if @values[:metadata]
            values[:metadata] = serialize_metadata
          end

          if values.length > 0
            values.delete(:id)

            response, api_token = Maestrano::API::Operation::Base[self.class.preset].request(:put, url, @api_token, values)
            refresh_from(response, api_token)
          end
          self
        end

        def serialize_metadata
          if @unsaved_values.include?(:metadata)
            # the metadata object has been reassigned
            # i.e. as object.metadata = {key => val}
            metadata_update = @values[:metadata]  # new hash
            new_keys = metadata_update.keys.map(&:to_sym)
            # remove keys at the server, but not known locally
            keys_to_unset = @previous_metadata.keys - new_keys
            keys_to_unset.each {|key| metadata_update[key] = ''}

            metadata_update
          else
            # metadata is a Maestrano::API::Object, and can be serialized normally
            serialize_params(@values[:metadata])
          end
        end

        def serialize_params(obj)
          case obj
          when nil
            ''
          when Maestrano::API::Object
            unsaved_keys = obj.instance_variable_get(:@unsaved_values)
            obj_values = obj.instance_variable_get(:@values)
            update_hash = {}

            unsaved_keys.each do |k|
              update_hash[k] = serialize_params(obj_values[k])
            end

            update_hash
          else
            obj
          end
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
maestrano-1.0.6 lib/maestrano/api/operation/update.rb
maestrano-1.0.5 lib/maestrano/api/operation/update.rb
maestrano-1.0.4 lib/maestrano/api/operation/update.rb
maestrano-1.0.3 lib/maestrano/api/operation/update.rb
maestrano-1.0.2 lib/maestrano/api/operation/update.rb
maestrano-1.0.1 lib/maestrano/api/operation/update.rb
maestrano-1.0.0 lib/maestrano/api/operation/update.rb
maestrano-1.0.0.pre.RC9 lib/maestrano/api/operation/update.rb
maestrano-1.0.0.pre.RC8 lib/maestrano/api/operation/update.rb
maestrano-1.0.0.pre.RC7 lib/maestrano/api/operation/update.rb
maestrano-1.0.0.pre.RC6 lib/maestrano/api/operation/update.rb
maestrano-1.0.0.pre.RC5 lib/maestrano/api/operation/update.rb
maestrano-1.0.0.pre.RC4 lib/maestrano/api/operation/update.rb
maestrano-1.0.0.pre.RC3 lib/maestrano/api/operation/update.rb
maestrano-1.0.0.pre.RC2 lib/maestrano/api/operation/update.rb
maestrano-1.0.0.pre.RC1 lib/maestrano/api/operation/update.rb
maestrano-0.12.5 lib/maestrano/api/operation/update.rb
maestrano-0.12.4 lib/maestrano/api/operation/update.rb
maestrano-0.12.3 lib/maestrano/api/operation/update.rb
maestrano-0.12.2 lib/maestrano/api/operation/update.rb