Categories: None [Edit]

value

https://rubygems.org/gems/value
Value Value is a library for defining immutable value objects in Ruby. A value object is an object whose equality to other objects is determined by its value, not its identity, think dates and amounts of money. A value object should also be immutable, as you don’t want the date “2013-04-22” itself to change but the current date to change from “2013-04-22” to “2013-04-23”. That is, you don’t want entries in a calendar for 2013-04-22 to move to 2013-04-23 simply because the current date changes from 2013-04-22 to 2013-04-23. A value object consists of one or more attributes stored in instance variables. Value sets up an #initialize method for you that let’s you set these attributes, as, value objects being immutable, this’ll be your only chance to do so. Value also adds equality checks ‹#==› and ‹#eql?› (which are themselves equivalent), a ‹#hash› method, a nice ‹#inspect› method, and a protected attribute reader for each attribute. You may of course add any additional methods that your value object will benefit from. That’s basically all there’s too it. Let’s now look at using the Value library. § Usage You create value object class by invoking ‹#Value› inside the class (module) you wish to make into a value object class. Let’s create a class that represent points on a plane: class Point Value :x, :y end A ‹Point› is thus a value object consisting of two sub-values ‹x› and ‹y› (the coordinates). Just from invoking ‹#Value›, a ‹Point› object will have a constructor that takes two arguments to set instance variables ‹@x› and ‹@y›, equality checks ‹#==› and ‹#eql?› (which are the same), a ‹#hash› method, a nice ‹#inspect› method, and two protected attribute readers ‹#x› and ‹#y›. We can thus already creat ‹Point›s: origo = Point.new(0, 0) The default of making t...

Total

Ranking: 39,877 of 180,689
Downloads: 25,916

Daily

Ranking: 19,680 of 180,681
Downloads: 34

Depended by

RankDownloadsName
32,79133,482lookout

Depends on

RankDownloadsName
223143,556,118yard
22,19657,222inventory
32,79133,482lookout
34,12731,687inventory-rake
34,39031,368inventory-rake-tasks-yard
51,00818,712yard-heuristics
60,97614,715lookout-rake

Owners

#GravatarHandle
1iconnow