Module: Neon::PropertyContainer::Embedded
- Includes:
- TransactionHelpers::Embedded
- Defined in:
- lib/neon/property_container.rb
Overview
Embedded implementation for a property container
Class Method Summary collapse
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.
-
#id ⇒ FixNum
The id of the entity.
-
#props ⇒ Hash
Return all properties of the property container.
-
#props=(attributes) ⇒ void
Reset all properties of the property container.
Methods included from TransactionHelpers::Embedded
Class Method Details
.included(klazz) ⇒ Object
133 134 135 |
# File 'lib/neon/property_container.rb', line 133 def self.included(klazz) raise "Cannot include PropertyContainer::Embedded without JRuby" unless RUBY_PLATFORM == 'java' end |
Instance Method Details
#==(other) ⇒ Boolean
Compares to anothe property container
147 148 149 |
# File 'lib/neon/property_container.rb', line 147 def ==(other) id == other.id end |
#[](*keys) ⇒ Array<String>, String
Fetch one or more properties e.g. node[:property, :another_Property]. Non existent keys return nil.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/neon/property_container.rb', line 157 def [](*keys) run_in_transaction do keys = _serialize(keys) result = [] keys.each do |k| # Result contains the values for the keys asked or nil if they key does not exist result << if has_property(k) get_property(k) else nil end end # 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 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.
184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/neon/property_container.rb', line 184 def []=(*keys, values) run_in_transaction do # 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) attributes = Hash[keys.zip values] nil_values = lambda { |_, v| v.nil? } # Resusable lambda keys_to_delete = attributes.select(&nil_values).keys # Get the keys to be removed attributes.delete_if(&nil_values) # Now remove those keys from attributes keys_to_delete.each { |k| remove_property(k) if has_property(k) } # Remove the keys to be deleted if they are valid attributes = _serialize(attributes) attributes.each { |k, v| set_property(k, v) } # Set key-value pairs for remaining attributes end end |
#del ⇒ Object
Delete this entity
224 225 226 |
# File 'lib/neon/property_container.rb', line 224 def del run_in_transaction { delete } 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.
230 231 232 |
# File 'lib/neon/property_container.rb', line 230 def destroy run_in_transaction { _destroy } end |
#id ⇒ FixNum
Returns the id of the entity.
138 139 140 |
# File 'lib/neon/property_container.rb', line 138 def id get_id end |
#props ⇒ Hash
Return all properties of the property container.
201 202 203 204 205 206 207 |
# File 'lib/neon/property_container.rb', line 201 def props run_in_transaction do result = {} # Initialize results get_property_keys.each { |key| result[key] = get_property(key) } # Populate the hash with the container's key-value pairs result # Return the result end end |
#props=(attributes) ⇒ void
This method returns an undefined value.
Reset all properties of the property container.
214 215 216 217 218 219 220 221 |
# File 'lib/neon/property_container.rb', line 214 def props=(attributes) run_in_transaction do attributes.delete_if { |key, value| key.nil? || value.nil? } # Remove keys-value pairs where either is nil before serialization attributes = _serialize(attributes) get_property_keys.each { |key| remove_property(key) } # Remove all properties attributes.each { |key, value| set_property(key, value) } # Set key-value pairs end end |