Class: ActiveGraph::Core::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/active_graph/core/element.rb

Direct Known Subclasses

Label, Type

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Element

Returns a new instance of Element.



7
8
9
# File 'lib/active_graph/core/element.rb', line 7

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/active_graph/core/element.rb', line 4

def name
  @name
end

Class Method Details

.drop_constraintsObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/active_graph/core/element.rb', line 99

def drop_constraints
  result = ActiveGraph::Base.read_transaction do |tx|
    tx.run('SHOW CONSTRAINTS YIELD *').to_a
  end
  ActiveGraph::Base.write_transaction do |tx|
    result.each do |record|
      tx.run("DROP CONSTRAINT `#{record[:name]}`")
    end
  end
end

.drop_indexesObject



93
94
95
96
97
# File 'lib/active_graph/core/element.rb', line 93

def drop_indexes
  indexes.each do |definition|
    ActiveGraph::Base.query("DROP INDEX #{definition[:name]}") unless definition[:owningConstraint]
  end
end

.indexesObject



89
90
91
# File 'lib/active_graph/core/element.rb', line 89

def indexes
  ActiveGraph::Base.indexes
end

Instance Method Details

#constraint?(property) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/active_graph/core/element.rb', line 78

def constraint?(property)
  constraints.any? { |definition| definition[:properties] == [property.to_sym] }
end

#constraints(_options = {}) ⇒ Object



66
67
68
69
70
# File 'lib/active_graph/core/element.rb', line 66

def constraints(_options = {})
  ActiveGraph::Base.constraints.select do |definition|
    definition[:label] == @name.to_sym
  end
end

#create_constraint(property, type: :key) ⇒ Object

Creates a neo4j constraint on a property See docs.neo4j.org/chunked/stable/query-constraints.html

Examples:

label = ActiveGraph::Label.create(:person)
label.create_constraint(:name, {type: :unique})


31
32
33
34
35
# File 'lib/active_graph/core/element.rb', line 31

def create_constraint(property, type: :key)
  schema_query(
    "CREATE CONSTRAINT #{constraint_name(property, type:)} FOR #{pattern("n:`#{name}`")} REQUIRE n.`#{property}` IS #{constraint_type(type:)}"
  )
end

#create_index(*properties, **options) ⇒ Object



11
12
13
14
15
# File 'lib/active_graph/core/element.rb', line 11

def create_index(*properties, **options)
  validate_index_options!(options)
  properties = properties.map { |p| "l.#{p}" }
  schema_query("CREATE INDEX FOR (l:`#{@name}`) ON (#{properties.join('.')})")
end

#drop_constraint(property, type: :key) ⇒ Object

Drops a neo4j constraint on a property See docs.neo4j.org/chunked/stable/query-constraints.html

Examples:

label = ActiveGraph::Label.create(:person)
label.create_constraint(:name, {type: :unique})
label.drop_constraint(:name, {type: :unique})


44
45
46
# File 'lib/active_graph/core/element.rb', line 44

def drop_constraint(property, type: :key)
  schema_query("DROP CONSTRAINT #{constraint_name(property, type:)} IF EXISTS")
end

#drop_index(property, options = {}) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/active_graph/core/element.rb', line 17

def drop_index(property, options = {})
  validate_index_options!(options)
  schema_query("SHOW INDEXES YIELD * WHERE labelsOrTypes = $labels AND properties = $properties",
               labels: [@name], properties: [property]).each do |record|
    schema_query("DROP INDEX #{record[:name]}")
  end
end

#drop_indexesObject



58
59
60
# File 'lib/active_graph/core/element.rb', line 58

def drop_indexes
  self.class.drop_indexes
end

#drop_uniqueness_constraint(property, options = {}) ⇒ Object



48
49
50
# File 'lib/active_graph/core/element.rb', line 48

def drop_uniqueness_constraint(property, options = {})
  drop_constraint(property, type: :unique)
end

#index?(property) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/active_graph/core/element.rb', line 62

def index?(property)
  indexes.any? { |definition| definition[:properties] == [property.to_sym] }
end

#indexesObject



52
53
54
55
56
# File 'lib/active_graph/core/element.rb', line 52

def indexes
  self.class.indexes.select do |definition|
    definition[:label] == @name.to_sym
  end
end

#uniqueness_constraint?(property) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/active_graph/core/element.rb', line 82

def uniqueness_constraint?(property)
  uniqueness_constraints.include?([property])
end

#uniqueness_constraints(_options = {}) ⇒ Object



72
73
74
75
76
# File 'lib/active_graph/core/element.rb', line 72

def uniqueness_constraints(_options = {})
  constraints.select do |definition|
    definition[:type] == :uniqueness
  end
end