Class: Evil::Client::Schema Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/evil/client/schema.rb

Overview

This class is abstract.

Base class for mutable containers of client-specific definitions of nested scopes and operations along with a corresponding [#settings] class subclassing [Evil::Client::Settings]

Every concrete container defines its only DSL for scope/operation definitions.

Direct Known Subclasses

Operation

Defined Under Namespace

Classes: Operation, Scope

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientEvil::Client (readonly)

Back reference to client the schema belongs to

Returns:



38
39
40
# File 'lib/evil/client/schema.rb', line 38

def client
  @client
end

#nameString (readonly)

The name of current schema which is unique for the existing [#parent], or equals to client class name without any [#parent] (root scope name).

Returns:

  • (String)


23
24
25
# File 'lib/evil/client/schema.rb', line 23

def name
  @name
end

#parentEvil::Client::Schema::Scope? (readonly)

Scope schema the operation belongs to

Only the root schema has no parents. Its definitions are shared by all operations

Returns:



32
33
34
# File 'lib/evil/client/schema.rb', line 32

def parent
  @parent
end

Instance Method Details

#let(key, &block) ⇒ self

Adds a memoized method to the [#settings] class

Parameters:

  • key (#to_sym)

    The name of the attribute

  • block (Proc)

    The body of new attribute

Returns:

  • (self)


77
78
79
80
# File 'lib/evil/client/schema.rb', line 77

def let(key, &block)
  settings.let(key, &block)
  self
end

#option(key, type = nil, **opts) ⇒ self

Adds an option to the [#settings] class

Parameters:

  • key (#to_sym)

    Symbolic name of the option

  • type (#call) (defaults to: nil)

    (nil) Type coercer for the option

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :type (#call)

    Another way to assign type coercer

  • :default (#call)

    Proc containing default value

  • :optional (Boolean)

    Whether it can be missed

  • :as (#to_sym)

    The name of settings variable

  • :reader (false, :private, :protected)

    Reader method type

Returns:

  • (self)


67
68
69
70
# File 'lib/evil/client/schema.rb', line 67

def option(key, type = nil, **opts)
  settings.option(key, type, **opts)
  self
end

#settingsClass

The settings class inherited from the [#parent]‘s one

Returns:

  • (Class)


57
58
59
# File 'lib/evil/client/schema.rb', line 57

def settings
  @settings ||= (parent&.settings || Settings).for(self)
end

#to_sString Also known as: to_str, inspect

The human-friendly representation of the schema

Examples:

"MyClient.users.fetch" # custom operation's schema

Returns:

  • (String)


47
48
49
# File 'lib/evil/client/schema.rb', line 47

def to_s
  [parent, name].compact.join(".")
end

#validate(&block) ⇒ self

Adds validator to the [#settings] class

Parameters:

  • block (Proc)

    The body of new attribute

Returns:

  • (self)


87
88
89
90
# File 'lib/evil/client/schema.rb', line 87

def validate(&block)
  settings.validate(&block)
  self
end