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

Methods

Classes and Modules

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

Public Instance methods

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 117
    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 object. if not found, NameError raised.

[Source]

# File kwalify/util.rb, line 93
    def get_class(classname)
      klass = Object
      classname.split('::').each do |name|
        klass = klass.const_get(name)
      end
      return klass
    end

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 146
    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

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 70
    def traverse_rule(validator, &block)  #:yield: rule
      rule = validator.is_a?(Rule) ? validator : validator.rule
      _done = {}
      _traverse_rule(rule, _done, &block)
    end

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 43
    def traverse_schema(schema, &block)  #:yield: rulehash
      hash = schema
      _done = {}
      _traverse_schema(hash, _done, &block)
    end

expand tab character to spaces

ex.

  untabified_str = YamlHelper.untabify(tabbed_str)

[Source]

# File kwalify/util.rb, line 19
    def untabify(str, width=8)
      list = str.split(/\t/)
      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]