Class: MockController

Inherits:
Object show all
Defined in:
lib/providers/mock/mock.rb

Overview

This Mock controller keep the data in memory in hash/Array data.

Constant Summary collapse

@@data =

rubocop: disable ClassVars

{}

Instance Method Summary collapse

Instance Method Details

#create(sObjectType, hParams) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/providers/mock/mock.rb', line 30

def create(sObjectType, hParams)
  PrcLib.debug("Mock: create object '%s' with parameters (hdata) '%s'",
               sObjectType, hParams[:hdata])

  result = {}
  hParams[].keys.each do |key|
    next if key == :hdata
    result[key] = hParams[key]
  end
  result.merge!(hParams[:hdata])

  @@data[sObjectType] = [] unless @@data.key?(sObjectType)

  array = @@data[sObjectType]

  array.each do |value|
    fail if value.key?(result[:name])
  end
  array << result

  result[:id] = array.length - 1

  # Typical code:
  # ~ case sObjectType
  # ~ when :public_ip
  # Following function can be executed to ensure the object :connection exists
  # ~ required?(hParams, :connection)
  # ~ required?(hParams, :server)
  # ~ ... CODE to create
  # ~ else
  # ~ controller_error "'%s' is not a valid object for 'create'", sObjectType
  # ~ end
  # The code should return some data
  # This data will be encapsulated in Lorj::Data object.
  # data will be extracted by the framework with the controller get_attr
  # function and mapped.
  PrcLib.debug("Mock: object '%s' = '%s' is created.", sObjectType, result)
  result
end

#delete(sObjectType, hParams) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/providers/mock/mock.rb', line 91

def delete(sObjectType, hParams)
  PrcLib.debug("Mock: delete object '%s' with hdata '%s'",
               sObjectType, hParams[:hdata])
  return nil unless @@data.key?(sObjectType)

  return false if !hParams.exist?(sObjectType) || hParams[sObjectType].nil?
  @@data[sObjectType].delete(hParams[sObjectType])
  PrcLib.debug("Mock: object '%s' = '%s' is deleted.",
               sObjectType, hParams[sObjectType])
  true
end

#get(sObjectType, sUniqId, hParams) ⇒ Object



103
104
105
106
107
108
# File 'lib/providers/mock/mock.rb', line 103

def get(sObjectType, sUniqId, hParams)
  PrcLib.debug("Mock: Get object '%s' = '%s' with hdata '%s'",
               sObjectType, sUniqId, hParams[:hdata])
  return nil unless @@data.key?(sObjectType)
  @@data[sObjectType][sUniqId]
end

#get_attr(oControlerObject, key) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/providers/mock/mock.rb', line 110

def get_attr(oControlerObject, key)
  # This controller function read the data and
  # extract the information requested by the framework.
  # Those data will be mapped to the process data model.
  # The key is an array, to get data from a level tree.
  # [data_l1, data_l2, data_l3] => should retrieve data from structure like
  # data[ data_l2[ data_l3 ] ]
  attributes = oControlerObject

  attributes.rh_get(key)
 rescue => e
 controller_error "Unable to map '%s'.\n%s", key, e.message
end

#query(sObjectType, sQuery, hParams) ⇒ Object

This function return a collection which have to provide: functions: [], length, each Used by network process.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/providers/mock/mock.rb', line 73

def query(sObjectType, sQuery, hParams)
  PrcLib.debug("Mock: query object '%s' with hdata '%s' using query '%s'",
               sObjectType, hParams[:hdata], sQuery)

  return [] unless @@data.key?(sObjectType)

  result = []

  @@data[sObjectType].each do |value|
    elem = value
    sQuery.each do |query_key, query_value|
      elem = nil if !value.key?(query_key) || value[query_key] != query_value
    end
    result << elem if elem
  end
  result
end

#set_attr(oControlerObject, key, value) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/providers/mock/mock.rb', line 124

def set_attr(oControlerObject, key, value)
  attributes = oControlerObject

  attributes.rh_set(value, key)
 rescue => e
 controller_error "Unable to map '%s' on '%s'.\n%s",
                  key, sObjectType, e.message
end

#update(sObjectType, oObject, hParams) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/providers/mock/mock.rb', line 133

def update(sObjectType, oObject, hParams)
  PrcLib.debug("Mock: Update object '%s' = '%s' with hdata '%s'",
               sObjectType, sUniqId, hParams[:hdata])
  return false unless @@data.key?(sObjectType)

  return false unless @@data[sObjectType][oObject[:id]].nil?
  # Considered hash object is already updated.
  # This action emule the object save which doesn't make sense in this empty
  # Mock controller.
  true
end