Module: Diametric::Persistence::Peer

Defined in:
lib/diametric/persistence/peer.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object


120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/diametric/persistence/peer.rb', line 120

def method_missing(method_name, *args, &block)
  functions = self.instance_variable_get("@transaction_functions")
  if functions && functions.include?(method_name)
    return invoke_function(method_name, args)
  end
  result = /(.+)_from_this_(.+)/.match(method_name)
  if result
    query_string = ":#{result[1]}/_#{result[2]}"
    entities = Diametric::Persistence::Peer.reverse_q(args[0].db, self.dbid, query_string)
    entities.collect {|e| self.class.reify(e, args[0])}
  else
    super
  end
end

Instance Method Details

#invoke_function(method_name, args) ⇒ Object


135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/diametric/persistence/peer.rb', line 135

def invoke_function(method_name, args)
  params = args.dup
  conn = params.shift
  attribute_names = self.class.attribute_names
  params = params.map do |e|
    if attribute_names.include?(e)
      e = (self.class.prefix + "/" + e.to_s).to_sym
    else
      e
    end
  end
  conn.transact([[method_name, self.dbid, *params]]).get
  self.class.reify(self.dbid, conn)
end

#parse_array_data(c_array, result) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/diametric/persistence/peer.rb', line 81

def parse_array_data(c_array, result)
  array = []
  c_array.each do |c_value|
    if c_value.respond_to?(:to_a)
      a_values = []
      c_value.to_a.each do |a_value|
        if a_value.respond_to?(:tx_data) && a_value.tx_data.empty?
          a_values << a_value.dbid
        else
          a_values << a_value
        end
      end
      array << a_values
    else
      array << c_value
    end
  end
  result << array
end

#parse_hash_data(c_hash, result, queue) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/diametric/persistence/peer.rb', line 48

def parse_hash_data(c_hash, result, queue)
    hash = {}
    c_hash.each do |c_key, c_value|
      if c_value.respond_to?(:tx_data)
        if c_value.tx_data.empty?
          hash[c_key] = c_value.dbid
        else
          c_value.dbid = c_value.tempid
          hash[c_key] = c_value.dbid
          queue << c_value.tx_data.first
        end
      elsif c_value.is_a? Set
        set_value = Set.new
        c_value.each do |s|
          if s.respond_to?(:tx_data) && s.tx_data.empty?
            set_value << s.dbid
          elsif s.respond_to?(:tx_data)
            set_value << s.tx_data[":db/id"]
            parsed_tx_data(s, result)
          else
            set_value << s
          end
        end
        hash[c_key] = set_value
      elsif c_value.respond_to?(:eid)
        hash[c_key] = c_value.eid
      else
        hash[c_key] = c_value
      end
    end
    result << hash
end

#parse_tx_data(data, result) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/diametric/persistence/peer.rb', line 36

def parse_tx_data(data, result)
  queue = []
  data.each do |c_data|
    if c_data.is_a? Hash
      parse_hash_data(c_data, result, queue)
    elsif c_data.is_a? Array
      parse_array_data(c_data, result)
    end
  end
  parse_tx_data(queue, result) unless queue.empty?
end

#resolve_changes(parents, map) ⇒ Object


101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/diametric/persistence/peer.rb', line 101

def resolve_changes(parents, map)
  queue = []
  parents.each do |child|
    child.attributes.each do |a_key, a_val|
      if a_val.respond_to?(:tx_data)
        queue << a_val
      end
    end
    child.instance_variable_set("@previously_changed", child.changes)
    child.changed_attributes.clear
    child.dbid = resolve_tempid(map, child.dbid)
  end
  resolve_changes(queue, map) unless queue.empty?
end

#resolve_tempid(map, id) ⇒ Object


29
30
31
32
33
34
# File 'lib/diametric/persistence/peer.rb', line 29

def resolve_tempid(map, id)
  if id.to_s =~ /-\d+/
    return Diametric::Persistence::Peer.resolve_tempid(map, id)
  end
  return id
end

#retract_entity(dbid) ⇒ Object


116
117
118
# File 'lib/diametric/persistence/peer.rb', line 116

def retract_entity(dbid)
  Diametric::Persistence::Peer.retract_entity(dbid)
end

#save(connection = nil) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/diametric/persistence/peer.rb', line 6

def save(connection=nil)
  return false unless valid?
  return true unless changed?
  connection ||= Diametric::Persistence::Peer.connect

  parsed_data = []
  parse_tx_data(tx_data, parsed_data)

  # TODO: exception handling. error message should be found by errors.messages
  map = connection.transact(parsed_data).get

=begin
    resolve_fn = Java::ClojureLang::RT.var("datomic.api", "resolve-tempid")
    tempids_key = Java::ClojureLang::Keyword.intern("tempids")
    ids = map.to_java.get(tempids_key)
    db  = connection.db.to_java
    resolve_fn.invoke(db, ids, self.dbid.to_java)
=end
  resolve_changes([self], map)

  map
end