Class: Jimmy::Json::Hash
- Inherits:
-
Object
- Object
- Jimmy::Json::Hash
- Includes:
- Collection
- Defined in:
- lib/jimmy/json/hash.rb
Overview
Represents a JSON object that is part of a JSON schema.
Direct Known Subclasses
Instance Method Summary collapse
-
#[]=(key, value) ⇒ Object
Set a value in the hash.
-
#each {|key, member| ... } ⇒ Enumerable, self
Iterate over items in the hash.
-
#fetch(key, *args, &block) ⇒ Object
Fetch a value from the hash.
-
#get_fragment(json_pointer) ⇒ Jimmy::Collection?
Get the JSON fragment for the given pointer.
-
#initialize(hash = {}) ⇒ Hash
constructor
A new instance of Hash.
-
#key?(key) ⇒ Boolean
Returns true if the given key is assigned.
-
#keys ⇒ Array<String>
Get an array of all keys in the hash.
-
#merge!(hash) ⇒ self
Merge values of another hash into this hash.
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.
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.
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.
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.
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.
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.
65 66 67 |
# File 'lib/jimmy/json/hash.rb', line 65 def key?(key) @members.key? cast_key(key) end |
#keys ⇒ Array<String>
Get an array of all keys in the hash.
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.
39 40 41 42 |
# File 'lib/jimmy/json/hash.rb', line 39 def merge!(hash) hash.each { |k, v| self[k] = v } self end |