Class: Modern::DSL::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/modern/dsl/scope.rb

Direct Known Subclasses

Root

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(descriptor, settings = nil) ⇒ Scope

Returns a new instance of Scope.



20
21
22
23
# File 'lib/modern/dsl/scope.rb', line 20

def initialize(descriptor, settings = nil)
  @descriptor = descriptor
  @settings = settings&.dup || ScopeSettings.new
end

Instance Attribute Details

#descriptorObject (readonly)

Returns the value of attribute descriptor.



18
19
20
# File 'lib/modern/dsl/scope.rb', line 18

def descriptor
  @descriptor
end

#settingsObject (readonly)

Returns the value of attribute settings.



17
18
19
# File 'lib/modern/dsl/scope.rb', line 17

def settings
  @settings
end

Class Method Details

.evaluate(descriptor, settings, &block) ⇒ Object



129
130
131
132
133
134
# File 'lib/modern/dsl/scope.rb', line 129

def self.evaluate(descriptor, settings, &block)
  scope = Scope.new(descriptor, settings)
  scope.instance_exec(&block)

  scope.descriptor
end

Instance Method Details

#capsule(cap) ⇒ Object



25
26
27
28
# File 'lib/modern/dsl/scope.rb', line 25

def capsule(cap)
  raise "Must be a Modern::Capsule." unless cap.is_a?(Modern::Capsule)
  @descriptor = _scope({}, &cap.block)
end

#clear_input_converters!Object



82
83
84
# File 'lib/modern/dsl/scope.rb', line 82

def clear_input_converters!
  @settings = @settings.copy(input_converters: [])
end

#clear_output_converters!Object



100
101
102
# File 'lib/modern/dsl/scope.rb', line 100

def clear_output_converters!
  @settings = @settings.copy(output_converters: [])
end

#clear_security!Object



60
61
62
# File 'lib/modern/dsl/scope.rb', line 60

def clear_security!
  @settings = @settings.copy(security: [])
end

#default_response(&block) ⇒ Object



36
37
38
39
# File 'lib/modern/dsl/scope.rb', line 36

def default_response(&block)
  resp = ResponseBuilder.evaluate(@settings.default_response, &block)
  @settings = @settings.copy(default_response: resp)
end

#delete(id, path = nil, &block) ⇒ Object



121
122
123
# File 'lib/modern/dsl/scope.rb', line 121

def delete(id, path = nil, &block)
  route(id, :delete, path, &block)
end

#deprecate!Object



41
42
43
# File 'lib/modern/dsl/scope.rb', line 41

def deprecate!
  @settings = @settings.copy(deprecated: true)
end

#get(id, path = nil, &block) ⇒ Object



109
110
111
# File 'lib/modern/dsl/scope.rb', line 109

def get(id, path = nil, &block)
  route(id, :get, path, &block)
end

#helper(h) ⇒ Object



49
50
51
# File 'lib/modern/dsl/scope.rb', line 49

def helper(h)
  @settings = @settings.copy(helpers: @settings.helpers + [h])
end

#input_converter(media_type_or_converter, &block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/modern/dsl/scope.rb', line 68

def input_converter(media_type_or_converter, &block)
  if media_type_or_converter.is_a?(Modern::Descriptor::Converters::Input::Base)
    @settings = @settings.copy(input_converters: @settings.input_converters + [media_type_or_converter])
  elsif media_type_or_converter.is_a?(String) && !block.nil?
    input_converter(
      Modern::Descriptor::Converters::Input::Base.new(
        media_type: media_type_or_converter, converter: block
      )
    )
  else
    raise "must pass a String and block or a Modern::Descriptor::Converters::Input::Base."
  end
end

#output_converter(media_type_or_converter, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/modern/dsl/scope.rb', line 86

def output_converter(media_type_or_converter, &block)
  if media_type_or_converter.is_a?(Modern::Descriptor::Converters::Output::Base)
    @settings = @settings.copy(output_converters: @settings.output_converters + [media_type_or_converter])
  elsif media_type_or_converter.is_a?(String) && !block.nil?
    output_converter(
      Modern::Descriptor::Converters::Output::Base.new(
        media_type: media_type_or_converter, converter: block
      )
    )
  else
    raise "must pass a String and block or a Modern::Descriptor::Converters::Output::Base."
  end
end

#parameter(name, parameter_type, opts) ⇒ Object



53
54
55
56
57
58
# File 'lib/modern/dsl/scope.rb', line 53

def parameter(name, parameter_type, opts)
  param = Modern::Descriptor::Parameters.from_inputs(name, parameter_type, opts)
  raise "Duplicate parameter '#{name}'.'" if @settings.parameters.any? { |p| p.name == param.name }

  @settings = @settings.copy(parameters: @settings.parameters + [param])
end

#patch(id, path = nil, &block) ⇒ Object



125
126
127
# File 'lib/modern/dsl/scope.rb', line 125

def patch(id, path = nil, &block)
  route(id, :patch, path, &block)
end

#path(p, &block) ⇒ Object



30
31
32
33
34
# File 'lib/modern/dsl/scope.rb', line 30

def path(p, &block)
  p_segs = p.split("/")
  new_path_segments = ([@settings.path_segments] + p_segs).flatten
  @descriptor = _scope(path_segments: new_path_segments, &block)
end

#post(id, path = nil, &block) ⇒ Object



113
114
115
# File 'lib/modern/dsl/scope.rb', line 113

def post(id, path = nil, &block)
  route(id, :post, path, &block)
end

#put(id, path = nil, &block) ⇒ Object



117
118
119
# File 'lib/modern/dsl/scope.rb', line 117

def put(id, path = nil, &block)
  route(id, :put, path, &block)
end

#route(id, http_method, path = nil, &block) ⇒ Object



104
105
106
107
# File 'lib/modern/dsl/scope.rb', line 104

def route(id, http_method, path = nil, &block)
  route = RouteBuilder.evaluate(id, http_method, path, @settings.dup, &block)
  @descriptor = @descriptor.copy(routes: @descriptor.routes + [route])
end

#security(sec) ⇒ Object



64
65
66
# File 'lib/modern/dsl/scope.rb', line 64

def security(sec)
  @settings = @settings.copy(security: @settings.security + [sec])
end

#tag(t) ⇒ Object



45
46
47
# File 'lib/modern/dsl/scope.rb', line 45

def tag(t)
  @settings = @settings.copy(tags: @settings.tags + [t.to_s])
end