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
Constant Summary
- VALID_PROPERTY_VALUE_CLASSES =
the valid values on a property, and arrays of those.
Set.new([Array, NilClass, String, Float, TrueClass, FalseClass, Fixnum])
Instance Method Summary (collapse)
-
- (Object) [](key)
The value of the given key or nil if the property does not exist.
-
- (Object) []=(key, value)
Sets the property of this node.
-
- (Fixnum) neo_id
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.
-
- (true false) property?(key)
True if the given key exist as a property.
-
- (Hash) props
All properties plus the id of the node with the key _neo_id.
- - (Object) protected_keys
-
- (Object) update(struct_or_hash, options = {})
Updates this node/relationship's properties by using the provided struct/hash.
-
- (True, False) valid_property?(value)
A false means it can't be persisted.
Instance Method Details
- (Object) [](key)
The value of the given key or nil if the property does not exist.
69 70 71 72 73 |
# File 'lib/neo4j-core/property/property.rb', line 69 def [](key) return unless property?(key) val = get_property(key.to_s) val.class.superclass == ArrayJavaProxy ? val.to_a : val end |
- (Object) []=(key, value)
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.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/neo4j-core/property/property.rb', line 90 def []=(key, value) raise "Not valid Neo4j Property value #{value.class}, valid: #{VALID_PROPERTY_VALUE_CLASSES.to_a.join(', ')}" unless valid_property?(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 |
- (Fixnum) neo_id
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.
21 22 23 |
# File 'lib/neo4j-core/property/property.rb', line 21 def neo_id getId end |
- (true false) property?(key)
True if the given key exist as a property.
27 28 29 |
# File 'lib/neo4j-core/property/property.rb', line 27 def property?(key) has_property?(key.to_s) end |
- (Hash) props
All properties plus the id of the node with the key _neo_id
9 10 11 12 13 14 15 |
# File 'lib/neo4j-core/property/property.rb', line 9 def props ret = {"_neo_id" => neo_id} property_keys.each do |key| ret[key] = get_property(key) end ret end |
- (Object) protected_keys
64 65 66 |
# File 'lib/neo4j-core/property/property.rb', line 64 def protected_keys end |
- (Object) update(struct_or_hash, options = {})
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, except '_neo_id' and '_classname' (defined in Neo4j::Node.protected_keys). The option {:protected_keys => array of strings} is similar to the <code<:strict</code> option, except that it allows you to specify which keys will be protected from being updated or deleted. If neither the protected nor strict option is given then all properties starting with '_' will never be touched.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/neo4j-core/property/property.rb', line 43 def update(struct_or_hash, ={}) protected_keys = self.class.protected_keys if [:strict] protected_keys ||= [:protected_keys].map(&:to_s) if [:protected_keys] keys_to_delete = props.keys - protected_keys if protected_keys struct_or_hash.each_pair do |k, value| key = k.to_s # do not allow special properties to be mass assigned if protected_keys keys_to_delete.delete(key) next if protected_keys.include?(key) else next if key[0..0] == '_' end self[key] = value end keys_to_delete.each { |key| remove_property(key) } if protected_keys self end |
- (True, False) valid_property?(value)
A false means it can't be persisted.
77 78 79 |
# File 'lib/neo4j-core/property/property.rb', line 77 def valid_property?(value) VALID_PROPERTY_VALUE_CLASSES.include?(value.class) end |