Skip to content
This repository has been archived by the owner on Apr 18, 2019. It is now read-only.
/ hash_struct Public archive

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

Notifications You must be signed in to change notification settings

botanicus/hash_struct

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages