Module: GHTorrent::Utils
Class Method Summary collapse
Instance Method Summary collapse
-
#read_value(from, key) ⇒ Object
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.
- #user_type(type) ⇒ Object
-
#write_value(to, key, value) ⇒ Object
Overwrite an existing
keywhose format is “foo.bar” (where a dot represents one level deep in the hierarchy) in hashtowithvalue.
Class Method Details
.included(other) ⇒ Object
6 7 8 |
# File 'lib/ghtorrent/utils.rb', line 6 def self.included(other) other.extend self end |
Instance Method Details
#read_value(from, key) ⇒ Object
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.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# 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) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/ghtorrent/utils.rb', line 53 def user_type(type) if type == "User" "USR" else "ORG" end end |
#write_value(to, key, value) ⇒ Object
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
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# 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 |