Class: Cline::SecretString

Inherits:
Schema
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cline/secret_string.rb

Overview

::SecretString wrapper that allows us to use it in our Shale schemas

Internal collapse

Attributes inherited from Schema

#extra_attributes

Public API collapse

Internal collapse

Methods inherited from Schema

cline_snake_attributes, #to_cline_json

Constructor Details

#initialize(unprotected_string) ⇒ SecretString

Constructor

Parameters:

  • unprotected_string (String)

    The unprotected string



26
27
28
29
# File 'lib/cline/secret_string.rb', line 26

def initialize(unprotected_string)
  super()
  @secret_string = ::SecretString.new(unprotected_string)
end

Instance Attribute Details

#secret_stringHash (readonly)

Returns The internal secret string.

Returns:

  • (Hash)

    The internal secret string



32
33
34
# File 'lib/cline/secret_string.rb', line 32

def secret_string
  @secret_string
end

Class Method Details

.as_hash(instance, *_args, **_kwargs) ⇒ Hash

Get a Hash object from an instance.

Parameters:

  • instance (Schema)

    Object to serialize to a Hash

  • _args (Array)

    Remaining arguments to be transferred to Shale

  • _kwargs (Hash)

    Remaining kwargs to be transferred to Shale

Returns:

  • (Hash)

    Corresponding hash



60
61
62
# File 'lib/cline/secret_string.rb', line 60

def as_hash(instance, *_args, **_kwargs)
  instance.secret_string.to_unprotected
end

.cast(value) ⇒ Schema?

Cast an input value to this Schema object. Allow the attribute to be initialized directly using its Hash form.

Parameters:

  • value (Schema, String, nil)

    The value that could be used to initialize a new instance of this attribute.

Returns:

  • (Schema, nil)

    The corresponding instance, or nil if none.



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cline/secret_string.rb', line 69

def cast(value)
  return nil if value.nil?

  # We expect the value to be a String, or a new instance already initialized.
  if value.is_a?(self)
    value
  elsif value.is_a?(String)
    new(value)
  else
    raise "Unable to cast #{value} into #{name}"
  end
end

.of_hash(hash, *_args, **_kwargs) ⇒ Schema

Parse a Hash object and instantiate the proper instance from it.

Parameters:

  • hash (Hash)

    Data

  • _args (Array)

    Remaining arguments to be transferred to Shale

  • _kwargs (Hash)

    Remaining kwargs to be transferred to Shale

Returns:

  • (Schema)

    Corresponding instance



50
51
52
# File 'lib/cline/secret_string.rb', line 50

def of_hash(hash, *_args, **_kwargs)
  SecretString.new(hash)
end

Instance Method Details

#==(other) ⇒ Boolean

Equality check

Parameters:

  • other (Object)

    The other to check equality with

Returns:

  • (Boolean)

    True if objects are equal



16
17
18
19
# File 'lib/cline/secret_string.rb', line 16

def ==(other)
  other.is_a?(SecretString) &&
    other.to_unprotected == to_unprotected
end

#to_hashHash

Output this object as a Hash.

Returns:

  • (Hash)

    Cline JSON



37
38
39
# File 'lib/cline/secret_string.rb', line 37

def to_hash
  SecretString.as_hash(self)
end