Class: Treaty::Versions::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/treaty/versions/factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version:, default:) ⇒ Factory



14
15
16
17
18
19
20
21
22
# File 'lib/treaty/versions/factory.rb', line 14

def initialize(version:, default:)
  @version = Semantic.new(version)
  @default_result = default.is_a?(Proc) ? default.call : default
  @summary_text = nil
  @deprecated_result = false
  @executor = nil

  validate!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, &_block) ⇒ Object (private)



102
103
104
105
# File 'lib/treaty/versions/factory.rb', line 102

def method_missing(name, *, &_block)
  raise Treaty::Exceptions::MethodName,
        I18n.t("treaty.versioning.factory.unknown_method", method: name)
end

Instance Attribute Details

#default_resultObject (readonly)

Returns the value of attribute default_result.



6
7
8
# File 'lib/treaty/versions/factory.rb', line 6

def default_result
  @default_result
end

#deprecated_resultObject (readonly)

Returns the value of attribute deprecated_result.



6
7
8
# File 'lib/treaty/versions/factory.rb', line 6

def deprecated_result
  @deprecated_result
end

#executorObject (readonly)

Returns the value of attribute executor.



6
7
8
# File 'lib/treaty/versions/factory.rb', line 6

def executor
  @executor
end

#request_factoryObject (readonly)

Returns the value of attribute request_factory.



6
7
8
# File 'lib/treaty/versions/factory.rb', line 6

def request_factory
  @request_factory
end

#response_factoryObject (readonly)

Returns the value of attribute response_factory.



6
7
8
# File 'lib/treaty/versions/factory.rb', line 6

def response_factory
  @response_factory
end

#summary_textObject (readonly)

Returns the value of attribute summary_text.



6
7
8
# File 'lib/treaty/versions/factory.rb', line 6

def summary_text
  @summary_text
end

#versionObject (readonly)

Returns the value of attribute version.



6
7
8
# File 'lib/treaty/versions/factory.rb', line 6

def version
  @version
end

Instance Method Details

#delegate_to(executor, method = :call) ⇒ Object



69
70
71
# File 'lib/treaty/versions/factory.rb', line 69

def delegate_to(executor, method = :call)
  @executor = Executor.new(executor, method)
end

#deprecated(condition = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/treaty/versions/factory.rb', line 36

def deprecated(condition = nil)
  result =
    if condition.is_a?(Proc)
      condition.call
    elsif condition.is_a?(TrueClass) || condition.is_a?(FalseClass)
      condition
    else
      yield
    end

  @deprecated_result = result
end

#request(entity_class = nil, &block) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/treaty/versions/factory.rb', line 49

def request(entity_class = nil, &block)
  @request_factory ||= Request::Factory.new

  if entity_class.present?
    @request_factory.use_entity(entity_class)
  elsif block_given?
    @request_factory.instance_eval(&block)
  end
end

#response(status, entity_class = nil, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/treaty/versions/factory.rb', line 59

def response(status, entity_class = nil, &block)
  @response_factory ||= Response::Factory.new(status)

  if entity_class.present?
    @response_factory.use_entity(entity_class)
  elsif block_given?
    @response_factory.instance_eval(&block)
  end
end

#summary(text) ⇒ Object



32
33
34
# File 'lib/treaty/versions/factory.rb', line 32

def summary(text)
  @summary_text = text
end

#validate!Object



24
25
26
# File 'lib/treaty/versions/factory.rb', line 24

def validate!
  validate_default_option!
end

#validate_after_block!Object



28
29
30
# File 'lib/treaty/versions/factory.rb', line 28

def validate_after_block!
  validate_default_deprecated_conflict!
end