Module: Scorpio::ResourceBase::Container::Hash

Includes:
Enumerable, JSI::Util::Hashlike
Defined in:
lib/scorpio/resource_base.rb

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Object



554
555
556
557
558
559
560
561
562
563
564
# File 'lib/scorpio/resource_base.rb', line 554

def as_json(options = {})
  hash = {}
  each_key do |k|
    ks = k.is_a?(String) ? k :
      k.is_a?(Symbol) ? k.to_s :
      k.respond_to?(:to_str) && (kstr = k.to_str).is_a?(String) ? kstr :
      raise(TypeError, "JSON object (Hash) cannot be keyed with: #{k.pretty_inspect.chomp}")
    hash[ks] = JSI::Util.as_json(self[k], **options)
  end
  hash
end

#each(**kw, &block) ⇒ Object



538
539
540
541
542
543
544
545
546
# File 'lib/scorpio/resource_base.rb', line 538

def each(**kw, &block)
  return(to_enum(__method__, **kw) { contained_object.size }) unless block
  if block.arity > 1
    contained_object.each_key { |k| yield(k, self[k, **kw]) }
  else
    contained_object.each_key { |k| yield([k, self[k, **kw]]) }
  end
  self
end

#to_hash(**kw) ⇒ Object



548
549
550
551
552
# File 'lib/scorpio/resource_base.rb', line 548

def to_hash(**kw)
  hash = {}
  contained_object.each_key { |k| hash[k] = self[k, **kw] }
  hash
end