Sha256: 3ae31388d80095e39454c7d93b4ba30577455f3f1cddb091744f0ada28ac2c1f

Contents?: true

Size: 1003 Bytes

Versions: 6

Compression:

Stored size: 1003 Bytes

Contents

# frozen_string_literal: true
module GetYourRep
  # Base class for external responses
  class Base
    # Accepts an options hash and creates setter and getter attribute methods from the hash keys.
    # After initializing methods the values are added to the attributes.
    # Taken with gratitude from davidbella https://gist.github.com/davidbella/6918455
    def initialize(options = {})
      options.each do |attr, value|
        create_setters_from_opts(attr)
        create_getters_from_opts(attr)
        add_value_to_attr(attr, value)
      end
    end

    private

    def add_value_to_attr(attr, value)
      send("#{attr}=".to_sym, value)
    end

    def create_getters_from_opts(attr)
      self.class.send(:define_method, attr.to_sym) do
        instance_variable_get('@' + attr.to_s)
      end
    end

    def create_setters_from_opts(attr)
      self.class.send(:define_method, "#{attr}=".to_sym) do |val|
        instance_variable_set('@' + attr.to_s, val)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
get_your_rep-1.0.5 lib/get_your_rep/responses/base.rb
get_your_rep-1.0.4 lib/get_your_rep/responses/base.rb
get_your_rep-1.0.3 lib/get_your_rep/responses/base.rb
get_your_rep-1.0.2 lib/get_your_rep/responses/base.rb
get_your_rep-1.0.1 lib/get_your_rep/responses/base.rb
get_your_rep-1.0.0 lib/get_your_rep/responses/base.rb