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
# 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

  instance_eval(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



21
22
23
# File 'lib/praxis/trait.rb', line 21

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.



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

def name
  @name
end

Instance Method Details

#apply!(target) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/praxis/trait.rb', line 91

def apply!(target)
  @attribute_groups.each do |name, blocks|
    blocks.each do |block|
      target.send(name, &block)
    end
  end

  target.routing(&@routing) if @routing

  @responses.each do |name, args|
    target.response(name, **args)
  end
  return unless @other.any?

  @other.each do |name, args, block|
    if block
      target.send(name, *args, &block)
    else
      target.send(name, *args)
    end
  end
end

#create_group(name, &block) ⇒ Object



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

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

#describeObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/praxis/trait.rb', line 63

def describe
  desc = { description: @description }
  desc[:name] = @name if @name
  desc[:responses] = @responses if @responses.any?

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

  @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 {}, **hash_opts)
                 else
                   Attributor::Hash.construct(proc {})
                 end
    blocks.each do |block|
      type_class.construct(block)
    end
    hash[name] = type_class.describe[:attributes]
  end

  desc
end

#description(desc = nil) ⇒ Object



29
30
31
32
33
# File 'lib/praxis/trait.rb', line 29

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

  @description = desc
end

#headers(*_args, &block) ⇒ Object



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

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

#params(*_args, &block) ⇒ Object



47
48
49
# File 'lib/praxis/trait.rb', line 47

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

#payload(*args, &block) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/praxis/trait.rb', line 51

def payload(*args, &block)
  type, _opts = args

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

  create_group(:payload, &block)
end

#respond_to_missing?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/praxis/trait.rb', line 25

def respond_to_missing?(*)
  true
end

#response(resp, **args) ⇒ Object



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

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

#routing(&block) ⇒ Object



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

def routing(&block)
  @routing = block
end