Module: Neo4j::Core::Property

Included in:
Node, Relationship
Defined in:
lib/neo4j-core/property/java.rb,
lib/neo4j-core/property/property.rb

Defined Under Namespace

Modules: Java

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

Returns the value of the given key or nil if the property does not exist.

Returns:

  • the value of the given key or nil if the property does not exist.



51
52
53
54
55
# File 'lib/neo4j-core/property/property.rb', line 51

def [](key)
  return unless property?(key)
  val = get_property(key.to_s)
  val.class.superclass == ArrayJavaProxy ? val.to_a : val
end

#[]=(key, value) ⇒ Object

Sets the property of this node. Property keys are always strings. Valid property value types are the primitives(String, Fixnum, Float, FalseClass, TrueClass) or array of those primitives.

Gotchas

  • Values in the array must be of the same type.

  • You can not delete or add one item in the array (e.g. person.phones.delete(‘123’)) but instead you must create a new array instead.

Parameters:

  • key (String, Symbol)

    of the property to set

  • value (String, Fixnum, Float, true, false, Array)

    to set



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/neo4j-core/property/property.rb', line 66

def []=(key, value)
  k = key.to_s
  if value.nil?
    remove_property(k)
  elsif (Array === value)
    case value[0]
      when NilClass
        set_property(k, [].to_java(:string))
      when String
        set_property(k, value.to_java(:string))
      when Float
        set_property(k, value.to_java(:double))
      when FalseClass, TrueClass
        set_property(k, value.to_java(:boolean))
      when Fixnum
        set_property(k, value.to_java(:long))
      else
        raise "Not allowed to store array with value #{value[0]} type #{value[0].class}"
    end
  else
    set_property(k, value)
  end
end

#neo_idFixnum

Ids are garbage collected over time so they are only guaranteed to be unique during a specific time span: if the node is deleted, it’s likely that a new node at some point will get the old id. Note: this makes node ids brittle as public APIs.

Returns:

  • (Fixnum)

    the unique id of this node.



18
19
20
# File 'lib/neo4j-core/property/property.rb', line 18

def neo_id
  getId
end

#property?(key) ⇒ true false

Returns true if the given key exist as a property.

Parameters:

  • the (#to_s)

    property we want to check if it exist.

Returns:

  • (true false)

    true if the given key exist as a property.



24
25
26
# File 'lib/neo4j-core/property/property.rb', line 24

def property?(key)
  has_property?(key.to_s)
end

#propsHash

Returns all properties plus the id of the node with the key _neo_id.

Returns:

  • (Hash)

    all properties plus the id of the node with the key _neo_id



6
7
8
9
10
11
12
# File 'lib/neo4j-core/property/property.rb', line 6

def props
  ret = {"_neo_id" => neo_id}
  property_keys.each do |key|
    ret[key] = get_property(key)
  end
  ret
end

#update(struct_or_hash, options = {}) ⇒ Object

Updates this node/relationship’s properties by using the provided struct/hash. If the option {:strict => true} is given, any properties present on the node but not present in the hash will be removed from the node.

Parameters:

  • struct_or_hash (Hash, :each_pair)

    the key and value to be set

  • options (Hash) (defaults to: {})

    further options defining the context of the update

Options Hash (options):

  • :strict (Boolean)

    any properties present on the node but not present in the hash will be removed from the node if true

Returns:

  • self



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/neo4j-core/property/property.rb', line 36

def update(struct_or_hash, options={})
  strict = options[:strict]
  keys_to_delete = props.keys - %w(_neo_id _classname) if strict
  struct_or_hash.each_pair do |key, value|
    next if key.to_s[0..0] == '_'
    # do not allow special properties to be mass assigned
    keys_to_delete.delete(key.to_s) if strict
    self[key] = value
  end
  keys_to_delete.each { |key| remove_property(key) } if strict
  self
end