class Levelup::Templates::DataParcel

A series of methods used to convert objects to/from hashes.

Public Class Methods

excluded?(instance_variable) click to toggle source

Determines if the specified instance variable is excluded from this object's generated hash.

# File lib/levelup/templates/data_parcel.rb, line 11
def self.excluded?(instance_variable)
  instance_variables_excluded_from_hash.include? instance_variable
end
instance_variables_excluded_from_hash() click to toggle source
# File lib/levelup/templates/data_parcel.rb, line 15
def self.instance_variables_excluded_from_hash
  [:excluded_from_hash]
end
new(hash = {}) click to toggle source
# File lib/levelup/templates/data_parcel.rb, line 5
def initialize(hash = {})
  assign_instance_variables_from_hash hash
end

Private Instance Methods

assign_instance_variables_from_hash(hash) click to toggle source

hash: A hash mapping instance variable names to their desired values

# File lib/levelup/templates/data_parcel.rb, line 22
def assign_instance_variables_from_hash(hash)
  hash.each { |key, value| public_send("#{key}=", value) }
end
to_hash() click to toggle source
# File lib/levelup/templates/data_parcel.rb, line 26
def to_hash
  body = {}

  instance_variables.each do |variable|
    instance_variable_name = variable.to_s.delete('@').to_sym

    unless self.class.excluded?(instance_variable_name)
      body[instance_variable_name] = instance_variable_get(variable)
    end
  end

  body
end