Module: Textus::Contract::DSL

Overview

Mixed onto a use-case class via ‘extend`. Calls accumulate into ivars, frozen into a Spec on first read of `.contract`.

Instance Method Summary collapse

Instance Method Details

#arg(name, type, required: false, positional: false, session_default: nil, description: nil) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/textus/contract.rb', line 73

def arg(name, type, required: false, positional: false, session_default: nil, description: nil)
  raise "contract already built; declare args before reading .contract" if defined?(@__contract) && @__contract

  (@__args ||= []) << Arg.new(
    name: name, type: type, required: required,
    positional: positional, session_default: session_default, description: description
  )
end

#contractObject

rubocop:disable Naming/MemoizedInstanceVariableName (@__verb, @__args, etc.) and avoid name collision with user-defined ‘@contract`.



94
95
96
97
98
99
100
101
102
# File 'lib/textus/contract.rb', line 94

def contract
  @__contract ||= Spec.new(
    verb: @__verb,
    summary: @__summary,
    args: (@__args || []).freeze,
    surfaces: (@__surfaces || []).freeze,
    response: response,
  )
end

#contract?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/textus/contract.rb', line 87

def contract?
  !@__verb.nil?
end

#response(&blk) ⇒ Object



82
83
84
85
# File 'lib/textus/contract.rb', line 82

def response(&blk)
  @__response = blk if blk
  @__response || ->(v) { v }
end

#summary(text = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/textus/contract.rb', line 53

def summary(text = nil)
  if text
    raise "contract already built; declare summary before reading .contract" if defined?(@__contract) && @__contract

    @__summary = text
  else
    @__summary
  end
end

#surfaces(*list) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/textus/contract.rb', line 63

def surfaces(*list)
  if list.empty?
    @__surfaces ||= []
  else
    raise "contract already built; declare surfaces before reading .contract" if defined?(@__contract) && @__contract

    @__surfaces = list
  end
end

#verb(name = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/textus/contract.rb', line 43

def verb(name = nil)
  if name
    raise "contract already built; declare verb before reading .contract" if defined?(@__contract) && @__contract

    @__verb = name
  else
    @__verb
  end
end