hyper-resource 1.0.0.lap42 → 1.0.0.lap43

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: da77708e3267f001793c0e6ffc81e8918eee67a4c15506bc653dec30c2e8b6c4
4
- data.tar.gz: 8732baa55c60b86e196909aacfa035101f92da9949c7d18456035abf22183b0c
3
+ metadata.gz: c900a50935ad8ee04c936e060ffbaacfdc8e7f6905ce08b262547f00cc194840
4
+ data.tar.gz: 75f3ed8b57e4bafd58d43596d30a339a15fca9caf0d1b600e89fcc17ed6631a1
5
5
  SHA512:
6
- metadata.gz: a9fa531b7f57850ab4a812ad0d0019913b2442985099d92dfb320e930f91ac80f726db9359b90e1de0b11186dc4845abc08046f263cd138a39db0217cf8aceba
7
- data.tar.gz: 7c7b24c30062a03cdbd826cb0f25896eb6ef205d5c376951ea6769105bc8e15429fb9ea25e09bfc7c7134801603c2f2bf48f056c8a2a2f470cad9fde3bbe5ff1
6
+ metadata.gz: 778be7dd25209e8f2d8bd443471bff11053edceaf8c6a70ae593393a71ef72d788c8c9ca49cd56d4ad254a46c01166b2a1f5ae8ec9ae2f9f119e0bfd248d6a07
7
+ data.tar.gz: '096a369c3e717cbfd20af88742acaddca0ca9c1947dd0d698d77c4269ba9bf47e8dbf7ebe86ff9bcaf71385b26798de5f9cff84a8744c143b00f7c4b155f00b0'
@@ -289,6 +289,38 @@ module HyperRecord
289
289
  @reflections ||= {}
290
290
  end
291
291
 
292
+ def rest_class_method(name, options = { default_result: '...' })
293
+ rest_methods[name] = options
294
+ rest_methods[name][:class_method] = true
295
+ singleton_class.send(:define_method, name) do |*args|
296
+ _register_class_observer
297
+ if rest_methods[name][:force] || !rest_methods[name].has_key?(:result)
298
+ _promise_get_or_patch("#{resource_base_uri}/methods/#{name}.json?timestamp=#{`Date.now() + Math.random()`}", *args).then do |response_json|
299
+ rest_methods[name][:result] = response_json[:result] # result is parsed json
300
+ _notify_class_observers
301
+ rest_methods[name][:result]
302
+ end.fail do |response|
303
+ error_message = "#{self.to_s}.#{name}, a rest_method, failed to fetch records!"
304
+ `console.error(error_message)`
305
+ response
306
+ end
307
+ end
308
+ if rest_methods[name].has_key?(:result)
309
+ rest_methods[name][:result]
310
+ else
311
+ rest_methods[name][:default_result]
312
+ end
313
+ end
314
+ end
315
+
316
+ def rest_class_method_force_updates(method_name)
317
+ rest_methods[method_name][:force] = true
318
+ end
319
+
320
+ def rest_class_method_unforce_updates(method_name)
321
+ rest_methods[method_name][:force] = false
322
+ end
323
+
292
324
  def rest_method(name, options = { default_result: '...' })
293
325
  rest_methods[name] = options
294
326
  define_method(name) do |*args|
@@ -1,5 +1,18 @@
1
1
  module HyperRecord
2
2
  module ServerClassMethods
3
+ def rest_class_method(name, options = { default_result: '...' }, &block)
4
+ rest_methods[name] = options
5
+ rest_methods[name][:params] = block.arity
6
+ rest_methods[name][:class_method] = true
7
+ singleton_class.send(:define_method, name) do |*args|
8
+ if args.size > 0
9
+ block.call(*args)
10
+ else
11
+ block.call
12
+ end
13
+ end
14
+ end
15
+
3
16
  def rest_method(name, options = { default_result: '...' }, &block)
4
17
  rest_methods[name] = options
5
18
  rest_methods[name][:params] = block.arity
@@ -1,29 +1,50 @@
1
1
  class Hyperloop::Resource::MethodsController < ApplicationController
2
2
  include Hyperloop::Resource::SecurityGuards
3
-
3
+
4
4
  def index
5
- # used for introspection
6
- record_class = guarded_record_class_from_param(record_class_param)
7
- if record_class
8
- @methods = record_class.rest_methods
9
- @record_name = record_class.to_s.underscore.to_sym
5
+ model_klass = guarded_record_class_from_param(model_klass_param)
6
+
7
+ if model_klass
8
+ @methods = model_klass.rest_methods
9
+ @model_name = model_klass.to_s.underscore.to_sym
10
10
  end
