abstract class Crysda::CustomColumnValue

Overview

Base class for storing custom values inside columns. Crystal restricts usage of base Reference or Object classes in Unions so we need a mechanism to bypass that restriction. Child classes inheriting this base class need to only override #hashcode method to ensure that call to this method returns the unique and consistent value on each invocation. This #hashcode is different from one provided by the language, as that ensures the consistency during same run, but provides different value on app different runs. Hashing value is used in Grouping to ensure the order, so relying on Crystal implementation would reveal different grouping results on different runs. Shard provides HashBuilder interface which should be used to calculate the #hashcode, as this ensures the same hashcode returns on each and every invocation. It is advised to override to_s methods, so that you see useful information when data is printed to console via schema and/or print method. Sample Usage

class Address < CustomColumnValue
  getter street : String
  getter city : String

  def initialize(@street, @city)
  end

  def to_s
    "#{street}, #{city}"
  end

  def to_s(io : IO) : Nil
    io << to_s
  end

  def hashcode : Int64
    hb = HashBuilder.new
    hb.add(@street).add(@city).hashcode
  end
end

Defined in:

crysda/context.cr

Instance Method Summary

Instance Method Detail

abstract def hashcode : Int64 #

returns the hash code value for this object. this should be consistent in returning the value


[View source]