Class: Praxis::ActionDefinition
- Inherits:
-
Object
- Object
- Praxis::ActionDefinition
- Defined in:
- lib/praxis.rb,
lib/praxis/action_definition.rb,
lib/praxis/action_definition/headers_dsl_compiler.rb
Defined Under Namespace
Classes: HeadersDSLCompiler
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#named_routes ⇒ Object
readonly
Returns the value of attribute named_routes.
-
#primary_route ⇒ Object
readonly
Returns the value of attribute primary_route.
-
#resource_definition ⇒ Object
readonly
Returns the value of attribute resource_definition.
-
#responses ⇒ Object
readonly
Returns the value of attribute responses.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
- #create_attribute(type = Attributor::Struct, **opts, &block) ⇒ Object
- #describe ⇒ Object
- #description(text = nil) ⇒ Object
- #headers(type = Attributor::Hash.of(key:String), **opts, &block) ⇒ Object
-
#initialize(name, resource_definition, **opts, &block) ⇒ ActionDefinition
constructor
A new instance of ActionDefinition.
- #params(type = Attributor::Struct, **opts, &block) ⇒ Object
- #payload(type = Attributor::Struct, **opts, &block) ⇒ Object
-
#precomputed_header_keys_for_rack ⇒ Object
Good optimization to avoid creating lots of strings and comparisons on a per-request basis.
- #response(name, **args) ⇒ Object
- #routing(&block) ⇒ Object
- #update_attribute(attribute, options, block) ⇒ Object
- #use(trait_name) ⇒ Object
Constructor Details
#initialize(name, resource_definition, **opts, &block) ⇒ ActionDefinition
Returns a new instance of ActionDefinition.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/praxis/action_definition.rb', line 19 def initialize(name, resource_definition, **opts, &block) @name = name @resource_definition = resource_definition @responses = Hash.new if (media_type = resource_definition.media_type) if media_type.kind_of?(Class) && media_type < Praxis::MediaType @reference_media_type = media_type end end if resource_definition.params saved_type, saved_opts, saved_block = resource_definition.params params(saved_type, saved_opts, &saved_block) end if resource_definition.payload saved_type, saved_opts, saved_block = resource_definition.payload payload(saved_type, saved_opts, &saved_block) end if resource_definition.headers saved_opts, saved_block = resource_definition.headers headers(saved_opts, &saved_block) end resource_definition.responses.each do |name, args| response(name, args) end self.instance_eval(&block) if block_given? end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/praxis/action_definition.rb', line 12 def name @name end |
#named_routes ⇒ Object (readonly)
Returns the value of attribute named_routes.
16 17 18 |
# File 'lib/praxis/action_definition.rb', line 16 def named_routes @named_routes end |
#primary_route ⇒ Object (readonly)
Returns the value of attribute primary_route.
15 16 17 |
# File 'lib/praxis/action_definition.rb', line 15 def primary_route @primary_route end |
#resource_definition ⇒ Object (readonly)
Returns the value of attribute resource_definition.
13 14 15 |
# File 'lib/praxis/action_definition.rb', line 13 def resource_definition @resource_definition end |
#responses ⇒ Object (readonly)
Returns the value of attribute responses.
17 18 19 |
# File 'lib/praxis/action_definition.rb', line 17 def responses @responses end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
14 15 16 |
# File 'lib/praxis/action_definition.rb', line 14 def routes @routes end |
Instance Method Details
#create_attribute(type = Attributor::Struct, **opts, &block) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/praxis/action_definition.rb', line 62 def create_attribute(type=Attributor::Struct, **opts, &block) unless opts[:reference] opts[:reference] = @reference_media_type if @reference_media_type && block end return Attributor::Attribute.new(type, opts, &block) end |
#describe ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/praxis/action_definition.rb', line 151 def describe {}.tap do |hash| hash[:description] = description hash[:name] = name # FIXME: change to :routes along with api browser hash[:urls] = routes.collect(&:describe) hash[:headers] = headers.describe if headers hash[:params] = params.describe if params hash[:payload] = payload.describe if payload hash[:responses] = responses.inject({}) do |memo, (response_name, response)| memo[response.name] = response.describe memo end end end |
#description(text = nil) ⇒ Object
144 145 146 147 |
# File 'lib/praxis/action_definition.rb', line 144 def description(text = nil) @description = text if text @description end |
#headers(type = Attributor::Hash.of(key:String), **opts, &block) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/praxis/action_definition.rb', line 107 def headers(type=Attributor::Hash.of(key:String), **opts, &block) return @headers unless block if @headers update_attribute(@headers, opts, block) else @headers = create_attribute(type, dsl_compiler: HeadersDSLCompiler, case_insensitive_load: true, **opts, &block) end @precomputed_header_keys_for_rack = nil #clear memoized data end |
#params(type = Attributor::Struct, **opts, &block) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/praxis/action_definition.rb', line 77 def params(type=Attributor::Struct, **opts, &block) return @params if !block && type == Attributor::Struct if @params unless type == Attributor::Struct && @params.type < Attributor::Struct raise Exceptions::InvalidConfiguration.new( "Invalid type received for extending params: #{type.name}" ) end update_attribute(@params, opts, block) else @params = create_attribute(type, **opts, &block) end end |
#payload(type = Attributor::Struct, **opts, &block) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/praxis/action_definition.rb', line 92 def payload(type=Attributor::Struct, **opts, &block) return @payload if !block && type == Attributor::Struct if @payload unless type == Attributor::Struct && @payload.type < Attributor::Struct raise Exceptions::InvalidConfiguration.new( "Invalid type received for extending params: #{type.name}" ) end update_attribute(@payload, opts, block) else @payload = create_attribute(type, **opts, &block) end end |
#precomputed_header_keys_for_rack ⇒ Object
Good optimization to avoid creating lots of strings and comparisons on a per-request basis. However, this is hacky, as it is rack-specific, and does not really belong here
122 123 124 125 126 127 128 129 130 |
# File 'lib/praxis/action_definition.rb', line 122 def precomputed_header_keys_for_rack @precomputed_header_keys_for_rack ||= begin @headers.attributes.keys.each_with_object(Hash.new) do |key,hash| name = key.to_s name = "HTTP_#{name.upcase}" unless ( name == "CONTENT_TYPE" || name == "CONTENT_LENGTH" ) hash[name] = key end end end |
#response(name, **args) ⇒ Object
57 58 59 60 |
# File 'lib/praxis/action_definition.rb', line 57 def response( name, **args ) template = ApiDefinition.instance.response(name) @responses[name] = template.compile(self, **args) end |
#routing(&block) ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/praxis/action_definition.rb', line 133 def routing(&block) routing_config = Skeletor::RestfulRoutingConfig.new(name, resource_definition, &block) @routes = routing_config.routes @primary_route = routing_config.routes.first @named_routes = routing_config.routes.each_with_object({}) do |route, hash| next if route.name.nil? hash[route.name] = route end end |
#update_attribute(attribute, options, block) ⇒ Object
52 53 54 55 |
# File 'lib/praxis/action_definition.rb', line 52 def update_attribute(attribute, , block) attribute..merge!() attribute.type.attributes(, &block) end |
#use(trait_name) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/praxis/action_definition.rb', line 70 def use(trait_name) unless ApiDefinition.instance.traits.has_key? trait_name raise Exceptions::InvalidTrait.new("Trait #{trait_name} not found in the system") end self.instance_eval(&ApiDefinition.instance.traits[trait_name]) end |