Class: ValueObject::Base
- Inherits:
-
Object
- Object
- ValueObject::Base
- Defined in:
- lib/value_object.rb
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #copy_with(attrs) ⇒ Object
- #empty? ⇒ Boolean
- #eql?(other) ⇒ Boolean
-
#hash ⇒ Object
if the class is the same, and the fields’ values are the same, the hash should be the same.
-
#initialize(*args) ⇒ Base
constructor
A new instance of Base.
- #to_hash ⇒ Object
Constructor Details
#initialize(*args) ⇒ Base
Returns a new instance of Base.
16 17 18 19 20 21 22 |
# File 'lib/value_object.rb', line 16 def initialize(*args) if args.length == 1 && args[0].is_a?(Hash) fields.each { |f| instance_variable_set "@#{f}", args[0][f] } else fields.each_with_index { |f, idx| instance_variable_set "@#{f}", args[idx] } end end |
Class Method Details
.has_fields(*args) ⇒ Object
10 11 12 13 |
# File 'lib/value_object.rb', line 10 def has_fields(*args) attr_reader(*args) self.fields += args end |
Instance Method Details
#==(other) ⇒ Object
29 30 31 |
# File 'lib/value_object.rb', line 29 def ==(other) eql?(other) end |
#copy_with(attrs) ⇒ Object
43 44 45 46 |
# File 'lib/value_object.rb', line 43 def copy_with(attrs) new_attrs = self.to_hash.update(attrs) self.class.new(new_attrs) end |
#empty? ⇒ Boolean
33 34 35 |
# File 'lib/value_object.rb', line 33 def empty? fields.all? { |f| public_send(f).nil? } end |
#eql?(other) ⇒ Boolean
24 25 26 27 |
# File 'lib/value_object.rb', line 24 def eql?(other) return false if self.class != other.class fields.all? { |f| public_send(f) == other.public_send(f) } end |
#hash ⇒ Object
if the class is the same, and the fields’ values are the same, the hash should be the same
50 51 52 |
# File 'lib/value_object.rb', line 50 def hash to_hash.hash + self.class.hash end |
#to_hash ⇒ Object
37 38 39 40 41 |
# File 'lib/value_object.rb', line 37 def to_hash hash = {} fields.each { |f| hash[f] = public_send(f) } hash end |