Class: Topolys::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/topolys/model.rb

Overview

Model

Direct Known Subclasses

DirectedEdge, Edge, Face, Shell, Vertex, Wire

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject

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).

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

#childrenArray (readonly)

Returns Array of child Objects.

Returns:

  • (Array)

    Array of child Objects



683
684
685
# File 'lib/topolys/model.rb', line 683

def children
  @children
end

#idString (readonly)

Returns Unique string id.

Returns:

  • (String)

    Unique string id



677
678
679
# File 'lib/topolys/model.rb', line 677

def id
  @id
end

#parentsArray (readonly)

Returns Array of parent Objects.

Returns:

  • (Array)

    Array of parent Objects



680
681
682
# File 'lib/topolys/model.rb', line 680

def parents
  @parents
end

Class Method Details

Links a parent with a child object

Parameters:

  • parent (Object)

    A parent object to link

  • child (Object)

    A child object to link



750
751
752
753
# File 'lib/topolys/model.rb', line 750

def Object.link(parent, child)
  child.link_parent(parent)
  parent.link_child(child)
end

Unlinks a parent from a child object

Parameters:

  • parent (Object)

    A parent object to unlink

  • child (Object)

    A child object to unlink



760
761
762
763
# File 'lib/topolys/model.rb', line 760

def Object.unlink(parent, child)
  child.unlink_parent(parent)
  parent.unlink_child(child)
end

Instance Method Details

#child_classClass

Returns Class of Child objects.

Returns:

  • (Class)

    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

#hashString

Returns Unique string id.

Returns:

  • (String)

    Unique string id



705
706
707
# File 'lib/topolys/model.rb', line 705

def hash
  @id
end

Links a child object

Parameters:

  • object (Object)

    A child object to link



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

Links a parent object

Parameters:

  • object (Object)

    A parent object to link



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_classClass

Returns Class of Parent objects.

Returns:

  • (Class)

    Class of Parent objects



736
737
738
# File 'lib/topolys/model.rb', line 736

def parent_class
  NilClass
end

#recalculateObject

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_idString

Returns Short id used for Graphviz.

Returns:

  • (String)

    Short id used for Graphviz



717
718
719
# File 'lib/topolys/model.rb', line 717

def short_id
  @id.slice(0,6)
end

#short_nameString

Returns Short name used for Graphviz.

Returns:

  • (String)

    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_jsonHash

Returns Hash containing JSON serialized fields.

Returns:

  • (Hash)

    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_sString

Returns To string.

Returns:

  • (String)

    To string



727
728
729
# File 'lib/topolys/model.rb', line 727

def to_s
  short_name
end

Unlinks a child object

Parameters:

  • object (Object)

    A child object to unlink



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

Unlinks a parent object

Parameters:

  • object (Object)

    A parent object to unlink



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