11
- respond_to { |format| format.json { render(json: {}, status: :unprocessable_entity) if record_class.nil? }}
11
+ respond_to { |format| format.json { render(json: {}, status: :unprocessable_entity) if model_klass.nil? }}
12
12
  end
13
13
 
14
14
  def show
15
- @record, id = guarded_record_from_params(params)
16
- method_name = params[:id].to_sym
17
15
  result = { error: 'A error occured, wrong method?' }
18
16
  error = true
19
- if @record.class.rest_methods.has_key?(method_name)
20
- begin
21
- result = @record.send(method_name)
22
- error = false
23
- rescue Exception => e
24
- Rails.logger.debug e.message
25
- result = { error: e.message }
26
- error = true
17
+ mc_param = params[:model_klass]
18
+ if mc_param
19
+ # rest_class_method
20
+ mc_param = mc_param.chop if mc_param.end_with?('s')
21
+ @model_klass = guarded_record_class_from_param(mc_param)
22
+ method_name = params[:id].to_sym
23
+ if @model_klass.rest_methods.has_key?(method_name)
24
+ if @model_klass.rest_methods[method_name][:class_method]
25
+ begin
26
+ result = @model_klass.send(method_name)
27
+ error = false
28
+ rescue Exception => e
29
+ Rails.logger.debug e.message
30
+ result = { error: e.message }
31
+ error = true
32
+ end
33
+ end
34
+ end
35
+ else
36
+ # rest_method
37
+ @record, id = guarded_record_from_params(params)
38
+ method_name = params[:id].to_sym
39
+ if @record.class.rest_methods.has_key?(method_name)
40
+ begin
41
+ result = @record.send(method_name)
42
+ error = false
43
+ rescue Exception => e
44
+ Rails.logger.debug e.message
45
+ result = { error: e.message }
46
+ error = true
47
+ end
27
48
  end
28
49
  end
29
50
  respond_to do |format|
@@ -34,18 +55,38 @@ class Hyperloop::Resource::MethodsController < ApplicationController
34
55
  end
35
56
 
36
57
  def update
37
- @record, id = guarded_record_from_params(params)
38
- method_name = params[:id].to_sym
39
58
  result = { error: 'A error occured, wrong method?' }
40
59
  error = true
41
- if @record.class.rest_methods.has_key?(method_name)
42
- begin
43
- result = @record.send(method_name, params[:params])
44
- error = false
45
- rescue Exception => e
46
- Rails.logger.debug e.message
47
- result = { error: e.message }
48
- error = true
60
+ mc_param = params[:model_klass]
61
+ if mc_param
62
+ # rest_class_method
63
+ mc_param = mc_param.chop if mc_param.end_with?('s')
64
+ @model_klass = guarded_record_class_from_param(mc_param)
65
+ method_name = params[:id].to_sym
66
+ if @model_klass.rest_methods.has_key?(method_name)
67
+ if @model_klass.rest_methods[method_name][:class_method]
68
+ begin
69
+ result = @model_klass.send(method_name, params[:params])
70
+ error = false
71
+ rescue Exception => e
72
+ Rails.logger.debug e.message
73
+ result = { error: e.message }
74
+ error = true
75
+ end
76
+ end
77
+ end
78
+ else
79
+ @record, id = guarded_record_from_params(params)
80
+ method_name = params[:id].to_sym
81
+ if @record.class.rest_methods.has_key?(method_name)
82
+ begin
83
+ result = @record.send(method_name, params[:params])
84
+ error = false
85
+ rescue Exception => e
86
+ Rails.logger.debug e.message
87
+ result = { error: e.message }
88
+ error = true
89
+ end
49
90
  end
50
91
  end
51
92
  respond_to do |format|
@@ -55,9 +96,4 @@ class Hyperloop::Resource::MethodsController < ApplicationController
55
96
  end
56
97
  end
57
98
 
58
- private
59
-
60
- def record_class_param
61
- params.require(:record_class)
62
- end
63
- end
99
+ end
@@ -1,5 +1,5 @@
1
1
  module Hyperloop
2
2
  module Resource
3
- VERSION = '1.0.0.lap42'
3
+ VERSION = '1.0.0.lap43'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyper-resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.lap42
4
+ version: 1.0.0.lap43
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Biedermann