class ServiceOperation::Params::DSL

Build {Params::Attribute} DSL

Public Class Methods

new() click to toggle source
# File lib/service_operation/params/dsl.rb, line 17
def initialize
  @attributes = []
end
run(&block) click to toggle source

@yield to the block containing the DSL @return [Array<Attribute>]

# File lib/service_operation/params/dsl.rb, line 11
def self.run(&block)
  dsl = new
  dsl.instance_eval(&block)
  dsl.instance_variable_get('@attributes').freeze
end

Public Instance Methods

Any(*subvalidators) click to toggle source

rubocop:disable Naming/MethodName

# File lib/service_operation/params/dsl.rb, line 23
def Any(*subvalidators)
  Any.new(subvalidators)
end
Anything() click to toggle source
# File lib/service_operation/params/dsl.rb, line 27
def Anything
  Anything
end
ArrayOf(element_validator) click to toggle source
# File lib/service_operation/params/dsl.rb, line 31
def ArrayOf(element_validator)
  ArrayOf.new(element_validator)
end
Bool() click to toggle source
# File lib/service_operation/params/dsl.rb, line 35
def Bool
  Bool
end
EnumerableOf(element_validator) click to toggle source
# File lib/service_operation/params/dsl.rb, line 39
def EnumerableOf(element_validator)
  EnumerableOf.new(element_validator)
end
_query_params(default_sort: 'id') click to toggle source

@todo: move

# File lib/service_operation/params/dsl.rb, line 46
def _query_params(default_sort: 'id')
  id :integer, optional: true
  ids [:integer], optional: true

  filter :hash, optional: true,
                coerce: ->(f) { f.is_a?(Hash) ? f : Array(f).map { [f, nil] }.to_h }

  includes [:string], optional: true
  page :hash, optional: true, coerce: :json_api_page
  sort :string, optional: true, default: default_sort
end

Private Instance Methods

def_attr(*args) click to toggle source
# File lib/service_operation/params/dsl.rb, line 60
def def_attr(*args)
  @attributes << Attribute.define(*args)
end
method_missing(name, *args) click to toggle source
Calls superclass method
# File lib/service_operation/params/dsl.rb, line 64
def method_missing(name, *args)
  if respond_to_missing?(name)
    def_attr(name, *args)
  else
    super
  end
end
respond_to_missing?(method_name, _include_private = nil) click to toggle source

any lowercase method name becomes an attribute

# File lib/service_operation/params/dsl.rb, line 73
def respond_to_missing?(method_name, _include_private = nil)
  first_letter = method_name.to_s.each_char.first
  first_letter.eql?(first_letter.downcase)
end