Class: Jimmy::Json::Hash

Inherits:
Object
  • Object
show all
Includes:
Collection
Defined in:
lib/jimmy/json/hash.rb

Overview

Represents a JSON object that is part of a JSON schema.

Direct Known Subclasses

Schema

Instance Method Summary collapse

Methods included from Collection

#[], #as_json, #clear, #deep_dup, #dig, #dup, #empty?, #freeze, #inspect, #to_json

Constructor Details

#initialize(hash = {}) ⇒ Hash

Returns a new instance of Hash.

Parameters:

  • hash (Hash, ::Hash) (defaults to: {})

    Items to be merged into the new hash.



13
14
15
16
17
# File 'lib/jimmy/json/hash.rb', line 13

def initialize(hash = {})
  super()
  @members = {}
  merge! hash
end

Instance Method Details

#[]=(key, value) ⇒ Object

Set a value in the hash.

Parameters:

  • key (String, Symbol)

    The key to set

  • value (Object)

    The value to set



22
23
24
25
26
# File 'lib/jimmy/json/hash.rb', line 22

def []=(key, value)
  key, value = cast(key, value)
  @members[key] = value
  sort!
end

#each {|key, member| ... } ⇒ Enumerable, self

Iterate over items in the hash. If a block with a single argument is given, only values will be yielded. Otherwise, keys and values will be yielded.

Yield Parameters:

  • key (String)

    The key of each item.

  • member (Object)

    Each item.

Returns:

  • (Enumerable, self)

    If no block is given, an Enumerable is returned. Otherwise, self is returned.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jimmy/json/hash.rb', line 51

def each(&block)
  return enum_for :each unless block

  @members.each do |key, value|
    if block.arity == 1
      yield value
    else
      yield key, value
    end
  end
end

#fetch(key, *args, &block) ⇒ Object

Fetch a value from the hash.

Parameters:

  • key (String, Symbol)

    Key of the item to fetch

Returns:

  • (Object)

See Also:

  • Hash#fetch


32
33
34
# File 'lib/jimmy/json/hash.rb', line 32

def fetch(key, *args, &block)
  @members.fetch cast_key(key), *args, &block
end

#get_fragment(json_pointer) ⇒ Jimmy::Collection?

Get the JSON fragment for the given pointer. Returns nil if the pointer is unmatched.

Parameters:

Returns:

  • (Jimmy::Collection, nil)


79
80
81
82
83
84
# File 'lib/jimmy/json/hash.rb', line 79

def get_fragment(json_pointer)
  json_pointer = Pointer.new(json_pointer)
  return self if json_pointer.empty?

  dig *json_pointer.to_a
end

#key?(key) ⇒ Boolean

Returns true if the given key is assigned.

Parameters:

  • key (String, Symbol)

    The key to check.

Returns:

  • (Boolean)


65
66
67
# File 'lib/jimmy/json/hash.rb', line 65

def key?(key)
  @members.key? cast_key(key)
end

#keysArray<String>

Get an array of all keys in the hash.

Returns:



71
72
73
# File 'lib/jimmy/json/hash.rb', line 71

def keys
  @members.keys
end

#merge!(hash) ⇒ self

Merge values of another hash into this hash.

Parameters:

  • hash (Hash, ::Hash)

Returns:

  • (self)


39
40
41
42
# File 'lib/jimmy/json/hash.rb', line 39

def merge!(hash)
  hash.each { |k, v| self[k] = v }
  self
end