Sha256: b018db36188a7c0afe33f532e06cf14281999708f429f39ce25fb627c1e70657

Contents?: true

Size: 719 Bytes

Versions: 3

Compression:

Stored size: 719 Bytes

Contents

module Erede
  module Models
    class Base
      def initialize
        set_accessors_for_api_attributes
      end

      def set_accessors_for_api_attributes
        self.class.const_get(:API_ATTRIBUTES).each do |attr_name|
          define_singleton_method attr_name do
            instance_variable_get("@#{attr_name}")
          end

          define_singleton_method "#{attr_name}=" do |value|
            instance_variable_set("@#{attr_name}", value)
          end
        end
      end

      def to_json(*)
        Hash[
          self.class.const_get(:API_ATTRIBUTES).map { |attr_name| [attr_name, send(attr_name)] }
        ]
        .delete_if { |k, v| v.nil? }
        .to_json
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
erede-ruby-sdk-3.0.2 lib/erede/models/base.rb
erede-ruby-sdk-3.0.1 lib/erede/models/base.rb
erede-ruby-sdk-3.0.0 lib/erede/models/base.rb