Class: Topolys::Object
- Inherits:
-
Object
- Object
- Topolys::Object
- Defined in:
- lib/topolys/model.rb
Overview
Model
Instance Attribute Summary collapse
-
#attributes ⇒ -
Attribute linked to a pre-speficied key (e.g. keyword).
-
#children ⇒ Array
readonly
Array of child Objects.
-
#id ⇒ String
readonly
Unique string id.
-
#parents ⇒ Array
readonly
Array of parent Objects.
Class Method Summary collapse
-
.link(parent, child) ⇒ Object
Links a parent with a child object.
-
.unlink(parent, child) ⇒ Object
Unlinks a parent from a child object.
Instance Method Summary collapse
-
#child_class ⇒ Class
Class of Child objects.
- #debug(str) ⇒ Object
-
#hash ⇒ String
Unique string id.
-
#initialize ⇒ Object
constructor
Initialize the object with read only attributes.
-
#link_child(object) ⇒ Object
Links a child object.
-
#link_parent(object) ⇒ Object
Links a parent object.
-
#parent_class ⇒ Class
Class of Parent objects.
-
#recalculate ⇒ Object
Must be called when read only attributes are updated externally.
-
#short_id ⇒ String
Short id used for Graphviz.
-
#short_name ⇒ String
Short name used for Graphviz.
-
#to_json ⇒ Hash
Hash containing JSON serialized fields.
-
#to_s ⇒ String
To string.
-
#unlink_child(object) ⇒ Object
Unlinks a child object.
-
#unlink_parent(object) ⇒ Object
Unlinks a parent object.
Constructor Details
#initialize ⇒ Object
Initialize the object with read only attributes. If read only attributes are changed externally, must call recalculate.
689 690 691 692 693 694 |
# File 'lib/topolys/model.rb', line 689 def initialize @attributes = {} @id = SecureRandom.uuid @parents = [] @children = [] end |
Instance Attribute Details
#attributes ⇒ -
Returns attribute linked to a pre-speficied key (e.g. keyword).
667 668 669 |
# File 'lib/topolys/model.rb', line 667 def attributes @attributes end |
#children ⇒ Array (readonly)
Returns Array of child Objects.
683 684 685 |
# File 'lib/topolys/model.rb', line 683 def children @children end |
#id ⇒ String (readonly)
Returns Unique string id.
677 678 679 |
# File 'lib/topolys/model.rb', line 677 def id @id end |
#parents ⇒ Array (readonly)
Returns Array of parent Objects.
680 681 682 |
# File 'lib/topolys/model.rb', line 680 def parents @parents end |
Class Method Details
Instance Method Details
#child_class ⇒ Class
Returns Class of Child objects.
741 742 743 |
# File 'lib/topolys/model.rb', line 741 def child_class NilClass end |
#debug(str) ⇒ Object
731 732 733 |
# File 'lib/topolys/model.rb', line 731 def debug(str) #puts "#{str}#{self.class} #{short_id} has [#{@parents.map{|p| p.short_id}.join(', ')}] parents and [#{@children.map{|c| c.short_id}.join(', ')}] children" end |
#hash ⇒ String
Returns Unique string id.
705 706 707 |
# File 'lib/topolys/model.rb', line 705 def hash @id end |
#link_child(object) ⇒ Object
Links a child object
789 790 791 792 793 794 |
# File 'lib/topolys/model.rb', line 789 def link_child(object) #puts "link child #{object.short_id} with parent #{self.short_id}" if object && object.is_a?(child_class) @children << object if !@children.find {|obj| obj.id == object.id } end end |
#link_parent(object) ⇒ Object
Links a parent object
769 770 771 772 773 774 |
# File 'lib/topolys/model.rb', line 769 def link_parent(object) #puts "link parent #{object.short_id} with child #{self.short_id}" if object && object.is_a?(parent_class) @parents << object if !@parents.find {|obj| obj.id == object.id } end end |
#parent_class ⇒ Class
Returns Class of Parent objects.
736 737 738 |
# File 'lib/topolys/model.rb', line 736 def parent_class NilClass end |
#recalculate ⇒ Object
Must be called when read only attributes are updated externally. Recalculates cached attribute values and links children with parents. Throws if a class invariant is violated.
701 702 |
# File 'lib/topolys/model.rb', line 701 def recalculate end |
#short_id ⇒ String
Returns Short id used for Graphviz.
717 718 719 |
# File 'lib/topolys/model.rb', line 717 def short_id @id.slice(0,6) end |
#short_name ⇒ String
Returns Short name used for Graphviz.
722 723 724 |
# File 'lib/topolys/model.rb', line 722 def short_name "#{self.class.to_s.gsub('Topolys::','').gsub('DirectedEdge', 'DEdge')}_#{short_id}" end |
#to_json ⇒ Hash
Returns Hash containing JSON serialized fields.
710 711 712 713 714 |
# File 'lib/topolys/model.rb', line 710 def to_json result = { id: @id} result[:attributes] = @attributes if !@attributes.empty? return result end |
#to_s ⇒ String
Returns To string.
727 728 729 |
# File 'lib/topolys/model.rb', line 727 def to_s short_name end |
#unlink_child(object) ⇒ Object
Unlinks a child object
800 801 802 803 |
# File 'lib/topolys/model.rb', line 800 def unlink_child(object) #puts "unlink child #{object.short_id} from parent #{self.short_id}" @children.reject!{ |obj| obj.id == object.id } end |
#unlink_parent(object) ⇒ Object
Unlinks a parent object
780 781 782 783 |
# File 'lib/topolys/model.rb', line 780 def unlink_parent(object) #puts "unlink parent #{object.short_id} from child #{self.short_id}" @parents.reject!{ |obj| obj.id == object.id } end |