remote_attr_accessor 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d8bafc9588ecb202ca2e69a4be628f030bfcc5e3
4
+ data.tar.gz: b5b7505338e24c90ce1c2261bdc4033c8b5afa47
5
+ SHA512:
6
+ metadata.gz: 001e11dd3ac987ef2d6f24de6463e8da8e4b68c54c41107ee0fadd2114744694de76567f9f7e594b8c724f461ef67ab4dcd6f9d1266dfa829a0569ae7358be7e
7
+ data.tar.gz: 55d7c1fe45b97802231d086f6a8f7c739f0806e7347e51407aecb412448abfca6273a1b86355fa819039c7f93d099298c38913c11547c9143df8672de5c3809b
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 kenji.suzuki
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,88 @@
1
+ = [Beta] RemoteAttrAccessor
2
+
3
+ == Purpose
4
+ Get remote server's attributes and set them into local attributes via API.
5
+
6
+ === For example : Micorservice's user infomation
7
+
8
+ User's infomation will be used by other servers. So, prepare remote(provider) server for common user infomation provider, and Other servers(client) use 'remote_attr_accessor' to get common infomation of users.
9
+
10
+ == Settings
11
+ === Set Config
12
+ Make file in "lib/remote_attr_accessor/config.rb" to Overwrite for your environment.
13
+
14
+ module RemoteAttrAccessor
15
+ class Config
16
+ def self.id_name
17
+ 'id'
18
+ end
19
+
20
+ def self.prefix
21
+ 'remote_'
22
+ end
23
+
24
+ def self.remote_json_key
25
+ 'users'
26
+ end
27
+
28
+ def self.remote_attrs
29
+ [:email, :last_name, :first_name]
30
+ end
31
+
32
+ def self.remote_attrs_with_prefix
33
+ @remote_attrs_with_prefix = remote_attrs.map{|attr| "#{prefix}#{attr}"}
34
+ end
35
+ end
36
+ end
37
+
38
+ === Set API
39
+ Make file in "lib/remote_attr_accessor/api.rb" to Overwrite for your environments.
40
+
41
+ module RemoteAttrAccessor
42
+ class Api
43
+ def self.get_remote_attrs(ids)
44
+ # Write here API access code
45
+ end
46
+
47
+ def self.upsert_remote_attr(params)
48
+ # Write here API access code
49
+ end
50
+ end
51
+ end
52
+
53
+ ==== Response format of RemoteAttrAccessor::API.get_remote_attrs
54
+ {
55
+ "users": {
56
+ "1": {
57
+ "id": 1,
58
+ "email": "kenjiszk@example.com",
59
+ "last_name": szk,
60
+ "first_name": kenji,
61
+ "created_at": "2016-05-31T18:04:05.000Z",
62
+ "updated_at": "2016-06-03T05:37:51.000Z"
63
+ },
64
+ "2": {
65
+ "id": 2,
66
+ "email": "sample@example.com",
67
+ "last_name": ple,
68
+ "first_name": sam,
69
+ "created_at": "2016-05-31T07:51:10.000Z",
70
+ "updated_at": "2016-05-31T07:51:10.000Z"
71
+ }
72
+ }
73
+ }
74
+
75
+ == How to get/set remote attribute
76
+ Use sample for "User model".
77
+
78
+ users = User.limit(1)
79
+ user.first.remote_email # Get users email from remote server
80
+ user.first.remote_email = 'test@example.com' # Set users email to remote server
81
+
82
+ == Bulk get from remote attribute
83
+
84
+ users = User.limit(1)
85
+ RemoteAttrAccessor::Bulk.load_remote_attr(users) # Get users info in bulk and set instance variable
86
+ user.first.remote_email # Not access to remote server
87
+
88
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'RemoteAttrAccessor'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
@@ -0,0 +1,11 @@
1
+ module RemoteAttrAccessor
2
+ # Override by lib/remote_attr_accessor/api.rb
3
+ class Api
4
+ def self.get_remote_attrs(ids)
5
+ {}
6
+ end
7
+
8
+ def self.upsert_remote_attr(params)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+ module RemoteAttrAccessor::Attrs
2
+ extend RemoteAttrAccessor::Base
3
+
4
+ Dir.glob("#{Dir.pwd}/lib/remote_attr_accessor/*.rb").each { |r| require r}
5
+
6
+ config.remote_attrs.each do |attr|
7
+ attr_with_prefix = config.prefix + attr.to_s
8
+ define_method(attr_with_prefix) do
9
+ config = RemoteAttrAccessor::Config
10
+ remote_id = eval(config.id_name).to_s
11
+ eval("@#{attr_with_prefix}") ||
12
+ instance_variable_set(
13
+ "@#{attr_with_prefix}",
14
+ RemoteAttrAccessor::Api.get_remote_attrs([remote_id])[config.remote_json_key][remote_id][attr.to_s]
15
+ )
16
+ end
17
+
18
+ define_method("#{attr_with_prefix}=") do |val|
19
+ config = RemoteAttrAccessor::Config
20
+ params = Hash.new
21
+ params[config.id_name] = eval(config.id_name).to_s
22
+ params[attr.to_s] = val
23
+ RemoteAttrAccessor::Api.upsert_remote_attr(params)
24
+ instance_variable_set("@#{attr_with_prefix}", nil)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,9 @@
1
+ module RemoteAttrAccessor::Base
2
+ def config
3
+ RemoteAttrAccessor::Config
4
+ end
5
+
6
+ def api
7
+ RemoteAttrAccessor::Api
8
+ end
9
+ end
@@ -0,0 +1,29 @@
1
+ module RemoteAttrAccessor::Bulk
2
+ extend RemoteAttrAccessor::Base
3
+
4
+ def load_remote_attr(locals)
5
+ id_name = config.id_name
6
+ ids = Array.new
7
+ if locals.is_a?(ActiveRecord::Relation)
8
+ locals.each do |local|
9
+ ids << local[id_name]
10
+ end
11
+ elsif locals.is_a?(ActiveRecord::Base)
12
+ ids << locals[id_name]
13
+ end
14
+
15
+ remote_attrs_json = api.get_remote_attrs(ids)
16
+ return if remote_attrs_json == {}
17
+ remote_attrs = remote_attrs_json[config.remote_json_key]
18
+
19
+ (locals.is_a?(ActiveRecord::Relation || Array) ? locals : [locals]).each do |local|
20
+ ignored_attrs = [id_name.to_s, 'created_at', 'updated_at']
21
+ (remote_attrs[local[id_name].to_s].keys - ignored_attrs).each do |rattr|
22
+ attr_with_prefix = "@#{config.prefix}#{rattr}"
23
+ local.instance_variable_set(attr_with_prefix, remote_attrs[local[id_name].to_s][rattr.to_s])
24
+ end
25
+ end
26
+ end
27
+
28
+ module_function :load_remote_attr
29
+ end
@@ -0,0 +1,24 @@
1
+ module RemoteAttrAccessor
2
+ # Override by lib/remote_attr_accessor/config.rb
3
+ class Config
4
+ def self.id_name
5
+ 'id'
6
+ end
7
+
8
+ def self.prefix
9
+ 'remote_'
10
+ end
11
+
12
+ def self.remote_json_key
13
+ 'users'
14
+ end
15
+
16
+ def self.remote_attrs
17
+ [:email, :last_name, :first_name]
18
+ end
19
+
20
+ def self.remote_attrs_with_prefix
21
+ @remote_attrs_with_prefix = remote_attrs.map{|attr| "#{prefix}#{attr}"}
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module RemoteAttrAccessor
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ module RemoteAttrAccessor
2
+ end
3
+
4
+ require_relative 'remote_attr_accessor/version'
5
+ require_relative 'remote_attr_accessor/config'
6
+ require_relative 'remote_attr_accessor/base'
7
+ require_relative 'remote_attr_accessor/api'
8
+ require_relative 'remote_attr_accessor/attrs'
9
+ require_relative 'remote_attr_accessor/bulk'
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :remote_attr_accessor do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: remote_attr_accessor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - kenji.suzuki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Access to Remote Rails Model by API and set to Local Model attribute.
42
+ email:
43
+ - kenji.suzuki.1984@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.rdoc
50
+ - Rakefile
51
+ - lib/remote_attr_accessor.rb
52
+ - lib/remote_attr_accessor/api.rb
53
+ - lib/remote_attr_accessor/attrs.rb
54
+ - lib/remote_attr_accessor/base.rb
55
+ - lib/remote_attr_accessor/bulk.rb
56
+ - lib/remote_attr_accessor/config.rb
57
+ - lib/remote_attr_accessor/version.rb
58
+ - lib/tasks/remote_attr_accessor_tasks.rake
59
+ homepage: ''
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.4.5.1
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Remote Model Attribute Accessor.
83
+ test_files: []