nube 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +98 -6
- data/lib/nube/base.rb +7 -5
- data/lib/nube/remote_relation.rb +11 -10
- data/lib/nube/remote_scope.rb +3 -8
- data/lib/nube/site.rb +8 -3
- data/lib/nube/version.rb +1 -1
- metadata +7 -7
data/README.md
CHANGED
@@ -1,9 +1,5 @@
|
|
1
1
|
# Nube
|
2
|
-
|
3
|
-
|
4
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/nube`. To experiment with that code, run `bin/console` for an interactive prompt.
|
5
|
-
|
6
|
-
TODO: Delete this and the text above, and describe your gem
|
2
|
+
**Nube** aims to be as flexible and abstract as possible while helping you work with remote object in different Rails applicaction as activerecord objects. It's 100% compatible with **Associations** (Locals-Remotes, Remote-Remote, Remote-local), **Scopes**, **Validations**. Besides is posible work with object from different Rails applications.
|
7
3
|
|
8
4
|
## Installation
|
9
5
|
|
@@ -22,8 +18,104 @@ Or install it yourself as:
|
|
22
18
|
$ gem install nube
|
23
19
|
|
24
20
|
## Usage
|
21
|
+
### Associations
|
22
|
+
Its possible define relations between differents class, locals with remotes. It's possible use any options, ***as***, ***dependent***, ***foreing_key***, , ***class_name***, etc.
|
23
|
+
For local class is only necessary:
|
24
|
+
1. Add ***include LocalAssociation***.
|
25
|
+
2. Define associations as activerecord but use ***remote_*** when you like define one relation with a remote class, for example: remote_belongs_to, remote_has_one, remote_has_many.
|
26
|
+
|
27
|
+
For Remote class is only necessary:
|
28
|
+
1. All remote class inherits from **Nube::Base**.
|
29
|
+
2. All relation definition is with ***remote_***
|
30
|
+
|
31
|
+
Examples:
|
32
|
+
|
33
|
+
#### Local class with Remote class or Remote class with Local class
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
# Rails app A with the persisting object
|
37
|
+
Class Client < ActiveRecord::Base
|
38
|
+
include LocalAssociation
|
39
|
+
remote_has_one :remote_class, dependent: :nullify, foreign_key: "remote_class_foreign_key_id"
|
40
|
+
remote_has_one :other_remote_class, through: :foo
|
41
|
+
remote_has_many :foo_bar_remote_class, class_name: "RemoteClass", foreign_key: 'foo_bar_remote_class_id'
|
42
|
+
belongs_to :remoteable, polymorphic: true
|
43
|
+
belong_to :bar
|
44
|
+
end
|
45
|
+
|
46
|
+
# Rails app A with the persisting object
|
47
|
+
class Bar < ActiveRecord::Base
|
48
|
+
has_one :client
|
49
|
+
end
|
50
|
+
|
51
|
+
# Rails app B without the persisting object
|
52
|
+
class Remote_class << Nube::Base
|
53
|
+
remote_belongs_to :client
|
54
|
+
remote_has_many :clients
|
55
|
+
has_many :clients, as: :remoteable, class_name: "Client", dependent: :destroy
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
#### Remote class with Remote class
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
# Rails app B without the persisting object
|
63
|
+
Class Client < Nube::Base
|
64
|
+
include LocalAssociation
|
65
|
+
remote_has_one :remote_class, dependent: :nullify, foreign_key: "remote_class_foreign_key_id"
|
66
|
+
remote_has_one :other_remote_class, through: :foo
|
67
|
+
remote_has_many :foo_bar_remote_class, class_name: "RemoteClass", foreign_key: 'foo_bar_remote_class_id'
|
68
|
+
remote_belongs_to :remoteable, polymorphic: true
|
69
|
+
end
|
70
|
+
|
71
|
+
# Rails app B without the persisting object
|
72
|
+
class Remote_class < Nube::Base
|
73
|
+
remote_remote_belongs_to :client
|
74
|
+
remote_has_many :clients
|
75
|
+
remote_has_many :clients, as: :remoteable, class_name: "Client", dependent: :destroy
|
76
|
+
end
|
77
|
+
```
|
78
|
+
### Scopes
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
# Rails app A with the persisting object
|
82
|
+
Class Client < ActiveRecord::Base # Rails app A
|
83
|
+
scope :submitted, -> { where(submitted: true) }
|
84
|
+
scope :newer_than, -> (date1, date2) { where('start_date > ? and start_date < ?', date1, date2) }
|
85
|
+
scope :late, -> { where("timesheet.submitted_at <= ?", 7.days.ago) }
|
86
|
+
end
|
87
|
+
|
88
|
+
# Rails app B without the persisting object
|
89
|
+
class Client < Nube::Base
|
90
|
+
scope :submitted # Only define the name
|
91
|
+
scope :newer_than, using: [:date1, :date2] # Onlye define scope name, and options.
|
92
|
+
scope :foo, remote_scope: :late #Online define name. In this case the scope it's renamed
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
### Validations
|
97
|
+
|
98
|
+
The validations must be defined where activerecord models exist. So when a class it's defined as Nube::Base, it's not necessary define any validations.
|
99
|
+
|
100
|
+
### API Controller
|
101
|
+
|
102
|
+
It's only necessary define:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
class AnyController < ApplicationController
|
106
|
+
include NubeController
|
107
|
+
RESOURCE = ModelName
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
The public action are:
|
25
112
|
|
26
|
-
|
113
|
+
1. index
|
114
|
+
2. count
|
115
|
+
3. create
|
116
|
+
4. update
|
117
|
+
5. update_all
|
118
|
+
6. destroy_all
|
27
119
|
|
28
120
|
## Development
|
29
121
|
|
data/lib/nube/base.rb
CHANGED
@@ -26,12 +26,12 @@ module Nube
|
|
26
26
|
|
27
27
|
def save(params={})
|
28
28
|
if new_record?
|
29
|
-
remote_obj = self.class.post(
|
29
|
+
remote_obj = self.class.post(identity.merge(params), { attributes: @attributes } ).first
|
30
30
|
@attributes = remote_obj["object"]
|
31
31
|
@errors = remote_obj["errors"]
|
32
32
|
@new_record = false if @errors.empty?
|
33
33
|
else
|
34
|
-
@errors = self.class.put(
|
34
|
+
@errors = self.class.put(identity.merge(params.merge(id: id)), { attributes: @attrs_changed, id: id } ).first
|
35
35
|
end
|
36
36
|
@errors.empty?
|
37
37
|
end
|
@@ -50,8 +50,8 @@ module Nube
|
|
50
50
|
end
|
51
51
|
|
52
52
|
["get","post","put","delete"].each do |method|
|
53
|
-
define_singleton_method method do |
|
54
|
-
do_request(method,
|
53
|
+
define_singleton_method method do |site_options, params={}|
|
54
|
+
do_request(method, site_options, params)
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
@@ -97,10 +97,12 @@ module Nube
|
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
100
|
-
def self.do_request(method,
|
100
|
+
def self.do_request(method, site_options, params={})
|
101
|
+
site = site(site_options[:identity], self.name.pluralize.underscore, site_options[:action], site_options[:id])
|
101
102
|
url = URI.parse(site)
|
102
103
|
req = "Net::HTTP::#{method.to_s.camelize}".constantize.new(url.to_s)
|
103
104
|
req.body = params.to_param
|
105
|
+
req['Authorization'] = "Token token=#{token(site_options[:identity])}"
|
104
106
|
res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) }
|
105
107
|
if (res.code == "200")
|
106
108
|
[JSON.parse(res.body, quirks_mode: true)].flatten.compact
|
data/lib/nube/remote_relation.rb
CHANGED
@@ -23,7 +23,7 @@ module RemoteRelation
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def destroy_all(where={}, relations={})
|
26
|
-
@xmodel.delete(@
|
26
|
+
@xmodel.delete(@params.delete(:site_options).merge({ action: 'destroy_all' }), { where: [where].flatten }).map{|attrs| @xmodel.new(attrs, false) }
|
27
27
|
end
|
28
28
|
|
29
29
|
def update_all(changes={}, where={}, relations={})
|
@@ -31,19 +31,19 @@ module RemoteRelation
|
|
31
31
|
# change true and false by 1 or 0 becouse params are sent as string
|
32
32
|
changes[attrib] = (value ? 1 : 0) if !!value == value
|
33
33
|
end
|
34
|
-
@xmodel.put(@
|
34
|
+
@xmodel.put(@params.delete(:site_options).merge({ action: 'update_all' }), { changes: changes, where: [where].flatten }).first
|
35
35
|
end
|
36
36
|
|
37
37
|
def empty_attributes
|
38
|
-
@xmodel.get(@
|
38
|
+
@xmodel.get(@params.delete(:site_options).merge({ action: 'empty_attributes' })).first
|
39
39
|
end
|
40
40
|
|
41
41
|
def count
|
42
|
-
@xmodel.get(@
|
42
|
+
@xmodel.get(@params.delete(:site_options).merge({ action: 'count' }), @params).first.to_i
|
43
43
|
end
|
44
44
|
|
45
45
|
def all
|
46
|
-
@xmodel.get(@
|
46
|
+
@xmodel.get(@params.delete(:site_options), @params).map{|attributes| @xmodel.new(attributes, false) }
|
47
47
|
end
|
48
48
|
|
49
49
|
def first
|
@@ -69,7 +69,8 @@ module RemoteRelation
|
|
69
69
|
|
70
70
|
def create(attrs={})
|
71
71
|
obj = @xmodel.new(attrs)
|
72
|
-
obj.save
|
72
|
+
obj.save
|
73
|
+
obj
|
73
74
|
end
|
74
75
|
|
75
76
|
def where(conditions); tap{|s| (s.params[:where] || s.params[:where] = []) << conditions}; end
|
@@ -104,14 +105,14 @@ module RemoteRelation
|
|
104
105
|
alias_method :ransack, :search
|
105
106
|
|
106
107
|
def massive_transactions(transaction, method, action)
|
107
|
-
@xmodel.send(method, @
|
108
|
+
@xmodel.send(method, @params.delete(:site_options).merge({ action: action }), @params.merge(transaction: transaction))
|
108
109
|
end
|
109
110
|
|
110
|
-
def massive_creation(transaction); massive_transactions(transaction, 'post', 'massive_creation'); end
|
111
|
+
def massive_creation(transaction); massive_transactions(transaction, 'post', 'massive_creation', site_options); end
|
111
112
|
|
112
|
-
def massive_sum(transaction); massive_transactions(transaction, 'put', 'massive_sum'); end
|
113
|
+
def massive_sum(transaction); massive_transactions(transaction, 'put', 'massive_sum', site_options); end
|
113
114
|
|
114
|
-
def massive_update(transaction); massive_transactions(transaction, 'put', 'massive_update'); end
|
115
|
+
def massive_update(transaction); massive_transactions(transaction, 'put', 'massive_update', site_options); end
|
115
116
|
|
116
117
|
def method_missing(name, *args, &block)
|
117
118
|
all.send(name, *args, &block)
|
data/lib/nube/remote_scope.rb
CHANGED
@@ -5,9 +5,9 @@ module RemoteScope
|
|
5
5
|
|
6
6
|
class_methods do
|
7
7
|
|
8
|
-
def node(
|
9
|
-
|
10
|
-
"#{self}Relation".constantize.new(self, { site_options:
|
8
|
+
def node(identity=nil)
|
9
|
+
# identity = IDENTITY_SITE if not identity and defined?(IDENTITY_SITE)
|
10
|
+
"#{self}Relation".constantize.new(self, { site_options: {identity: identity} })
|
11
11
|
end
|
12
12
|
|
13
13
|
def build_params keys, values
|
@@ -18,11 +18,6 @@ module RemoteScope
|
|
18
18
|
def scope(name, opts={})
|
19
19
|
scope_name = opts.has_key?(:remote_scope) ? opts[:remote_scope] : name
|
20
20
|
|
21
|
-
define_singleton_method name do |*values|
|
22
|
-
scope_params = build_params(opts[:using].to_a, values)
|
23
|
-
"#{self}Relation".constantize.new(self, { scopes: {scope_name => scope_params} })
|
24
|
-
end
|
25
|
-
|
26
21
|
"#{self}Relation".constantize.class_eval do
|
27
22
|
define_method name do |*values|
|
28
23
|
scope_params = xmodel.build_params(opts[:using].to_a, values)
|
data/lib/nube/site.rb
CHANGED
@@ -3,14 +3,15 @@ module Site
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
class_methods do
|
6
|
-
|
6
|
+
# Methods to override
|
7
|
+
def site(identity, controller_name, action_name, object_id, opts={})
|
7
8
|
# Cunado esto esa el BMU el server_id siempre va ser a nil, esto es por que la url esta fixeada en un archivo.
|
8
9
|
# ServerConfiguration.find_by_server_id(args.first).site o algo parecido
|
9
10
|
# CLOUD_URL + "/api/v1/#{args[0]}"
|
10
|
-
host_resource(
|
11
|
+
host_resource(identity) + path_resource + controller_name + (object_id ? ('/' + object_id.to_s) : '') + (action_name ? ('/' + action_name) : '') + default_options(opts)
|
11
12
|
end
|
12
13
|
|
13
|
-
def host_resource(
|
14
|
+
def host_resource(identity)
|
14
15
|
# CLOUD_URL
|
15
16
|
"http://0.0.0.0:3002"
|
16
17
|
end
|
@@ -22,6 +23,10 @@ module Site
|
|
22
23
|
def default_options(opts={})
|
23
24
|
(opts.nil? || opts.empty?) ? '' : ('?' + opts.map{|key,value| "#{key}=#{value}" }.join("&"))
|
24
25
|
end
|
26
|
+
|
27
|
+
def token(identity)
|
28
|
+
identity
|
29
|
+
end
|
25
30
|
end
|
26
31
|
|
27
32
|
def site(*args)
|
data/lib/nube/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nube
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- g.edera
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: exe
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2016-
|
19
|
+
date: 2016-04-15 00:00:00 -03:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -27,7 +27,7 @@ dependencies:
|
|
27
27
|
requirements:
|
28
28
|
- - ~>
|
29
29
|
- !ruby/object:Gem::Version
|
30
|
-
hash:
|
30
|
+
hash: 999358234
|
31
31
|
segments:
|
32
32
|
- 4
|
33
33
|
- x
|
@@ -42,7 +42,7 @@ dependencies:
|
|
42
42
|
requirements:
|
43
43
|
- - ~>
|
44
44
|
- !ruby/object:Gem::Version
|
45
|
-
hash:
|
45
|
+
hash: 999358234
|
46
46
|
segments:
|
47
47
|
- 4
|
48
48
|
- x
|
@@ -57,7 +57,7 @@ dependencies:
|
|
57
57
|
requirements:
|
58
58
|
- - ~>
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
hash:
|
60
|
+
hash: 999358234
|
61
61
|
segments:
|
62
62
|
- 4
|
63
63
|
- x
|