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.
User = HashStruct.generate(:first_name, :last_name) @user = User.new(first_name: "Jakub", last_name: "Stastny")
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.