Skip to content
This repository has been archived by the owner. It is now read-only.
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.
Ruby
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
spec
.gitignore
CHANGELOG
README.textile
hash_struct.gemspec

README.textile

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.

You can’t perform that action at this time.