Module | Kwalify::Util
In: | kwalify/util/hashlike.rb kwalify/util/ordered-hash.rb kwalify/util.rb kwalify.rb

### Methods

create_hashtable get_class get_value traverse_rule traverse_schema untabify

### Classes and Modules

Module Kwalify::Util::HashLike
Class Kwalify::Util::OrderedHash

### Public Instance methods

create_hashtable(hashlist, primarykey, flag_duplicate_check=true)

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, }, }

[Source]

    # File 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)

get class object. if not found, NameError raised.

[Source]

    # File 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)

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

[Source]

    # File 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) {|rule| ...}

traverse rule

ex.

      schema = YAML.load_file('myschema.yaml')
      validator = Kwalify::Validator.new(schema)
      Kwalify::Util.traverse_rule(validator) do |rule|
        p rule if rule.classname
      end

[Source]

    # File 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) {|rulehash| ...}

traverse schema

ex.

      schema = YAML.load_file('myschema.yaml')
      Kwalify::Util.traverse_schema(schema) do |rulehash|
        ## add module prefix to class name
        if rulehash['class']
          rulehash['class'] = 'MyModule::' + rulehash['class']
        end
      end

[Source]

    # File 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)

expand tab character to spaces

ex.

      untabified_str = YamlHelper.untabify(tabbed_str)

[Source]

    # File 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

[Validate]