Module: CPEE::Persistence

Defined in:
lib/cpee/persistence.rb

Class Method Summary collapse

Class Method Details

.extract_handler(id, opts, key) ⇒ Object

}}}



125
126
127
# File 'lib/cpee/persistence.rb', line 125

def self::extract_handler(id,opts,key) #{{{
  opts[:redis].smembers("instance:#{id}/handlers/#{key}")
end

.extract_handlers(id, opts) ⇒ Object

}}}



128
129
130
131
132
# File 'lib/cpee/persistence.rb', line 128

def self::extract_handlers(id,opts) #{{{
  opts[:redis].smembers("instance:#{id}/handlers").map do |e|
    [e, opts[:redis].get("instance:#{id}/handlers/#{e}/url")]
  end
end

.extract_item(id, opts, item) ⇒ Object

}}}



88
89
90
# File 'lib/cpee/persistence.rb', line 88

def self::extract_item(id,opts,item) #{{{
  opts[:redis].get("instance:#{id}/#{item}")
end

.extract_list(id, opts, item) ⇒ Object

}}}



70
71
72
73
74
# File 'lib/cpee/persistence.rb', line 70

def self::extract_list(id,opts,item) #{{{
  opts[:redis].zrange("instance:#{id}/#{item}",0,-1).map do |e|
    [e,opts[:redis].get("instance:#{id}/#{item}/#{e}")]
  end
end

.extract_set(id, opts, item) ⇒ Object

}}}



65
66
67
68
69
# File 'lib/cpee/persistence.rb', line 65

def self::extract_set(id,opts,item) #{{{
  opts[:redis].smembers("instance:#{id}/#{item}").map do |e|
    [e,opts[:redis].get("instance:#{id}/#{item}/#{e}")]
  end
end

.set_handler(id, opts, key, url, values, update = false) ⇒ Object

}}}



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cpee/persistence.rb', line 92

def self::set_handler(id,opts,key,url,values,update=false) #{{{
  exis = opts[:redis].smembers("instance:#{id}/handlers/#{key}")

  if update == false && exis.length > 0
    return 405
  end

  ah = AttributesHelper.new
  attributes = Persistence::extract_list(id,opts,'attributes').to_h
  dataelements = Persistence::extract_list(id,opts,'dataelements').to_h
  endpoints = Persistence::extract_list(id,opts,'endpoints').to_h

  deleted = exis - values

  CPEE::Message::send(
    :event,
    'handler/change',
    opts[:url],
    id,
    Persistence::extract_item(id,opts,'attributes/uuid'),
    Persistence::extract_item(id,opts,'attributes/info'),
    {
      :key => key,
      :url => url,
      :changed => values,
      :deleted => deleted,
      :attributes => ah.translate(attributes,dataelements,endpoints),
    },
    opts[:redis]
  )

  200
end

.set_item(id, opts, item, value) ⇒ Object

}}}



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cpee/persistence.rb', line 76

def self::set_item(id,opts,item,value) #{{{
  CPEE::Message::send(
    :event,
    File.join(item,'change'),
    opts[:url],
    id,
    Persistence::extract_item(id,opts,'attributes/uuid'),
    Persistence::extract_item(id,opts,'attributes/info'),
    value,
    opts[:redis]
  )
end

.set_list(id, opts, item, values, deleted = []) ⇒ Object

}}}



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cpee/persistence.rb', line 44

def self::set_list(id,opts,item,values,deleted=[]) #{{{
  ah = AttributesHelper.new
  attributes = Persistence::extract_list(id,opts,'attributes').to_h
  dataelements = Persistence::extract_list(id,opts,'dataelements').to_h
  endpoints = Persistence::extract_list(id,opts,'endpoints').to_h
  CPEE::Message::send(
    :event,
    File.join(item,'change'),
    opts[:url],
    id,
    Persistence::extract_item(id,opts,'attributes/uuid'),
    Persistence::extract_item(id,opts,'attributes/info'),
    {
      :changed => values.keys,
      :deleted => deleted,
      :values => values,
      :attributes => ah.translate(attributes,dataelements,endpoints),
    },
    opts[:redis]
  )
end

.write_instance(id, opts) ⇒ Object

{{{



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cpee/persistence.rb', line 18

def self::write_instance(id,opts) #{{{
  Dir.mkdir(File.join(opts[:instances],id.to_s)) rescue nil
  FileUtils.copy(opts[:backend_run],File.join(opts[:instances],id.to_s))
  dsl = CPEE::Persistence::extract_item(id,opts,'dsl')
  hw = CPEE::Persistence::extract_item(id,opts,'handlerwrapper')
  endpoints = CPEE::Persistence::extract_list(id,opts,'endpoints')
  dataelements = CPEE::Persistence::extract_list(id,opts,'dataelements')
  positions = CPEE::Persistence::extract_set(id,opts,'positions')
  positions.map! do |k, v|
    [ k, v, CPEE::Persistence::extract_item(id,opts,File.join('positions',k,'@passthrough')) ]
  end
  File.open(File.join(opts[:instances],id.to_s,opts[:backend_opts]),'w') do |f|
    YAML::dump({
      :host => opts[:host],
      :url => opts[:url],
      :redis_path => opts[:redis_path],
      :redis_db => opts[:redis_db],
      :global_handlerwrappers => opts[:global_handlerwrappers],
      :handlerwrappers => opts[:handlerwrappers]
    },f)
  end
  template = ERB.new(File.read(opts[:backend_template]), trim_mode: '-')
  res = template.result_with_hash(dsl: dsl, handlerwrapper: hw, dataelements: dataelements, endpoints: endpoints, positions: positions)
  File.write(File.join(opts[:instances],id.to_s,opts[:backend_instance]),res)
end