0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
HashStruct is similar to Struct from Ruby standard library, the difference is that HashStruct.generate creates a class which takes a hash with attributes rather than just positional arguments as Struct.new does.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

About

HashStruct is similar to Struct from Ruby standard library, the difference is that HashStruct.generate creates a class which takes a hash with attributes rather than just positional arguments as Struct.new does.

The point is that this is behaviour which all the model have, so you can use HashStruct to test thinks which expect something what quacks like a model class.

Usage

User  = HashStruct.generate(:first_name, :last_name)
@user = User.new(first_name: "Jakub", last_name: "Stastny")

Inheritance

module TestMixin
  def say_hi() puts("Hi!") end
end

HashStruct.extend(TestMixin)
User = HashStruct.generate(:first_name, :last_name)
User.say_hi
# => "Hi!"
HashStruct.send(:include, TestMixin)
User = HashStruct.generate(:first_name, :last_name)
User.new.say_hi
# => "Hi!"

You may want to create your custom class inhterited from HashStruct in case you want to extend it.