lib/dao/api/context.rb in dao-3.3.0 vs lib/dao/api/context.rb in dao-4.2.1

- old
+ new

@@ -1,68 +1,58 @@ module Dao class Context - Attrs = %w( api route path interface args status errors params result data form validations ) + Attrs = %w( + api path route endpoint + params validator errors status form + result + data + args + ) Attrs.each{|a| attr_accessor(a)} def Context.attrs Attrs end - def Context.for(api, route, path, interface, params, *args) - # setup - # - options = Dao.options_for!(args) + def Context.for(*args, &block) + new(*args, &block) + end - parsed_params = Dao.parse(path, params, options) + def initialize(api, path, route, endpoint, params, *args) + @api = api + @path = path + @route = route + @endpoint = endpoint - result = Result.new(:mode => api.mode) - params = result.params - params.update(parsed_params) + @params = Params.new + @params.update(params) + @params.path = @path + @params.route = @route + @form = @params.form - args = - if interface.arity < 1 - [params, result] - else - [params, result].slice(0, interface.arity) - end + @validator = Validator.new(@params) + @validator.validations_search_path.unshift(@api.class) - # build the context - # - context = new - context.api = api - context.interface = interface - context.route = route - context.path = path - context.args = args - context.status = Status.default - context.errors = Errors.new + @validations = @validator.validations - context.result = result - context.data = result.data + @params.validator = @validator + @errors = @validator.errors + @status = @validator.status - context.params = params - context.form = params.form - context.validations = params.validations + @result = Result.new + @result.path = @path + @result.route = @route + @result.mode = @api.mode + @result.params = @params + @result.errors = @params.errors - # wire up shared state - # - result.route = context.route - result.path = context.path - result.status = context.status - result.errors = context.errors + @data = @result.data - params.route = context.route - params.path = context.path - params.status = context.status - params.errors = context.errors - - context + @args = @endpoint.arity < 1 ? [@params, @result] : [@params, @result].slice(0, @endpoint.arity) end - include InstanceExec - def call - api.instance_exec(*args, &interface) + @api.instance_exec(*@args, &@endpoint) end end end