Module: Hold::ObjectCell

Includes:
Cell
Included in:
IdentitySetRepository::IdCell, InMemory::ObjectCell, ObjectPropertyCell
Defined in:
lib/hold/interfaces/object_cell.rb

Overview

Interface extending Cell which offers some object-property-specific persistence methods for use only with Structs/Objects. Default implementations are in terms of get and set, but it’s expected that you’d override with more efficient implementations.

Defined Under Namespace

Classes: ArrayPropertyCell, ObjectPropertyCell, PropertyCell

Instance Method Summary collapse

Methods included from Cell

#can_get_class?, #can_set_class?, #clear, #empty?, #get_unless_empty, #set_if_empty, #set_unless_empty, #value, #value=

Instance Method Details

#clear_property(property_name) ⇒ Object



25
26
27
28
29
# File 'lib/hold/interfaces/object_cell.rb', line 25

def clear_property(property_name)
  value = get()
  value.delete(property_name)
  set(value)
end

#get_properties(*properties) ⇒ Object



35
36
37
# File 'lib/hold/interfaces/object_cell.rb', line 35

def get_properties(*properties)
  properties.map {|p| get_property(p)}
end

#get_property(property_name) ⇒ Object

default implementation gets the entire object in order to get the property in question. you might want to override with something more efficient



12
13
14
# File 'lib/hold/interfaces/object_cell.rb', line 12

def get_property(property_name)
  value = get() and value[property_name]
end

#has_property?(property_name) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/hold/interfaces/object_cell.rb', line 31

def has_property?(property_name)
  !get_property(property_name).nil?
end

#property_cell(property_name) ⇒ Object

May return a Cell which allows get / set / potentially other operations on a particular property of this object in the context of its parent object.

Be careful about the semantics if exposing property cells which allow partial write operations (like set_property) on the property value in the context of the parent object. If you do this it should only update the property value in that context, not in all contexts.

By analogy to normal ruby hashes, it should mean this:

a[:foo] = a[:foo].merge(:bar => 3)

rather than this:

a[:foo][:bar] = 3

which would have an effect visible to any other object holding a reference to a.

If you want the latter, you probably want to be updating a in some hold cell which is canonical for the identity of that object.

If you don’t want the former, don’t return a PropertyCell which allows partial updates. For simplicity’s sake this is the stance taken by the default PropertyCell implementation.



61
62
63
# File 'lib/hold/interfaces/object_cell.rb', line 61

def property_cell(property_name)
  PropertyCell.new(self, property_name)
end

#set_property(property_name, value) ⇒ Object

default implementation gets the entire object and replaces it with a version with the property in question changed. you might want to override with something more efficient.



19
20
21
22
23
# File 'lib/hold/interfaces/object_cell.rb', line 19

def set_property(property_name, value)
  object = get()
  object[property_name] = value
  set(object)
end