Class: Marley::ModelController
Overview
Translates REST URL’s into ORM calls to the model class with which it is initialized.
Instance Method Summary collapse
-
#initialize(model) ⇒ ModelController
constructor
A new instance of ModelController.
- #rest_delete ⇒ Object
- #rest_get ⇒ Object
- #rest_post ⇒ Object
- #rest_put ⇒ Object
Constructor Details
#initialize(model) ⇒ ModelController
Returns a new instance of ModelController.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/marley/controllers.rb', line 4 def initialize(model) @model=model if $request[:path][1].to_s.match(/^\d+$/) #references a specific instance by ID @instance=@model.list_dataset(:"#{@model.table_name}__id" => $request[:path][1].to_i).all[0] raise RoutingError unless @instance @method_name=$request[:path][2].sub(/[\?\&\+]$/,'') rescue nil #ditch trailing characters, if any if @method_name raise RoutingError unless @instance.respond_to?(@method_name) @method=@instance.method(@method_name) end else #class method -- should yield 0 or more instances of model in an array @method_name=$request[:path][1].sub(/[\?\&\+]$/,'') rescue nil #ditch trailing characters, if any @method_name='list' if @method_name.nil? && $request[:verb]=='rest_get' if @method_name raise RoutingError unless @model.respond_to?(@method_name) @method=@model.method(@method_name) end end if (a=@instance || @model).requires_user? raise AuthorizationError unless a.(@method_name) end if @method && $request[:verb] != 'rest_post' @instances=if p=$request[:get_params][@model.resource_name.to_sym] @method.call(p) elsif i=$request[:path][3] @method.call[i.to_i] else @method.call end end end |
Instance Method Details
#rest_delete ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/marley/controllers.rb', line 62 def rest_delete raise RoutingError unless @instance if @instances @instances.each do |instance| meth="remove_#{instance.class}" raise RoutingError unless @instance.respond_to?(meth) @instance.send(meth,instance) end else @instance.destroy end end |
#rest_get ⇒ Object
35 |
# File 'lib/marley/controllers.rb', line 35 def rest_get; @instances || @instance; end |
#rest_post ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/marley/controllers.rb', line 36 def rest_post if @instance raise RoutingError unless @method params=$request[:post_params][@method_name.to_sym] || $request[:post_params][@model.resource_name.to_sym][@method_name.to_sym] raise ValidationFailed unless params params=[params] unless params.class==Array params.map do |param| @instance.send("add_#{@method_name}",param) end else params=($request[:post_params][@model.resource_name.to_sym]||{}) @instance=@model.new(params.reject {|k,v| v.nil?}) #reject nils to work around sequel validation flaw raise AuthorizationError if params.keys.find {|k| ! @instance.write_cols.include?(k) } params.keys.each {|k| @instance.send("#{k.to_s}=",params[k]) if k.to_s.match(/^_/)} @instance.save @instance.respond_to?('create_msg') ? @instance.create_msg : @instance end end |
#rest_put ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/marley/controllers.rb', line 54 def rest_put raise RoutingError unless @instance params=($request[:post_params][@model.resource_name.to_sym]||{}) raise AuthorizationError if params.keys.find {|k| ! @instance.write_cols.include?(k) } params.keys.each {|k| @instance.send("#{k.to_s}=",params[k]) if k.to_s.match(/^_/)} @instance.modified! @instance.update_only(params,@instance.write_cols) end |