Class: SteamCodec::KeyValues

Inherits:
InsensitiveHash
  • Object
show all
Defined in:
lib/steam_codec/key_values.rb

Overview

Direct Known Subclasses

VDF

Defined Under Namespace

Classes: Parser

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

:nodoc:



133
134
135
136
# File 'lib/steam_codec/key_values.rb', line 133

def method_missing(name, *args, &block) # :nodoc:
    return self[name] if args.empty? and block.nil? and key?(name)
    super
end

Class Method Details

.load(data) ⇒ Object



102
103
104
105
# File 'lib/steam_codec/key_values.rb', line 102

def self.load(data)
    json = self::Parser::toJSON(data)
    self.loadFromJSON(json)
end

.loadFromFile(file) ⇒ Object

Raises:

  • (ArgumentError)


96
97
98
99
100
# File 'lib/steam_codec/key_values.rb', line 96

def self.loadFromFile(file)
    raise ArgumentError, "file must respond to :read" unless file.respond_to?(:read)
    json = self::Parser::toJSON(file.read)
    self.loadFromJSON(json)
end

.loadFromJSON(json) ⇒ Object



90
91
92
93
94
# File 'lib/steam_codec/key_values.rb', line 90

def self.loadFromJSON(json)
    JSON.parse(json, {:object_class => self})
rescue JSON::ParserError
    nil
end

Instance Method Details

#asArray(name, seperator = '_') ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/steam_codec/key_values.rb', line 118

def asArray(name, seperator = '_')
    data = []
    counter = 0
    cname = name+seperator+counter.to_s
    data << self[cname] if key?(cname)
    counter += 1
    cname = name+seperator+counter.to_s
    while key?(cname) do
        data << self[cname]
        counter += 1
        cname = name+seperator+counter.to_s
    end
    data
end

#get(path = '', seperator = '/') ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/steam_codec/key_values.rb', line 107

def get(path = '', seperator = '/')
    escaped = Regexp.quote(seperator)
    fields = path.gsub(/^#{escaped}|#{escaped}$/, '').split(seperator)
    current = self
    fields.each do |name|
        return nil if not current.key?(name)
        current = current[name]
    end
    current
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


138
139
140
141
# File 'lib/steam_codec/key_values.rb', line 138

def respond_to_missing?(name, include_private = false) # :nodoc:
    return true if key?(name)
    super
end