Module: Neon::PropertyContainer::Rest

Includes:
TransactionHelpers::Rest
Included in:
Node::Rest, Relationship::Rest
Defined in:
lib/neon/property_container.rb

Overview

Server implementation for a property container

Instance Method Summary collapse

Methods included from TransactionHelpers::Rest

#run_rest_transaction

Instance Method Details

#==(other) ⇒ Boolean

Compares to anothe property container

Parameters:

Returns:

  • (Boolean)

    wether both are the same entities based on their ids and sessions



13
14
15
# File 'lib/neon/property_container.rb', line 13

def ==(other)
  @id == other.id && @session == other.session
end

#[](*keys) ⇒ Array<String>, String

Fetch one or more properties e.g. node[:property, :another_Property]. Non existent keys return nil.

Parameters:

  • keys (Array<String, Symbol>)

    the properties to return

Returns:

  • (Array<String>, String)

    an array of the values of the properties, sorted in the same order they were queried. In case only a single property is fetche e.g. node[ :property] it returns a String containing the corresponding value.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/neon/property_container.rb', line 23

def [](*keys)
  # Lesson Learnt
  # ==============
  # Don't change what doesn't belong to you
  keys = keys.map(&:to_s)
  run_in_transaction(:[], *keys) do
    properties = props # Fetch all properties as this is more efficient than firing a HTTP request for every key
    result = []
    keys.each { |k| result << properties[k] }
    # If a single key was asked then return it's value else return an array of values in the correct order
    if keys.length == 1
      result.first
    else
      result
    end
  end
rescue NoMethodError => e
  _raise_doesnt_exist_anymore_error(e)
end

#[]=(*keys, values) ⇒ void

This method returns an undefined value.

Set one or more properties e.g. node[:property, :another_property] = 5, “Neo4J”. nil keys are ignored.

Parameters:

  • keys (Array<String, Symbol>)

    the properties to set.

  • values (Array<Numeric, String, Symbol, Array<Numeric, String, Symbol>>)

    the value to assign to the properties in the order specified.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/neon/property_container.rb', line 49

def []=(*keys, values)
  # Flattent the values to 1 level. This creates an arrray of values in the case only a single value is provided.
  values = [values].flatten(1)
  keys = keys.map(&:to_s)
  run_in_transaction(:[]=, *keys, values) do
    properties = props
    Hash[keys.zip(values)].each { |k, v| properties[k] = v unless k.nil? }
    self.props = properties # Reset all the properties - write simple inefficient code until it proves inefficient
  end
rescue NoMethodError => e
  _raise_doesnt_exist_anymore_error(e)
end

#delObject

Delete this entity.



89
90
91
92
93
94
95
96
# File 'lib/neon/property_container.rb', line 89

def del
  run_in_transaction(:del) do
    _delete
    _set_private_vars_to_nil
  end
rescue NoMethodError => e
  _raise_doesnt_exist_anymore_error(e)
end

#destroyObject

Destroy this entity i.e. delete it and it’s associated entities e.g. relationships of a node and in case of relationships, both its nodes.



100
101
102
103
104
105
106
107
# File 'lib/neon/property_container.rb', line 100

def destroy
  run_in_transaction(:destroy) do
    _destroy # Delete the entity after deleting connected entities
    _set_private_vars_to_nil
  end
rescue NoMethodError => e
  _raise_doesnt_exist_anymore_error(e)
end

#propsHash

Return all properties of the property container.

Returns:

  • (Hash)

    a hash of all properties and their values.



65
66
67
68
69
70
71
# File 'lib/neon/property_container.rb', line 65

def props
  run_in_transaction(:props) do
    _get_properties || {}
  end
rescue NoMethodError => e
  _raise_doesnt_exist_anymore_error(e)
end

#props=(attributes) ⇒ void

This method returns an undefined value.

Reset all properties of the property container.

Parameters:

  • attributes (Hash)

    a hash of the key-value pairs to set.



78
79
80
81
82
83
84
85
86
# File 'lib/neon/property_container.rb', line 78

def props=(attributes)
  attributes.delete_if { |key, value| key.nil? || value.nil? } # Remove keys-value pairs where either is nil
  run_in_transaction(:props=, attributes) do
    _reset_properties(attributes)
    return
  end
rescue NoMethodError => e
  _raise_doesnt_exist_anymore_error(e)
end