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

Methods included from TransactionHelpers::Embedded

#run_in_transaction

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

Parameters:

Returns:

  • (Boolean)

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



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.

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.



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.

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.



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

#delObject

Delete this entity



224
225
226
# File 'lib/neon/property_container.rb', line 224

def del
  run_in_transaction { delete }
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.



230
231
232
# File 'lib/neon/property_container.rb', line 230

def destroy
  run_in_transaction { _destroy }
end

#idFixNum

Returns the id of the entity.

Returns:

  • (FixNum)

    the id of the entity.



138
139
140
# File 'lib/neon/property_container.rb', line 138

def id
  get_id
end

#propsHash

Return all properties of the property container.

Returns:

  • (Hash)

    a hash of all properties and their values.



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.

Parameters:

  • attributes (Hash)

    a hash of the key-value pairs to set.



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