class Vault::Response
Public Class Methods
decode(object)
click to toggle source
Decodes the given object (usually a Hash) into an instance of this class.
@param object [Hash<Symbol, Object>]
# File lib/vault/response.rb, line 37 def self.decode(object) self.new(object) end
field(n, opts = {})
click to toggle source
Defines a new field. This is designed to be used by the subclass as a mini-DSL.
@example Default
field :data
@example With a mutator
field :present, as: :present?
@param n [Symbol] the name of the field @option opts [Symbol] :as alias for method name
@!visibility private
# File lib/vault/response.rb, line 16 def self.field(n, opts = {}) self.fields[n] = opts if opts[:as].nil? attr_reader n else define_method(opts[:as]) do instance_variable_get(:"@#{n}") end end end
fields()
click to toggle source
Returns the list of fields defined on this subclass. @!visibility private
# File lib/vault/response.rb, line 30 def self.fields @fields ||= {} end
new(opts = {})
click to toggle source
# File lib/vault/response.rb, line 41 def initialize(opts = {}) # Initialize all fields as nil to start self.class.fields.each do |k, _| instance_variable_set(:"@#{k}", nil) end # For each supplied option, set the instance variable if it was defined # as a field. opts.each do |k, v| if self.class.fields.key?(k) opts = self.class.fields[k] if (m = opts[:load]) && !v.nil? v = m.call(v) end if opts[:freeze] v = v.freeze end instance_variable_set(:"@#{k}", v) end end end
Public Instance Methods
==(other)
click to toggle source
# File lib/vault/response.rb, line 85 def ==(other) self.to_h == other.to_h end
to_h()
click to toggle source
Create a hash-bashed representation of this response.
@return [Hash]
# File lib/vault/response.rb, line 69 def to_h self.class.fields.inject({}) do |h, (k, opts)| if opts[:as].nil? h[k] = self.public_send(k) else h[k] = self.public_send(opts[:as]) end if !h[k].nil? && !h[k].is_a?(Array) && h[k].respond_to?(:to_h) h[k] = h[k].to_h end h end end