Sha256: d920a9ed33989ba97eb2c6b389055b179a46a07308d7381680802997ddb47d15

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

module MistralClient
  module Mixins
    module MistralObject
      def self.included(child)
        child.send(:attr_reader, :id)

        %w[UNICODE_FIELDS DATE_FIELDS JSON_FIELDS BOOL_FIELDS].each do |fields|
          next unless child.const_defined? fields

          child.send(:attr_reader, *child.const_get(fields))
        end
      end

      def reload(id = @id)
        resp = @server.get("#{self.class::PATH}/#{id}")
        ivars_from_response(resp)
        self
      end

      def list(params = {})
        resp = @server.get("#{self.class::PATH}?#{URI.encode_www_form(params)}")
        resp[self.class::PATH].map do |t|
          task = self.class.new(@server)
          task.ivars_from_response(t)
          task
        end
      end

      # rubocop:disable Metrics/AbcSize
      # rubocop:disable Metrics/MethodLength
      def ivars_from_response(resp)
        klass = self.class
        @id = resp['id']
        klass::UNICODE_FIELDS.each do |var|
          instance_variable_set("@#{var}", resp[var]) if resp[var]
        end
        klass::BOOL_FIELDS.each do |var|
          instance_variable_set("@#{var}", resp[var]) if resp.key? var
        end
        klass::DATE_FIELDS.each do |var|
          if resp[var]
            instance_variable_set("@#{var}", DateTime.parse(resp[var]))
          end
        end
        klass::JSON_FIELDS.each do |var|
          instance_variable_set("@#{var}", JSON.parse(resp[var])) if resp[var]
        end
      end
      # rubocop:enable Metrics/MethodLength
      # rubocop:enable Metrics/AbcSize
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mistral_client-1.3.0 lib/mistral_client/mixins/mistral_object.rb