class JsonDoc::Document

Attributes

bIsStrict[RW]
bUseDeepKeys[RW]
bUseKeyAsDesc[RW]
dDocument[R]

Public Class Methods

new(dValues=nil,dSchema=nil,bDefaultifyDoc=false,bIsStrict=true,opts={}) click to toggle source
# File lib/jsondoc/document.rb, line 15
def initialize(dValues=nil,dSchema=nil,bDefaultifyDoc=false,bIsStrict=true,opts={})
  @dSchema        = dSchema || self.getDefaultSchema()
  @bDefaultifyDoc = bDefaultifyDoc ? true : false
  @bIsStrict      = bIsStrict ? true : false
  @bUseKeyAsDesc  = false
  @bUseDeepKeys   = opts.key?(:bUseDeepKeys) ? opts[:bUseDeepKeys] : true
  @dDocument      = self.getDefaultDocument()
  self.loadHash(dValues) if dValues.is_a?(Hash)
end

Public Instance Methods

asHash() click to toggle source
# File lib/jsondoc/document.rb, line 155
def asHash
  @dDocument
end
asJson() click to toggle source
# File lib/jsondoc/document.rb, line 159
def asJson
  JSON.dump( self.asHash() )
end
cpAttr(yKeySrc = nil, yKeyDest = nil)
Alias for: cpProp
cpProp(yKeySrc = nil, yKeyDest = nil) click to toggle source
# File lib/jsondoc/document.rb, line 134
def cpProp(yKeySrc = nil, yKeyDest = nil)
  yKeySrc = yKeySrc.to_sym if yKeySrc.is_a?(String)
  yKeyDest = yKeyDest.to_sym if yKeyDest.is_a?(String)
  self.setAttr(yKeyDest, self.getAttr(yKeySrc))
end
Also aliased as: cpAttr
fromDict(dDocument = nil) click to toggle source
# File lib/jsondoc/document.rb, line 151
def fromDict(dDocument = nil)
  @dDocument = dDocument if dDocument.is_a?(Hash)
end
fromJson(jDocument = nil) click to toggle source
# File lib/jsondoc/document.rb, line 144
def fromJson(jDocument = nil)
  if jDocument.is_a?(String)
    @dDocument = JSON.load(jDocument)
  end
  return self
end
getAttr(yKey = nil)
Alias for: getProp
getDefaultDocument() click to toggle source
# File lib/jsondoc/document.rb, line 34
def getDefaultDocument
  dDocument = {}
  if @bDefaultifyDoc && @dSchema.key?(:properties)
    @dSchema[:properties].keys.each do |yKey|
      dProperty = @dSchema[:properties][yKey]
      xxVal = dProperty.key?(:default) ? dProperty[:default] : ''
      dDocument[yKey] = xxVal
    end
  end
  dDocument
end
getDefaultSchema() click to toggle source
# File lib/jsondoc/document.rb, line 25
def getDefaultSchema
  {
    type: '',
    properties: {
      id: {default: '', description: 'Doc Id', type: 'string'}
    }
  }
end
getDescArrayForProperties(aCols = nil) click to toggle source
# File lib/jsondoc/document.rb, line 193
def getDescArrayForProperties(aCols = nil)
  aVals = []
  return aVals if aCols.nil?
  aCols.each do |yKey|
    yKey = yKey.to_sym if yKey.is_a? String
    xxVal = (
      @dSchema.key?(:properties)                          \
      && @dSchema[:properties].key?(yKey)                 \
      && @dSchema[:properties][yKey].key?(:description)   \
      && @dSchema[:properties][yKey][:description].length > 0
    ) \
      ? @dSchema[:properties][yKey][:description] : yKey.to_s

    xxVal = xxVal.to_s unless xxVal.is_a? String

    aVals.push xxVal
  end
  aVals
end
getDescStringForProperties(aCols = nil,sDelimiter = "\t") click to toggle source
# File lib/jsondoc/document.rb, line 187
def getDescStringForProperties(aCols = nil,sDelimiter = "\t")
  sDelimiter = "\t" unless sDelimiter.is_a?(String) && sDelimiter.length>0
  aVals = self.getDescArrayForProperties(aCols)
  aVals.join(sDelimiter)
end
getProp(yKey = nil) click to toggle source
# File lib/jsondoc/document.rb, line 58
def getProp(yKey = nil)
  raise ArgumentError, 'E_BAD_KEY__IS_NIL' if yKey.nil?

  yKey = yKey.to_sym if yKey.is_a?(String)

  if @bUseDeepKeys
    aKeys = yKey.split('.') # = yKey.to_s.split('.').map(&:to_sym)

    dDoc  = @dDocument
    xxVal = getPropRecurse(aKeys.clone,dDoc)
    return xxVal
  end
  return @dDocument.key?(yKey) ? @dDocument[yKey] : nil
