Sha256: c55a214bf45b37eecbed64d08bb83bbdeca8236472f9583151276fe3ffd4ee0b

Contents?: true

Size: 698 Bytes

Versions: 4

Compression:

Stored size: 698 Bytes

Contents

class Device < OpenStruct
  def getValue(key)
    getValueFromObject(self, key)
  end

  def getKeys
    getKeysNextLevel('', self)
  end

  def getKeysNextLevel(prefix, o)
    return [] if o.nil?

    o.each_pair.flat_map do |k, v|
      if v.is_a? OpenStruct
        getKeysNextLevel(concat(prefix, k.to_s), v)
      else
        [concat(prefix, k.to_s)]
      end
    end
  end

  def concat(prefix, key)
    prefix.to_s.empty? ? key : prefix + '.' + key
  end


  def getValueFromObject(obj, key)
    keys = key.split('.', 2)
    if keys[1].nil?
      obj[key]
    else
      getValueFromObject(obj[keys[0]], keys[1])
    end
  end

  private :getValueFromObject,:concat, :getKeysNextLevel

end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
stf-client-neofreko-0.1.9 lib/stf/model/device.rb
stf-client-neofreko-0.1.8 lib/stf/model/device.rb
stf-client-neofreko-0.1.7 lib/stf/model/device.rb
stf-client-0.1.6 lib/stf/model/device.rb