Module: Kwalify::Util
- Defined in:
- lib/kwalify.rb,
lib/kwalify/util.rb,
lib/kwalify/util/hashlike.rb,
lib/kwalify/util/ordered-hash.rb
Defined Under Namespace
Modules: HashLike Classes: OrderedHash
Class Method Summary collapse
- ._traverse_rule(rule, _done = {}) {|rule| ... } ⇒ Object
- ._traverse_schema(hash, _done = {}) {|hash| ... } ⇒ Object
-
.create_hashtable(hashlist, primarykey, flag_duplicate_check = true) ⇒ Object
create a hash table from list of hash with primary key.
-
.get_class(classname) ⇒ Object
get class object.
-
.get_value(obj, path) ⇒ Object
get nested value directly.
-
.traverse_rule(validator, &block) ⇒ Object
traverse rule.
-
.traverse_schema(schema, &block) ⇒ Object
traverse schema.
-
.untabify(str, width = 8) ⇒ Object
expand tab character to spaces.
Class Method Details
._traverse_rule(rule, _done = {}) {|rule| ... } ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/kwalify/util.rb', line 77 def _traverse_rule(rule, _done={}, &block) return if _done.key?(rule.__id__) _done[rule.__id__] = rule yield rule rule.sequence.each do |seq_rule| _traverse_rule(seq_rule, _done, &block) end if rule.sequence rule.mapping.each do |name, map_rule| _traverse_rule(map_rule, _done, &block) end if rule.mapping end |
._traverse_schema(hash, _done = {}) {|hash| ... } ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/kwalify/util.rb', line 50 def _traverse_schema(hash, _done={}, &block) return if _done.key?(hash.__id__) _done[hash.__id__] = hash yield hash if hash['mapping'] hash['mapping'].each {|k, v| _traverse_schema(v, _done, &block) } elsif hash['sequence'] _traverse_schema(hash['sequence'][0], _done, &block) end end |
.create_hashtable(hashlist, primarykey, flag_duplicate_check = true) ⇒ Object
create a hash table from list of hash with primary key.
ex.
hashlist = [
{ "name"=>"Foo", "gender"=>"M", "age"=>20, },
{ "name"=>"Bar", "gender"=>"F", "age"=>25, },
{ "name"=>"Baz", "gender"=>"M", "age"=>30, },
]
hashtable = YamlHelper.create_hashtable(hashlist, "name")
p hashtable
# => { "Foo" => { "name"=>"Foo", "gender"=>"M", "age"=>20, },
# "Bar" => { "name"=>"Bar", "gender"=>"F", "age"=>25, },
# "Baz" => { "name"=>"Baz", "gender"=>"M", "age"=>30, }, }
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/kwalify/util.rb', line 118 def create_hashtable(hashlist, primarykey, flag_duplicate_check=true) hashtable = {} hashlist.each do |hash| key = hash[primarykey] unless key riase "primary key '#{key}' not found." end if flag_duplicate_check && hashtable.key?(key) raise "primary key '#{key}' duplicated (value '#{hashtable[key]}')" end hashtable[key] = hash end if hashlist return hashtable end |
.get_class(classname) ⇒ Object
get class object. if not found, NameError raised.
94 95 96 97 98 99 100 |
# File 'lib/kwalify/util.rb', line 94 def get_class(classname) klass = Object classname.split('::').each do |name| klass = klass.const_get(name) end return klass end |
.get_value(obj, path) ⇒ Object
get nested value directly.
ex.
val = YamlHelper.get_value(obj, ['aaa', 0, 'xxx'])
This is equal to the following:
begin
val = obj['aaa'][0]['xxx']
rescue NameError
val = nil
end
147 148 149 150 151 152 153 154 |
# File 'lib/kwalify/util.rb', line 147 def get_value(obj, path) val = obj path.each do |key| return nil unless val.is_a?(Hash) || val.is_a?(Array) val = val[key] end if path return val end |
.traverse_rule(validator, &block) ⇒ Object
71 72 73 74 75 |
# File 'lib/kwalify/util.rb', line 71 def traverse_rule(validator, &block) #:yield: rule rule = validator.is_a?(Rule) ? validator : validator.rule _done = {} _traverse_rule(rule, _done, &block) end |
.traverse_schema(schema, &block) ⇒ Object
44 45 46 47 48 |
# File 'lib/kwalify/util.rb', line 44 def traverse_schema(schema, &block) #:yield: rulehash hash = schema _done = {} _traverse_schema(hash, _done, &block) end |
.untabify(str, width = 8) ⇒ Object
expand tab character to spaces
ex.
untabified_str = YamlHelper.untabify(tabbed_str)
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/kwalify/util.rb', line 19 def untabify(str, width=8) return str if str.nil? list = str.split(/\t/, -1) # if 2nd arg is negative then split() doesn't remove tailing empty strings last = list.pop sb = '' list.each do |s| column = (n = s.rindex(?\n)) ? s.length - n - 1 : s.length n = width - (column % width) sb << s << (' ' * n) end sb << last if last return sb end |