end
Also aliased as: getAttr
getPropRecurse(aKeys = [], dDoc = nil) click to toggle source
# File lib/jsondoc/document.rb, line 73
def getPropRecurse(aKeys = [], dDoc = nil)
  yKey = aKeys.shift
  if ! yKey.is_a?(Symbol) || yKey.length<1 || ! dDoc.key?( yKey )
    return nil
  end
  xxVal = dDoc[ yKey ]
  if aKeys.length == 0
    return xxVal
  elsif dDoc.is_a?(Hash)
    return getPropRecurse( aKeys, xxVal )
  else
    raise ArgumentError, "E_BAD_VAL__IS_NOT_HASH"
  end
end
getPropSingle(yKey = nil) click to toggle source
# File lib/jsondoc/document.rb, line 88
def getPropSingle(yKey = nil)
  raise ArgumentError, 'E_BAD_KEY__IS_NIL' if yKey.nil?
  yKey = yKey.to_sym if yKey.is_a?(String)
  xxVal = @dDocument.key?(yKey) ? @dDocument[yKey] : nil
  if xxVal.nil? && @bIsStrict
    self.validateKey(yKey)
  end
  xxVal
end
getValArrayForProperties(aCols = nil, xxNil = '') click to toggle source
# File lib/jsondoc/document.rb, line 169
def getValArrayForProperties(aCols = nil, xxNil = '')
  aVals = []
  return aVals if aCols.nil?

  if @bUseKeyAsDesc
    asVals = aCols.map {|x| x.to_s }
  end

  aCols.each do |yKey|
    yKey  = yKey.to_sym if yKey.is_a? String
    xxVal = getProp( yKey )
    #xVal = @dDocument.key?(yKey) ? @dDocument[yKey] : nil
    xxVal = xxNil if xxVal.nil?
    aVals.push xxVal
  end
  aVals
end
getValStringForProperties(aCols = nil, sDelimiter = "\t") click to toggle source
# File lib/jsondoc/document.rb, line 163
def getValStringForProperties(aCols = nil, sDelimiter = "\t")
  sDelimiter = "\t" unless sDelimiter.is_a?(String) && sDelimiter.length>0
  aVals = self.getValArrayForProperties(aCols)
  return aVals.join(sDelimiter)
end
loadHash(dValues = nil) click to toggle source
# File lib/jsondoc/document.rb, line 46
def loadHash(dValues = nil)
  if dValues.nil?
    return
  elsif ! dValues.is_a?(Hash)
    raise ArgumentError, 'E_INITIAL_VALUES_IS_NOT_A_HASH'
  end
  dValues.each do |yKey,xxVal|
    self.setProp(yKey,xxVal)
  end
  self
end
pushAttr(yKey = nil, xxVal = nil)
Alias for: pushProp
pushProp(yKey = nil, xxVal = nil) click to toggle source
# File lib/jsondoc/document.rb, line 119
def pushProp(yKey = nil, xxVal = nil)
  yKey = yKey.to_sym if yKey.is_a?(String)
  self.validateKey(yKey)

  if @dDocument.key?(yKey)
    if @dDocument[yKey].is_a?(Array)
      @dDocument[yKey].push xxVal
    else
      raise RuntimeError, 'E_PROPERTY_IS_NOT_ARRAY'
    end
  else
    @dDocument[yKey] = [xxVal]
  end
end
Also aliased as: pushAttr
setAttr(yKey = nil, xxVal = nil)
Alias for: setProp
setProp(yKey = nil, xxVal = nil) click to toggle source
# File lib/jsondoc/document.rb, line 111
def setProp(yKey = nil, xxVal = nil)
  yKey = yKey.to_sym if yKey.is_a?(String)

  self.validateKey(yKey)

  @dDocument[yKey] = xxVal
end
Also aliased as: setAttr
sortKeys() click to toggle source
# File lib/jsondoc/document.rb, line 140
def sortKeys
  @dDocument.keys.sort!
end
validateKey(yKey = nil) click to toggle source
# File lib/jsondoc/document.rb, line 98
def validateKey(yKey = nil)
  raise ArgumentError, "E_BAD_KEY__IS_NIL [#{yKey.to_s}]" if yKey.nil?

  return true unless @bIsStrict

  bKeyExists = @dSchema.key?(:properties) \
    && @dSchema[:properties].key?(yKey) ? true : false

  raise ArgumentError, "E_UNKNOWN_KEY__STRICT #{yKey.to_s}" unless bKeyExists

  return true
end