Sha256: 5fb6d9626de17113d9e8f24992229783ad9b8696dccdb4f13686d5c5d6367583

Contents?: true

Size: 1.62 KB

Versions: 7

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

require "time"
require_relative "deep_hash_accessor"

module Seam
  module Resources
    class BaseResource
      attr_accessor :data, :client

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

        @data.each do |key, value|
          value = Seam::DeepHashAccessor.new(value) if value.is_a?(Hash)
          instance_variable_set(:"@#{key}", value)
        end
      end

      def update_from_response(data)
        @data = data
        @data.each do |key, value|
          value = Seam::DeepHashAccessor.new(value) if value.is_a?(Hash)
          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
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
seam-2.3.0 lib/seam/base_resource.rb
seam-2.2.0 lib/seam/base_resource.rb
seam-2.1.0 lib/seam/base_resource.rb
seam-2.0.1 lib/seam/base_resource.rb
seam-2.0.0 lib/seam/base_resource.rb
seam-2.0.0rc0 lib/seam/base_resource.rb
seam-2.0.0b5 lib/seam/base_resource.rb