module GHTorrent::Utils
Public Class Methods
included(other)
click to toggle source
# File lib/ghtorrent/utils.rb, line 6 def self.included(other) other.extend self end
Public Instance Methods
read_value(from, key)
click to toggle source
Read the value for a key whose format is “foo.bar.baz” from a hierarchical map, where a dot represents one level deep in the hierarchy.
# File lib/ghtorrent/utils.rb, line 12 def read_value(from, key) return from if key.nil? or key == "" key.split(/\./).reduce({}) do |acc, x| unless acc.nil? if acc.empty? # Initial run acc = from[x] else if acc.has_key?(x) acc = acc[x] else # Some intermediate key does not exist return nil end end else # Some intermediate key returned a null value # This indicates a malformed entry return nil end end end
user_type(type)
click to toggle source
# File lib/ghtorrent/utils.rb, line 53 def user_type(type) if type == "User" "USR" else "ORG" end end
write_value(to, key, value)
click to toggle source
Overwrite an existing key
whose format is “foo.bar” (where a dot represents one level deep in the hierarchy) in hash to
with value
. If the key does not exist, it will be added at the appropriate depth level
# File lib/ghtorrent/utils.rb, line 39 def write_value(to, key, value) return to if key.nil? or key == "" prev = nil key.split(/\./).reverse.each {|x| a = Hash.new a[x] = if prev.nil? then value else prev end prev = a a } to.merge_recursive(prev) end