Module: Neography::Property

Included in:
Node, Relationship
Defined in:
lib/neography/property.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/neography/property.rb', line 79

def method_missing(method_sym, *arguments, &block)
  if (method_sym.to_s =~ /=$/) != nil
    new_ostruct_member(method_sym.to_s.chomp("="), *arguments)

    # We just defined the getter/setter above, but we haven't actually
    # applied them yet.
    self.send(method_sym, *arguments)
  else
    super
  end
end

Instance Method Details

#[](key) ⇒ Object



4
5
6
# File 'lib/neography/property.rb', line 4

def [](key)
  @table[key.to_sym]
end

#[]=(key, value) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/neography/property.rb', line 8

def []=(key, value)
  key = key.to_sym
  k_str = key.to_s
  if value.nil?
    unless @table[key].nil?
      if node?
        neo_server.remove_node_properties(self.neo_id, [k_str])
      else
        neo_server.remove_relationship_properties(self.neo_id, [k_str])
      end
    end
    remove_ostruct_member(key)
  else
    if node?
      neo_server.set_node_properties(self.neo_id, {k_str => value})
    else
      neo_server.set_relationship_properties(self.neo_id, {k_str => value})
    end
    new_ostruct_member(key, value)
  end
end

#add_or_remove_ostruct_member(name, value) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/neography/property.rb', line 49

def add_or_remove_ostruct_member(name, value)
  if value.nil?
    remove_ostruct_member(name)
  else
    new_ostruct_member(name, value)
  end
end

#attributesObject



91
92
93
# File 'lib/neography/property.rb', line 91

def attributes
  @table.keys
end

#new_ostruct_member(name, value) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/neography/property.rb', line 57

def new_ostruct_member(name, value)
  name = name.to_sym
  @table[name] = value
  unless self.respond_to?(name)
    meta = class << self; self; end
    meta.send(:define_method, name) { @table[name] }
    meta.send(:define_method, "#{name}=") do |new_value|
      self[name.to_sym] = new_value
    end
  end
  name
end

#node?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/neography/property.rb', line 95

def node?
  self.is_a?(Neography::Node)
end

#remove_ostruct_member(name) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/neography/property.rb', line 70

def remove_ostruct_member(name)
  @table.delete(name.to_sym)
  meta = class << self; self; end
  names = [name, "#{name}="].map(&:to_sym)
  names.each do |n|
    meta.send(:remove_method, n) if self.respond_to?(n)
  end
end

#reset_properties(hash) ⇒ Object

As #set_properties, but this one hard resets the node’s/relationship’s properties to exactly what’s given in the hash.



43
44
45
46
47
# File 'lib/neography/property.rb', line 43

def reset_properties(hash)
  @table.keys.each{|key| remove_ostruct_member(key)}
  hash.each{|key,value| new_ostruct_member(key,value)}
  rest_reset_properties
end

#set_properties(hash) ⇒ Object

Set many properties at once and only issue one http request and update the node/relationship instance on the fly.

To remove a property, set its value to nil.



34
35
36
37
38
39
# File 'lib/neography/property.rb', line 34

def set_properties(hash)
  hash.each do |key, value|
    add_or_remove_ostruct_member(key, value)
  end
  rest_reset_properties
end