| Total Complexity | 59 |
| Total Lines | 210 |
| Duplicated Lines | 4.76 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Document often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | class Symbol |
||
| 8 | class Document |
||
| 9 | attr_reader :dDocument |
||
| 10 | |||
| 11 | attr_accessor :bIsStrict |
||
| 12 | attr_accessor :bUseKeyAsDesc |
||
| 13 | attr_accessor :bUseDeepKeys |
||
| 14 | |||
| 15 | def initialize(dValues=nil,dSchema=nil,bDefaultifyDoc=false,bIsStrict=true,opts={}) |
||
| 16 | @dSchema = dSchema || self.getDefaultSchema() |
||
| 17 | @bDefaultifyDoc = bDefaultifyDoc ? true : false |
||
| 18 | @bIsStrict = bIsStrict ? true : false |
||
| 19 | @bUseKeyAsDesc = false |
||
| 20 | @bUseDeepKeys = opts.key?(:bUseDeepKeys) ? opts[:bUseDeepKeys] : true |
||
| 21 | @dDocument = self.getDefaultDocument() |
||
| 22 | self.loadHash(dValues) if dValues.is_a?(Hash) |
||
| 23 | end |
||
| 24 | |||
| 25 | def getDefaultSchema |
||
| 26 | { |
||
| 27 | type: '', |
||
| 28 | properties: { |
||
| 29 | id: {default: '', description: 'Doc Id', type: 'string'} |
||
| 30 | } |
||
| 31 | } |
||
| 32 | end |
||
| 33 | |||
| 34 | def getDefaultDocument |
||
| 35 | dDocument = {} |
||
| 36 | if @bDefaultifyDoc && @dSchema.key?(:properties) |
||
| 37 | @dSchema[:properties].keys.each do |yKey| |
||
| 38 | dProperty = @dSchema[:properties][yKey] |
||
| 39 | xxVal = dProperty.key?(:default) ? dProperty[:default] : '' |
||
| 40 | dDocument[yKey] = xxVal |
||
| 41 | end |
||
| 42 | end |
||
| 43 | dDocument |
||
| 44 | end |
||
| 45 | |||
| 46 | def loadHash(dValues = nil) |
||
| 47 | if dValues.nil? |
||
| 48 | return |
||
| 49 | elsif ! dValues.is_a?(Hash) |
||
| 50 | raise ArgumentError, 'E_INITIAL_VALUES_IS_NOT_A_HASH' |
||
| 51 | end |
||
| 52 | dValues.each do |yKey,xxVal| |
||
| 53 | self.setProp(yKey,xxVal) |
||
| 54 | end |
||
| 55 | self |
||
| 56 | end |
||
| 57 | |||
| 58 | def getProp(yKey = nil) |
||
| 59 | raise ArgumentError, 'E_BAD_KEY__IS_NIL' if yKey.nil? |
||
| 60 | |||
| 61 | yKey = yKey.to_sym if yKey.is_a?(String) |
||
| 62 | |||
| 63 | if @bUseDeepKeys |
||
| 64 | aKeys = yKey.split('.') # = yKey.to_s.split('.').map(&:to_sym) |
||
| 65 | |||
| 66 | dDoc = @dDocument |
||
| 67 | xxVal = getPropRecurse(aKeys.clone,dDoc) |
||
| 68 | return xxVal |
||
| 69 | end |
||
| 70 | return @dDocument.key?(yKey) ? @dDocument[yKey] : nil |
||
| 71 | end |
||
| 72 | |||
| 73 | def getPropRecurse(aKeys = [], dDoc = nil) |
||
| 74 | yKey = aKeys.shift |
||
| 75 | if ! yKey.is_a?(Symbol) || yKey.length<1 || ! dDoc.key?( yKey ) |
||
| 76 | return nil |
||
| 77 | end |
||
| 78 | xxVal = dDoc[ yKey ] |
||
| 79 | if aKeys.length == 0 |
||
| 80 | return xxVal |
||
| 81 | elsif dDoc.is_a?(Hash) |
||
| 82 | return getPropRecurse( aKeys, xxVal ) |
||
| 83 | else |
||
| 84 | raise ArgumentError, "E_BAD_VAL__IS_NOT_HASH" |
||
| 85 | end |
||
| 86 | end |
||
| 87 | |||
| 88 | def getPropSingle(yKey = nil) |
||
| 89 | raise ArgumentError, 'E_BAD_KEY__IS_NIL' if yKey.nil? |
||
| 90 | yKey = yKey.to_sym if yKey.is_a?(String) |
||
| 91 | xxVal = @dDocument.key?(yKey) ? @dDocument[yKey] : nil |
||
| 92 | if xxVal.nil? && @bIsStrict |
||
| 93 | self.validateKey(yKey) |
||
| 94 | end |
||
| 95 | xxVal |
||
| 96 | end |
||
| 97 | |||
| 98 | def validateKey(yKey = nil) |
||
| 99 | raise ArgumentError, "E_BAD_KEY__IS_NIL [#{yKey.to_s}]" if yKey.nil? |
||
| 100 | |||
| 101 | return true unless @bIsStrict |
||
| 102 | |||
| 103 | bKeyExists = @dSchema.key?(:properties) \ |
||
| 104 | && @dSchema[:properties].key?(yKey) ? true : false |
||
| 105 | |||
| 106 | raise ArgumentError, "E_UNKNOWN_KEY__STRICT #{yKey.to_s}" unless bKeyExists |
||
| 107 | |||
| 108 | return true |
||
| 109 | end |
||
| 110 | |||
| 111 | def setProp(yKey = nil, xxVal = nil) |
||
| 112 | yKey = yKey.to_sym if yKey.is_a?(String) |
||
| 113 | |||
| 114 | self.validateKey(yKey) |
||
| 115 | |||
| 116 | @dDocument[yKey] = xxVal |
||
| 117 | end |
||
| 118 | |||
| 119 | def pushProp(yKey = nil, xxVal = nil) |
||
| 120 | yKey = yKey.to_sym if yKey.is_a?(String) |
||
| 121 | self.validateKey(yKey) |
||
| 122 | |||
| 123 | if @dDocument.key?(yKey) |
||
| 124 | if @dDocument[yKey].is_a?(Array) |
||
| 125 | @dDocument[yKey].push xxVal |
||
| 126 | else |
||
| 127 | raise RuntimeError, 'E_PROPERTY_IS_NOT_ARRAY' |
||
| 128 | end |
||
| 129 | else |
||
| 130 | @dDocument[yKey] = [xxVal] |
||
| 131 | end |
||
| 132 | end |
||
| 133 | |||
| 134 | def cpProp(yKeySrc = nil, yKeyDest = nil) |
||
| 135 | yKeySrc = yKeySrc.to_sym if yKeySrc.is_a?(String) |
||
| 136 | yKeyDest = yKeyDest.to_sym if yKeyDest.is_a?(String) |
||
| 137 | self.setAttr(yKeyDest, self.getAttr(yKeySrc)) |
||
| 138 | end |
||
| 139 | |||
| 140 | def sortKeys |
||
| 141 | @dDocument.keys.sort! |
||
| 142 | end |
||
| 143 | |||
| 144 | def fromJson(jDocument = nil) |
||
| 145 | if jDocument.is_a?(String) |
||
| 146 | @dDocument = JSON.load(jDocument) |
||
| 147 | end |
||
| 148 | return self |
||
| 149 | end |
||
| 150 | |||
| 151 | def fromDict(dDocument = nil) |
||
| 152 | @dDocument = dDocument if dDocument.is_a?(Hash) |
||
| 153 | end |
||
| 154 | |||
| 155 | def asHash |
||
| 156 | @dDocument |
||
| 157 | end |
||
| 158 | |||
| 159 | def asJson |
||
| 160 | JSON.dump( self.asHash() ) |
||
| 161 | end |
||
| 162 | |||
| 163 | View Code Duplication | def getValStringForProperties(aCols = nil, sDelimiter = "\t") |
|
|
|
|||
| 164 | sDelimiter = "\t" unless sDelimiter.is_a?(String) && sDelimiter.length>0 |
||
| 165 | aVals = self.getValArrayForProperties(aCols) |
||
| 166 | return aVals.join(sDelimiter) |
||
| 167 | end |
||
| 168 | |||
| 169 | def getValArrayForProperties(aCols = nil, xxNil = '') |
||
| 170 | aVals = [] |
||
| 171 | return aVals if aCols.nil? |
||
| 172 | |||
| 173 | if @bUseKeyAsDesc |
||
| 174 | asVals = aCols.map {|x| x.to_s } |
||
| 175 | end |
||
| 176 | |||
| 177 | aCols.each do |yKey| |
||
| 178 | yKey = yKey.to_sym if yKey.is_a? String |
||
| 179 | xxVal = getProp( yKey ) |
||
| 180 | #xVal = @dDocument.key?(yKey) ? @dDocument[yKey] : nil |
||
| 181 | xxVal = xxNil if xxVal.nil? |
||
| 182 | aVals.push xxVal |
||
| 183 | end |
||
| 184 | aVals |
||
| 185 | end |
||
| 186 | |||
| 187 | View Code Duplication | def getDescStringForProperties(aCols = nil,sDelimiter = "\t") |
|
| 188 | sDelimiter = "\t" unless sDelimiter.is_a?(String) && sDelimiter.length>0 |
||
| 189 | aVals = self.getDescArrayForProperties(aCols) |
||
| 190 | aVals.join(sDelimiter) |
||
| 191 | end |
||
| 192 | |||
| 193 | def getDescArrayForProperties(aCols = nil) |
||
| 194 | aVals = [] |
||
| 195 | return aVals if aCols.nil? |
||
| 196 | aCols.each do |yKey| |
||
| 197 | yKey = yKey.to_sym if yKey.is_a? String |
||
| 198 | xxVal = ( |
||
| 199 | @dSchema.key?(:properties) \ |
||
| 200 | && @dSchema[:properties].key?(yKey) \ |
||
| 201 | && @dSchema[:properties][yKey].key?(:description) \ |
||
| 202 | && @dSchema[:properties][yKey][:description].length > 0 |
||
| 203 | ) \ |
||
| 204 | ? @dSchema[:properties][yKey][:description] : yKey.to_s |
||
| 205 | |||
| 206 | xxVal = xxVal.to_s unless xxVal.is_a? String |
||
| 207 | |||
| 208 | aVals.push xxVal |
||
| 209 | end |
||
| 210 | aVals |
||
| 211 | end |
||
| 212 | |||
| 213 | alias_method :setAttr , :setProp |
||
| 214 | alias_method :getAttr , :getProp |
||
| 215 | alias_method :pushAttr, :pushProp |
||
| 216 | alias_method :cpAttr , :cpProp |
||
| 217 | end |
||
| 218 | end |
||
| 219 |