Class: UserInput::TypeSafeHash

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/user_input/type_safe_hash.rb

Overview

TypeSafeHash is a read-only hash that requires you to specify an expected type when retrieving elements from it. When fetching an item from the hash, you must specify the type as the second argument. It uses the type specified’s from_user_input member to determine if the value is valid and then uses it.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ TypeSafeHash

Initializes the type safe hash based on an existing normal hash.



14
15
16
17
18
19
20
21
22
# File 'lib/user_input/type_safe_hash.rb', line 14

def initialize(hash = {})
	if (hash.kind_of? TypeSafeHash)
		@real_hash = hash.to_hash
	elsif (hash.kind_of? Hash)
		@real_hash = hash
	else
		raise ArgumentError, "TypeSafeHash expects a hash object for its initializer"
	end
end

Instance Attribute Details

#real_hashObject (readonly)

Returns the value of attribute real_hash.



9
10
11
# File 'lib/user_input/type_safe_hash.rb', line 9

def real_hash
  @real_hash
end

Class Method Details

.from_user_input(value) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/user_input/type_safe_hash.rb', line 121

def self.from_user_input(value)
	if (value.kind_of?(Hash))
		return TypeSafeHash.new(value)
	else
		return nil
	end
end

Instance Method Details

#==(other) ⇒ Object

Compares a type safe hash with another.



25
26
27
28
29
30
31
# File 'lib/user_input/type_safe_hash.rb', line 25

def ==(other)
	if (other.respond_to?(:real_hash))
		return real_hash == other.real_hash
	else
		return false
	end
end

#each_keyObject Also known as: each

Enumerates the keys in the hash.



54
55
56
# File 'lib/user_input/type_safe_hash.rb', line 54

def each_key()
	real_hash.each_key() { |key| yield key; }
end

#each_match(regex, type, default = nil) ⇒ Object

Enumerates keys that match a regex, passing the match object and the value.



71
72
73
74
75
76
77
78
79
80
# File 'lib/user_input/type_safe_hash.rb', line 71

def each_match(regex, type, default = nil)
	real_hash.each_key() { |key|
		if (matchinfo = regex.match(key))
			value = fetch(key, type, default)
			if (!value.nil?)
				yield(matchinfo, value)
			end
		end
	}
end

#each_pair(type, default = nil) ⇒ Object

Enumerates the key, value pairs in the has.



60
61
62
63
64
65
66
67
# File 'lib/user_input/type_safe_hash.rb', line 60

def each_pair(type, default = nil)
	real_hash.each_key() { |key|
		value = fetch(key, type, default)
		if (!value.nil?)
			yield(key, value)
		end
	}
end

#empty?Boolean

Returns true if the hash is empty.

Returns:



83
84
85
# File 'lib/user_input/type_safe_hash.rb', line 83

def empty?()
	return real_hash.empty?()
end

#fetch(key, type, default = nil) ⇒ Object Also known as: []

Retrieves an item from the hash based on the key. If it’s not there, or doesn’t validate with type.from_user_input(value), returns default.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/user_input/type_safe_hash.rb', line 35

def fetch(key, type, default = nil)
	if (real_hash.has_key?(key))
		value = real_hash[key]

		# if type is not an array, but value is, flatten it.
		if (type != Array && !type.kind_of?(Array) && value.kind_of?(Array))
			value = value[0]
		end

		real = type.from_user_input(value)
		if (real != nil)
			return real
		end
	end
	return default
end

#has_key?(key) ⇒ Boolean Also known as: include?, key?, member?

Returns true if key is in the hash.

Returns:



88
89
90
# File 'lib/user_input/type_safe_hash.rb', line 88

def has_key?(key)
	return real_hash.has_key?(key)
end

#inspectObject

Returns a string representation of the inner hash.



96
97
98
# File 'lib/user_input/type_safe_hash.rb', line 96

def inspect()
	return real_hash.inspect()
end

#keys(sorted = false) ⇒ Object

Returns an array of the keys in the hash.



101
102
103
# File 'lib/user_input/type_safe_hash.rb', line 101

def keys(sorted=false)
	return sorted ? real_hash.keys().sort() : real_hash.keys()
end

#lengthObject Also known as: size

Returns the number of elements in the hash.



111
112
113
# File 'lib/user_input/type_safe_hash.rb', line 111

def length()
	return real_hash.length()
end

#to_hashObject

Returns the inner hash.



117
118
119
# File 'lib/user_input/type_safe_hash.rb', line 117

def to_hash
	return @real_hash
end

#to_sObject

Returns a string representation of the inner hash.



130
131
132
# File 'lib/user_input/type_safe_hash.rb', line 130

def to_s()
	return real_hash.to_s
end

#valuesObject

Returns an array of the values in the hash.



106
107
108
# File 'lib/user_input/type_safe_hash.rb', line 106

def values()
	return real_hash.values()
end