🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

value_object_struct

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

value_object_struct

0.6.1
Rubygems
Version published
Maintainers
1
Created
Source

= ValueObjectStruct

The ValueObjectStruct module uses Ruby's native Struct class to facilitate the declaration of immutable value object classes.

ValueObjectStruct classes are defined with ValueObjectStruct.class_with_attributes and a list of member attributes specified by Symbol. Like Struct classes, they may be assigned to a constant or variable, or used in a superclass declaration.

ValueObjectStruct instances are Struct instances with private attribute write access, including both named attribute writers and Hash or Array style bracketed subscript assignment. ValueObjectStruct instances created with ::value (as opposed to ::new or ::for_values) are frozen, so that attribute values may not be reassigned after initialization, even by private methods.

ValueObjectStruct instances do not enforce immutability of attribute values themselves, so it may be desirable to freeze mutable value types such as String, Array and Hash before using them to initialize an instance.

As Struct instances, ValueObjectStruct instances provide equality and hash semantics via #== and #hash. #empty? returns true for value object instances with no non-nil attribute values.

ValueObjectStruct instances provide an #attributes and #attribute method for compatibility with ActiveModel. #attributes returns a hash representation of attribute values. #attribute is an alias for #[].

== Example

class Address < ValueObjectStruct.class_with_attributes(:street,:zip); end address = Address.new(street: "123 Main", zip: 11211)

address.street = "123 Maple" # raises NoMethodError address[:street] = "456 Elm" # raises NoMethodError address[1] = "789 Pine" # raises NoMethodError

address == Address.new(street: "123 Main", zip: 11211) # => true address == Address.new(street: "123 Maple", zip: 11211) # => false

set = Set[address] set.include? Address.new(street: "123 Main", zip: 11211) # => true set.include? Address.new(street: "123 Main", zip: 11212) # => false

== Copyright

Copyright (c) 2012 Riley Lynch, Teleological Software, LLC. See LICENSE.txt for further details.

FAQs

Package last updated on 14 Mar 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts