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
-
#==(other) ⇒ Boolean
Compares to anothe property container.
-
#[](*keys) ⇒ Array<String>, String
Fetch one or more properties e.g.
-
#[]=(*keys, values) ⇒ void
Set one or more properties e.g.
-
#del ⇒ Object
Delete this entity.
-
#destroy ⇒ Object
Destroy this entity i.e.
-
#props ⇒ Hash
Return all properties of the property container.
-
#props=(attributes) ⇒ void
Reset all properties of the property container.
Methods included from TransactionHelpers::Rest
Instance Method Details
#==(other) ⇒ Boolean
Compares to anothe property container
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.
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.
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 |
#del ⇒ Object
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 |
#destroy ⇒ Object
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 |
#props ⇒ Hash
Return all properties of the property container.
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.
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 |