class Restspec::Endpoints::EndpointDSL::ExampleOrValue

A special class to get a type example or a simple value from a block.

If the block returns a type, the result should be one example of that type.

Attributes

attribute_name[RW]
callable[RW]
endpoint[RW]

Public Class Methods

new(endpoint, attribute_name, callable) click to toggle source
# File lib/restspec/endpoints/dsl.rb, line 333
def initialize(endpoint, attribute_name, callable)
  self.attribute_name = attribute_name
  self.endpoint = endpoint
  self.callable = callable
end

Public Instance Methods

value() click to toggle source
# File lib/restspec/endpoints/dsl.rb, line 339
def value
  if example?
    raw_value.example_for(get_attribute)
  else
    raw_value
  end
end

Private Instance Methods

context() click to toggle source
# File lib/restspec/endpoints/dsl.rb, line 367
def context
  @context ||= Object.new.tap do |context|
    context.extend(Restspec::Schema::Types::TypeMethods)
  end
end
example?() click to toggle source
# File lib/restspec/endpoints/dsl.rb, line 351
def example?
  raw_value.is_a?(Restspec::Schema::Types::BasicType)
end
get_attribute() click to toggle source
# File lib/restspec/endpoints/dsl.rb, line 355
def get_attribute
  if (schema = endpoint.schema_for(:payload)).present? && schema.attributes[attribute_name]
    schema.attributes[attribute_name]
  else
    Restspec::Schema::Attribute.new(attribute_name, context.integer)
  end
end
raw_value() click to toggle source
# File lib/restspec/endpoints/dsl.rb, line 363
def raw_value
  @raw_value ||= context.instance_eval(&callable)
end