Sha256: 1852e5c3dee73588b9f81194aed9b4608f92d37eec414b8ec3062f4d35c6a608

Contents?: true

Size: 1.31 KB

Versions: 21

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module Seam
  class BaseResource
    attr_accessor :data, :client

    def initialize(data, client = nil)
      @data = data
      @client = client

      @data.each do |key, value|
        instance_variable_set("@#{key}", value)
      end
    end

    def update_from_response(data)
      @data = data
      @data.each do |key, value|
        instance_variable_set("@#{key}", value)
      end
    end

    def self.load_from_response(data, client = nil)
      if data.is_a?(Array)
        data.map { |d| new(d, client) }
      else
        new(data, client)
      end
    end

    def inspect
      "<#{self.class.name}:#{"0x00%x" % (object_id << 1)}\n" + # rubocop:disable Style/StringConcatenation, Style/FormatString
        instance_variables
          .map { |k| k.to_s.sub("@", "") }
          .filter { |k| k != "data" and k != "client" and respond_to? k }
          .map { |k| "  #{k}=#{send(k).inspect}" }
          .join("\n") + ">"
    end

    def self.date_accessor(*attrs)
      attrs.each do |attr|
        define_method(attr) do
          value = instance_variable_get("@#{attr}")

          raise "No value for #{attr} set" if value.nil?

          parse_datetime(value)
        end
      end
    end

    protected

    def parse_datetime(value)
      Time.parse(value)
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
seamapi-2.0.0 lib/seam/resources/base_resource.rb
seamapi-1.17.0 lib/seam/resources/base_resource.rb
seamapi-1.16.1 lib/seam/resources/base_resource.rb
seamapi-1.16.0 lib/seam/resources/base_resource.rb
seamapi-1.15.0 lib/seam/resources/base_resource.rb
seamapi-1.14.0 lib/seam/resources/base_resource.rb
seamapi-1.13.0 lib/seam/resources/base_resource.rb
seamapi-1.12.1 lib/seam/resources/base_resource.rb
seamapi-1.12.0 lib/seam/resources/base_resource.rb
seamapi-1.11.2 lib/seam/resources/base_resource.rb
seamapi-1.11.1 lib/seam/resources/base_resource.rb
seamapi-1.11.0 lib/seam/resources/base_resource.rb
seamapi-1.10.0 lib/seam/resources/base_resource.rb
seamapi-1.9.0 lib/seam/resources/base_resource.rb
seamapi-1.8.0 lib/seam/resources/base_resource.rb
seamapi-1.7.0 lib/seam/resources/base_resource.rb
seamapi-1.6.0 lib/seam/resources/base_resource.rb
seamapi-1.5.0 lib/seam/resources/base_resource.rb
seamapi-1.4.0 lib/seam/resources/base_resource.rb
seamapi-1.3.2 lib/seam/resources/base_resource.rb