Module: Praxis::ResourceDefinition::ClassMethods

Defined in:
lib/praxis/resource_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



20
21
22
# File 'lib/praxis/resource_definition.rb', line 20

def actions
  @actions
end

#controllerObject

Returns the value of attribute controller.



29
30
31
# File 'lib/praxis/resource_definition.rb', line 29

def controller
  @controller
end

#metadataObject (readonly)

opaque hash of user-defined medata, used to decorate the definition, and also available in the generated JSON documents



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

def 
  @metadata
end

#responsesObject (readonly)

Returns the value of attribute responses.



22
23
24
# File 'lib/praxis/resource_definition.rb', line 22

def responses
  @responses
end

#routing_configObject (readonly)

Returns the value of attribute routing_config.



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

def routing_config
  @routing_config
end

#version_optionsObject (readonly)

Returns the value of attribute version_options.



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

def version_options
  @version_options
end

Instance Method Details

#action(name, &block) ⇒ Object

Raises:

  • (ArgumentError)


116
117
118
119
120
# File 'lib/praxis/resource_definition.rb', line 116

def action(name, &block)
  raise ArgumentError, "can not create ActionDefinition without block" unless block_given?
  raise ArgumentError, "Action names must be defined using symbols (Got: #{name} (of type #{name.class}))" unless name.is_a? Symbol
  @actions[name] = ActionDefinition.new(name, self, &block)
end

#action_defaults(&block) ⇒ Object



82
83
84
85
86
# File 'lib/praxis/resource_definition.rb', line 82

def action_defaults(&block)
  return @action_defaults unless block_given?

  @action_defaults << block
end

#canonical_path(action_name = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/praxis/resource_definition.rb', line 51

def canonical_path( action_name=nil )
  if action_name
    raise "Canonical path for #{self.name} is already defined as: '#{@canonical_action_name}'. 'canonical_path' can only be defined once." if @canonical_action_name
    @canonical_action_name = action_name
  else
    # Resolution of the actual action definition needs to be done lazily, since we can use the `canonical_path` stanza
    # at the top of the resource, well before the actual action is defined.
    unless @canonical_action
      href_action = @canonical_action_name || DEFAULT_RESOURCE_HREF_ACTION
      @canonical_action = actions.fetch(href_action) do
        raise "Error: trying to set canonical_href of #{self.name}. Action '#{href_action}' does not exist"
      end
    end
    return @canonical_action
  end
end

#describeObject



131
132
133
134
135
136
137
138
139
# File 'lib/praxis/resource_definition.rb', line 131

def describe
  {}.tap do |hash|
    hash[:description] = description
    hash[:media_type] = media_type.id if media_type
    hash[:actions] = actions.values.map(&:describe)
    hash[:name] = self.name 
    hash[:metadata] = 
  end
end

#description(text = nil) ⇒ Object



122
123
124
125
# File 'lib/praxis/resource_definition.rb', line 122

def description(text=nil)
  @description = text if text
  @description
end

#headers(**opts, &block) ⇒ Object



102
103
104
105
106
107
# File 'lib/praxis/resource_definition.rb', line 102

def headers(**opts, &block)
  warn 'DEPRECATION: ResourceDefinition.headers is deprecated. Use action_defaults instead.'
  action_defaults do
    headers **opts, &block
  end
end

#idObject



127
128
129
# File 'lib/praxis/resource_definition.rb', line 127

def id
  self.name.gsub('::'.freeze,'-'.freeze)
end

#media_type(media_type = nil) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/praxis/resource_definition.rb', line 36

def media_type(media_type=nil)
  return @media_type unless media_type

  if media_type.kind_of?(String)
    media_type = SimpleMediaType.new(media_type)
  end
  @media_type = media_type
end

#nodoc!Object



148
149
150
# File 'lib/praxis/resource_definition.rb', line 148

def nodoc!
  [:doc_visibility] = :none
end

#params(type = Attributor::Struct, **opts, &block) ⇒ Object



88
89
90
91
92
93
# File 'lib/praxis/resource_definition.rb', line 88

def params(type=Attributor::Struct, **opts, &block)
  warn 'DEPRECATION: ResourceDefinition.params is deprecated. Use it in action_defaults instead.'
  action_defaults do
    params type, **opts, &block
  end
end

#parse_href(path) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/praxis/resource_definition.rb', line 72

def parse_href(path)
  param_values = canonical_path.primary_route.path.params(path)
  attrs = canonical_path.params.attributes
  param_values.each_with_object({}) do |(key,value),hash|
    hash[key.to_sym] = attrs[key.to_sym].load(value,[key])
  end
rescue => e
  raise Praxis::Exception.new("Error parsing or coercing parameters from href: #{path}\n"+e.message)
end

#payload(type = Attributor::Struct, **opts, &block) ⇒ Object



95
96
97
98
99
100
# File 'lib/praxis/resource_definition.rb', line 95

def payload(type=Attributor::Struct, **opts, &block)
  warn 'DEPRECATION: ResourceDefinition.payload is deprecated. Use action_defaults instead.'
  action_defaults do
    payload type, **opts, &block
  end
end

#response(name, **args) ⇒ Object



109
110
111
112
113
114
# File 'lib/praxis/resource_definition.rb', line 109

def response(name, **args)
  warn 'DEPRECATION: ResourceDefinition.response is deprecated. Use action_defaults instead.'
  action_defaults do
    response name, **args
  end
end

#routing(&block) ⇒ Object

FIXME: this is inconsistent with the rest of the magic DSL convention.



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

def routing(&block)
  @routing_config = block
end

#to_href(params) ⇒ Object



68
69
70
# File 'lib/praxis/resource_definition.rb', line 68

def to_href( params )
  canonical_path.primary_route.path.expand(params)
end

#use(trait_name) ⇒ Object



141
142
143
144
145
146
# File 'lib/praxis/resource_definition.rb', line 141

def use(trait_name)
  unless ApiDefinition.instance.traits.has_key? trait_name
    raise Exceptions::InvalidTrait.new("Trait #{trait_name} not found")
  end
  self.instance_eval(&ApiDefinition.instance.traits[trait_name])
end

#version(version = nil, options = nil) ⇒ Object



45
46
47
48
49
# File 'lib/praxis/resource_definition.rb', line 45

def version(version=nil, options=nil)
  return @version unless version
  @version = version
  @version_options = options || {using: [:header,:params]}
end