module CrvApiClient::Helpers::Attributes

Public Instance Methods

attr_accessor_with_type(name, type) click to toggle source

defines an public attribute writer and reader. name: the instance variable name for which a get method has to be created type: The object type.

# File lib/crv_api_client/helpers/attributes.rb, line 16
def attr_accessor_with_type(name, type)
  define_attribute_methods(name, type)
end
attr_reader_with_type(name, type) click to toggle source

defines an public attribute reader and a private writer name: the instance variable name for which a get method has to be created type: The object type.

# File lib/crv_api_client/helpers/attributes.rb, line 8
def attr_reader_with_type(name, type)
  define_attribute_methods(name, type)
  class_eval { private "#{name}=" }
end

Private Instance Methods

define_attribute_methods(name, type) click to toggle source

defines the attribute set and get methods and

# File lib/crv_api_client/helpers/attributes.rb, line 22
def define_attribute_methods(name, type)
  define_method(name) do
    instance_variable_get("@#{name}")
  end

  define_method("#{name}=") do |value|
    raise ArgumentError.new("Invalid Type, #{value.class.name} is not a #{type.name} ") unless (value.is_a?(type) || value.nil?)
    instance_variable_set("@#{name}", value)
  end
end