Class: MasterCard::Core::Model::SmartMap

Inherits:
Object
  • Object
show all
Defined in:
lib/mastercard/core/model.rb

Overview

SmartMap

Direct Known Subclasses

CaseInsensitiveMap, RequestMap

Constant Summary collapse

KEY_LIST =
"list"

Instance Method Summary collapse

Constructor Details

#initializeSmartMap

Returns a new instance of SmartMap.



38
39
40
41
# File 'lib/mastercard/core/model.rb', line 38

def initialize
  @properties = Hash.new
  @parentWithSquareBracket = /\[(.*)\]/
end

Instance Method Details

#containsKeys(key) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/mastercard/core/model.rb', line 163

def containsKeys(key)

  #checks if map contains the key

  unless get(key).nil?
    return true
  end

  return false

end

#get(key) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
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
125
126
127
# File 'lib/mastercard/core/model.rb', line 80

def get(key)
  #Gets the value from the map associated with the key

  #Sets the the value of key as value
  #Get the list of keys
  keys = key.split(".")

  #Number of keys
  keys_len = keys.length

  #Copy properties in sub properties so we can walk over it
  @subProperty = @properties

  count = 0

  keys.each do |part_key|
    count += 1
    #check if the key is of form key[0]
    match = @parentWithSquareBracket.match(part_key)

    #if the final key is of form key[0]
    unless match.nil?
      begin
        moveInList(part_key,match)
      rescue
        return nil
      end
      #If this is the last key
      if count == keys_len
        return @subProperty
      end
    else # Move in the map

      if @subProperty.is_a?(Hash) && @subProperty.key?(part_key)
        if count == keys_len #this is the final key, return the value
          return @subProperty[part_key]
        else #Move in the map
          @subProperty = @subProperty[part_key]
        end
      else
        return nil
      end

    end

  end

end

#getObjectObject



151
152
153
154
# File 'lib/mastercard/core/model.rb', line 151

def getObject
  #Returns the basemap internal object
  return @properties
end

#set(key, value) ⇒ Object



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
69
70
71
72
73
74
75
76
77
78
# File 'lib/mastercard/core/model.rb', line 43

def set(key,value)
  #Sets the the value of key as value
  #Get the list of keys
  keys = key.split(".")

  #Number of keys
  keys_len = keys.length

  #Copy properties in sub properties so we can walk over it
  @subProperty = @properties

  count = 0

  keys.each do |part_key|
    count += 1

    #If we are at the final key, then we need to set this key for subProperty
    if count == keys_len
      match = @parentWithSquareBracket.match(part_key)
      #If match is nil then we can set the value
      if match.nil?
        @subProperty[part_key] = value
      else #if the key is like key[0]
        handleListTypeKeys(part_key,match,value)
      end
    else
      #walk inside subProperty
      createMap(part_key)
    end


  end

  return @properties

end

#setAll(map) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/mastercard/core/model.rb', line 129

def setAll(map)

  #  Uses the hash object to create the base map object

  initialKey = ""

  #If given object is an array then the created map should have a key called list first

  if map.is_a?(Array)
    initialKey = KEY_LIST
  end

  #Iterate over the object and keeps on adding the items to the properties object
  iterateItemsAndAdd(map,initialKey)

  return @properties


end

#sizeObject



156
157
158
159
160
161
# File 'lib/mastercard/core/model.rb', line 156

def size
  #
  #Returns the number of keys at the first level of the object
  #
  return @properties.length
end