class Praxis::Trait

Attributes

attribute_groups[R]
name[R]

Public Class Methods

new(&block) click to toggle source
# File lib/praxis/trait.rb, line 7
def initialize(&block)
  @name = nil
  @description = nil
  @responses = {}
  @routing = nil
  @other = []

  @attribute_groups = Hash.new do |h,k|
    h[k] = []
  end

  if block_given?
    self.instance_eval(&block)
  end
end

Public Instance Methods

apply!(target) click to toggle source
# File lib/praxis/trait.rb, line 93
def apply!(target)
  @attribute_groups.each do |name, blocks|
    blocks.each do |block|
      target.send(name, &block)
    end
  end

  if @routing
    target.routing(&@routing)
  end

  @responses.each do |name, args|
    target.response(name, **args)
  end

  if @other.any?
    @other.each do |name, args, block|
      if block
        target.send(name, *args, &block)
      else
        target.send(name,*args)
      end
    end
  end
end
create_group(name, &block) click to toggle source
# File lib/praxis/trait.rb, line 36
def create_group(name, &block)
  @attribute_groups[name] << block
end
describe() click to toggle source
# File lib/praxis/trait.rb, line 62
def describe
  desc = {description: @description}
  desc[:name] = @name if @name
  desc[:responses] = @responses if @responses.any?

  if @routing
    desc[:routing] = ConfigHash.new(&@routing).to_hash
  end

  @attribute_groups.each_with_object(desc) do |(name, blocks), hash|
    type_class = if name == :headers
      # Headers are special:
      # Keys are strings, they have a special DSL, and are case insensitive
      hash_opts = {
        dsl_compiler: ActionDefinition::HeadersDSLCompiler,
        case_insensitive_load: true
      }
      Attributor::Hash.of(key: String).construct(Proc.new {}, hash_opts)
    else
      Attributor::Hash.construct(Proc.new {})
    end
    blocks.each do |block|
      type_class.construct(block)
    end
    hash[name] = type_class.describe[:attributes]
  end

  desc
end
description(desc=nil) click to toggle source
# File lib/praxis/trait.rb, line 27
def description(desc=nil)
  return @description if desc.nil?
  @description = desc
end
headers(*args, &block) click to toggle source
# File lib/praxis/trait.rb, line 40
def headers(*args, &block)
  create_group(:headers,&block)
end
method_missing(name, *args, &block) click to toggle source
# File lib/praxis/trait.rb, line 23
def method_missing(name, *args, &block)
  @other << [name, args, block]
end
params(*args, &block) click to toggle source
# File lib/praxis/trait.rb, line 44
def params(*args, &block)
  create_group(:params,&block)
end
payload(*args, &block) click to toggle source
# File lib/praxis/trait.rb, line 48
def payload(*args, &block)
  type, opts = args

  if type && !(type < Attributor::Hash)
    raise 'payload in a trait with non-hash (or model or struct) is not supported'
  end

  create_group(:payload,&block)
end
response(resp, **args) click to toggle source
# File lib/praxis/trait.rb, line 32
def response(resp, **args)
  @responses[resp] = args
end
routing(&block) click to toggle source
# File lib/praxis/trait.rb, line 58
def routing(&block)
  @routing = block
end