Sha256: 62f21af48f56af8126198ea9dcd033f25b9d0a8962bc0af6b13c4494365123a3

Contents?: true

Size: 914 Bytes

Versions: 16

Compression:

Stored size: 914 Bytes

Contents

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

    def getKeys
      getKeysNextLevel('', self)
    end

    def checkFilter(filter)
      return true if filter.nil?
      key, value = filter.split(':', 2)
      getValue(key) == value
    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
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
stf-client-0.3.0.rc10 lib/stf/model/device.rb
stf-client-0.3.0.rc9 lib/stf/model/device.rb
stf-client-0.3.0.rc8 lib/stf/model/device.rb
stf-client-0.3.0.rc7 lib/stf/model/device.rb
stf-client-0.3.0.rc6 lib/stf/model/device.rb
stf-client-0.3.0.rc5 lib/stf/model/device.rb
stf-client-0.3.0.rc4 lib/stf/model/device.rb
stf-client-0.3.0.rc3 lib/stf/model/device.rb
stf-client-0.3.0.rc2 lib/stf/model/device.rb
stf-client-0.2.6 lib/stf/model/device.rb
stf-client-0.2.5 lib/stf/model/device.rb
stf-client-0.2.4 lib/stf/model/device.rb
stf-client-0.2.3 lib/stf/model/device.rb
stf-client-0.2.2 lib/stf/model/device.rb
stf-client-0.2.1 lib/stf/model/device.rb
stf-client-0.2.0 lib/stf/model/device.rb