Class: Praxis::Trait

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis/trait.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Trait

Returns a new instance of Trait.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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] = Trait.new
  end

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



23
24
25
# File 'lib/praxis/trait.rb', line 23

def method_missing(name, *args, &block)
  @other << [name, args, block]
end

Instance Attribute Details

#attribute_groupsObject (readonly)

Returns the value of attribute attribute_groups.



5
6
7
# File 'lib/praxis/trait.rb', line 5

def attribute_groups
  @attribute_groups
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/praxis/trait.rb', line 4

def name
  @name
end

Instance Method Details

#apply!(target) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/praxis/trait.rb', line 79

def apply!(target)
  @attribute_groups.each do |name, block|
    target.send(name, &block)
  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) ⇒ Object



36
37
38
# File 'lib/praxis/trait.rb', line 36

def create_group(name, &block)
  @attribute_groups[name] = block
end

#describeObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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, block), hash|
    hash[name] = Attributor::Hash.construct(block).describe[:keys]
  end

  desc
end

#description(desc = nil) ⇒ Object



27
28
29
30
# File 'lib/praxis/trait.rb', line 27

def description(desc=nil)
  return @description if desc.nil?
  @description = desc
end

#headers(*args, &block) ⇒ Object



40
41
42
# File 'lib/praxis/trait.rb', line 40

def headers(*args, &block)
  create_group(:headers,&block)
end

#params(*args, &block) ⇒ Object



44
45
46
# File 'lib/praxis/trait.rb', line 44

def params(*args, &block)
  create_group(:params,&block)
end

#payload(*args, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



32
33
34
# File 'lib/praxis/trait.rb', line 32

def response(resp, **args)
  @responses[resp] = args
end

#routing(&block) ⇒ Object



58
59
60
# File 'lib/praxis/trait.rb', line 58

def routing(&block)
  @routing = block
end