cru_lib 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +30 -0
- data/Rakefile +2 -0
- data/cru_lib.gemspec +23 -0
- data/lib/cru_lib/async.rb +23 -0
- data/lib/cru_lib/global_registry_methods.rb +131 -0
- data/lib/cru_lib/global_registry_relationship_methods.rb +106 -0
- data/lib/cru_lib/version.rb +3 -0
- data/lib/cru_lib.rb +7 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5c8f38e8b4e257661870015c17629990db58161d
|
4
|
+
data.tar.gz: 9998e4d8a67bb11c536e7b4cbc7c2ac6282c6fc9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: af8bb684ad41e2666550aef5042653c4fec652146d47d9a40eff77187c7f2123fc6a0ef8e5917e56f44342ed16c878c2a92848c9e7f764917ef4f7c33d529745
|
7
|
+
data.tar.gz: 75a86c8d69ed9fb4dbb0453f425ceffc13b5d943bebdc4d1dd00dc4d0bbd79b1d13f6e4dbe378f9c7ce64526a4a9da0dd91a19f9e0176b29bb42f479b733212f
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Josh Starcher
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# CruLib
|
2
|
+
|
3
|
+
This gem hold a useful collection of functions, methods and classes used
|
4
|
+
in various Cru apps.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'cru_lib'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install cru_lib
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
TODO: Write usage instructions here
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
|
26
|
+
1. Fork it ( https://github.com/[my-github-username]/cru_lib/fork )
|
27
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
28
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
29
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
30
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/cru_lib.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cru_lib/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cru_lib"
|
8
|
+
spec.version = CruLib::VERSION
|
9
|
+
spec.authors = ["Josh Starcher"]
|
10
|
+
spec.email = ["josh.starcher@gmail.com"]
|
11
|
+
spec.summary = %q{Misc libraries for Cru}
|
12
|
+
spec.description = %q{Collection of common ruby logic used by a number of Cru apps}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module CruLib
|
2
|
+
module Async
|
3
|
+
|
4
|
+
# This will be called by a worker when a job needs to be processed
|
5
|
+
def perform(id, method, *args)
|
6
|
+
if id
|
7
|
+
begin
|
8
|
+
self.class.find(id).send(method, *args)
|
9
|
+
rescue ActiveRecord::RecordNotFound
|
10
|
+
# If the record was deleted after the job was created, swallow it
|
11
|
+
end
|
12
|
+
else
|
13
|
+
self.class.send(method, *args)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def async(method, *args)
|
18
|
+
Sidekiq::Client.enqueue(self.class, id, method, *args)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'cru_lib'
|
2
|
+
require 'cru_lib/async'
|
3
|
+
|
4
|
+
module CruLib
|
5
|
+
module GlobalRegistryMethods
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
include CruLib::Async
|
8
|
+
|
9
|
+
|
10
|
+
included do
|
11
|
+
after_commit :push_to_global_registry
|
12
|
+
after_destroy :delete_from_global_registry
|
13
|
+
end
|
14
|
+
|
15
|
+
def delete_from_global_registry
|
16
|
+
if global_registry_id
|
17
|
+
Sidekiq::Client.enqueue(self.class, nil, :async_delete_from_global_registry, global_registry_id)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Define default push method
|
22
|
+
def push_to_global_registry
|
23
|
+
async(:async_push_to_global_registry)
|
24
|
+
end
|
25
|
+
|
26
|
+
def async_push_to_global_registry(parent_id = nil, parent_type = nil)
|
27
|
+
self.class.push_structure_to_global_registry
|
28
|
+
|
29
|
+
if global_registry_id
|
30
|
+
begin
|
31
|
+
update_in_global_registry(parent_id, parent_type)
|
32
|
+
rescue RestClient::ResourceNotFound
|
33
|
+
create_in_global_registry(parent_id, parent_type)
|
34
|
+
end
|
35
|
+
else
|
36
|
+
create_in_global_registry(parent_id, parent_type)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def attributes_to_push(*args)
|
41
|
+
unless @attributes_to_push
|
42
|
+
@attributes_to_push = {}
|
43
|
+
attributes_to_push['client_integration_id'] = id unless self.class.skip_fields_for_gr.include?('client_integration_id')
|
44
|
+
attributes_to_push['client_updated_at'] = updated_at if respond_to?(:updated_at)
|
45
|
+
attributes.collect {|k, v| @attributes_to_push[k.underscore] = v}
|
46
|
+
@attributes_to_push.select! {|k, v| v.present? && !self.class.skip_fields_for_gr.include?(k)}
|
47
|
+
end
|
48
|
+
@attributes_to_push
|
49
|
+
end
|
50
|
+
|
51
|
+
def update_in_global_registry(parent_id = nil, parent_type = nil)
|
52
|
+
if parent_type
|
53
|
+
create_in_global_registry(parent_id, parent_type)
|
54
|
+
else
|
55
|
+
GlobalRegistry::Entity.put(global_registry_id, {entity: attributes_to_push})
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def create_in_global_registry(parent_id = nil, parent_type = nil)
|
60
|
+
entity_attributes = { self.class.global_registry_entity_type_name => attributes_to_push }
|
61
|
+
if parent_type.present?
|
62
|
+
entity_attributes = {parent_type => entity_attributes}
|
63
|
+
GlobalRegistry::Entity.put(parent_id, {entity: entity_attributes})
|
64
|
+
else
|
65
|
+
entity = GlobalRegistry::Entity.post(entity: entity_attributes)
|
66
|
+
global_registry_id = entity['entity'][self.class.global_registry_entity_type_name]['id']
|
67
|
+
update_column(:global_registry_id, global_registry_id)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
module ClassMethods
|
72
|
+
def push_structure_to_global_registry(parent_id = nil)
|
73
|
+
# Make sure all columns exist
|
74
|
+
entity_type = Rails.cache.fetch(global_registry_entity_type_name, expires_in: 1.hour) do
|
75
|
+
GlobalRegistry::EntityType.get(
|
76
|
+
{'filters[name]' => global_registry_entity_type_name, 'filters[parent_id]' => parent_id}
|
77
|
+
)['entity_types'].first
|
78
|
+
end
|
79
|
+
if entity_type
|
80
|
+
existing_fields = entity_type['fields'].collect {|f| f['name']}
|
81
|
+
else
|
82
|
+
entity_type = GlobalRegistry::EntityType.post(entity_type: {name: global_registry_entity_type_name, parent_id: parent_id, field_type: 'entity'})['entity_type']
|
83
|
+
existing_fields = []
|
84
|
+
end
|
85
|
+
|
86
|
+
columns_to_push.each do |column|
|
87
|
+
unless existing_fields.include?(column[:name])
|
88
|
+
GlobalRegistry::EntityType.post(entity_type: {name: column[:name], parent_id: entity_type['id'], field_type: column[:type]})
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def columns_to_push
|
94
|
+
@columns_to_push ||= columns.select { |c|
|
95
|
+
!skip_fields_for_gr.include?(c.name.underscore)
|
96
|
+
}.collect {|c|
|
97
|
+
{ name: c.name.underscore, type: normalize_column_type(c.type, c.name.underscore) }
|
98
|
+
}
|
99
|
+
end
|
100
|
+
|
101
|
+
def normalize_column_type(column_type, name)
|
102
|
+
case
|
103
|
+
when column_type.to_s == 'text'
|
104
|
+
'string'
|
105
|
+
when name.ends_with?('_id')
|
106
|
+
'uuid'
|
107
|
+
else
|
108
|
+
column_type
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def async_delete_from_global_registry(registry_id)
|
113
|
+
begin
|
114
|
+
GlobalRegistry::Entity.delete(registry_id)
|
115
|
+
rescue RestClient::ResourceNotFound
|
116
|
+
# If the record doesn't exist, we don't care
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def global_registry_entity_type_name
|
121
|
+
to_s.underscore
|
122
|
+
end
|
123
|
+
|
124
|
+
def skip_fields_for_gr
|
125
|
+
%w(id global_registry_id created_at updated_at)
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
@@ -0,0 +1,106 @@
|
|
1
|
+
module CruLib
|
2
|
+
module GlobalRegistryRelationshipMethods
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
include CruLib::GlobalRegistryMethods
|
5
|
+
|
6
|
+
def async_push_to_global_registry
|
7
|
+
super
|
8
|
+
end
|
9
|
+
|
10
|
+
# TODO - deleting a relationship is different.
|
11
|
+
def delete_from_global_registry
|
12
|
+
if global_registry_id
|
13
|
+
Sidekiq::Client.enqueue(self.class, nil, :async_delete_from_global_registry, global_registry_id)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param [String] relationship_name
|
18
|
+
# @param [String] related_name
|
19
|
+
# @param [Object] related_object
|
20
|
+
def attributes_to_push(relationship_name = nil, related_name = nil, related_object = nil)
|
21
|
+
if global_registry_id
|
22
|
+
attributes_to_push = super
|
23
|
+
attributes_to_push
|
24
|
+
else
|
25
|
+
{
|
26
|
+
"#{relationship_name}:relationship" => {
|
27
|
+
client_integration_id: id,
|
28
|
+
related_name => related_object.global_registry_id
|
29
|
+
}
|
30
|
+
}
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# @param [Object] base_object
|
35
|
+
# @param [String] relationship_name
|
36
|
+
def create_in_global_registry(base_object, relationship_name)
|
37
|
+
entity = GlobalRegistry::Entity.put(
|
38
|
+
base_object.global_registry_id,
|
39
|
+
entity: {base_object.class.global_registry_entity_type_name => attributes_to_push}
|
40
|
+
)
|
41
|
+
|
42
|
+
base_object_id = entity['entity'][base_object.class.global_registry_entity_type_name]['id']
|
43
|
+
|
44
|
+
entity = GlobalRegistry::Entity.find(base_object_id)['entity']
|
45
|
+
|
46
|
+
global_registry_id = Array.wrap(
|
47
|
+
entity[base_object.class.global_registry_entity_type_name]["#{relationship_name}:relationship"]
|
48
|
+
).detect { |hash| hash['client_integration_id'] == id.to_s }['relationship_entity_id']
|
49
|
+
|
50
|
+
update_column(:global_registry_id, global_registry_id)
|
51
|
+
|
52
|
+
update_in_global_registry
|
53
|
+
end
|
54
|
+
|
55
|
+
module ClassMethods
|
56
|
+
# @param [Class] base_type
|
57
|
+
# @param [Class] related_type
|
58
|
+
# @param [String] relationship1_name
|
59
|
+
# @param [String] relationship2_name
|
60
|
+
def push_structure_to_global_registry(base_type, related_type, relationship1_name, relationship2_name)
|
61
|
+
# A summer project application is a join table between people and projects
|
62
|
+
base_type_cache_key = "#{base_type.global_registry_entity_type_name}_entity_type"
|
63
|
+
base_entity_type = Rails.cache.fetch(base_type_cache_key, expires_in: 1.hour) do
|
64
|
+
GlobalRegistry::EntityType.get({'filters[name]' => base_type.global_registry_entity_type_name})['entity_types'].first
|
65
|
+
end
|
66
|
+
|
67
|
+
related_type_cache_key = "#{related_type.global_registry_entity_type_name}_entity_type"
|
68
|
+
related_entity_type = Rails.cache.fetch(related_type_cache_key, expires_in: 1.hour) do
|
69
|
+
GlobalRegistry::EntityType.get({'filters[name]' => related_type.global_registry_entity_type_name})['entity_types'].first
|
70
|
+
end
|
71
|
+
|
72
|
+
relationship_type_cache_key = "#{base_type}_#{related_type}_#{relationship1_name}"
|
73
|
+
relationship_type = Rails.cache.fetch(relationship_type_cache_key, expires_in: 1.hour) do
|
74
|
+
GlobalRegistry::RelationshipType.get(
|
75
|
+
{'filters[between]' => "#{base_entity_type['id']},#{related_entity_type['id']}"}
|
76
|
+
)['relationship_types'].detect { |r| r['relationship1']['relationship_name'] == relationship1_name }
|
77
|
+
end
|
78
|
+
|
79
|
+
unless relationship_type
|
80
|
+
relationship_type = GlobalRegistry::RelationshipType.post(relationship_type: {
|
81
|
+
entity_type1_id: base_entity_type['id'],
|
82
|
+
entity_type2_id: related_entity_type['id'],
|
83
|
+
relationship1: relationship1_name,
|
84
|
+
relationship2: relationship2_name
|
85
|
+
})['relationship_type']
|
86
|
+
end
|
87
|
+
|
88
|
+
existing_fields = relationship_type['fields'].collect {|f| f['name']}
|
89
|
+
|
90
|
+
(columns_to_push).each do |field|
|
91
|
+
next if existing_fields.include?(field[:name])
|
92
|
+
|
93
|
+
GlobalRegistry::RelationshipType.put(relationship_type['id'], relationship_type: {
|
94
|
+
fields: [field]
|
95
|
+
})
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def columns_to_push
|
100
|
+
super
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
|
data/lib/cru_lib.rb
ADDED
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cru_lib
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Josh Starcher
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
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: Collection of common ruby logic used by a number of Cru apps
|
42
|
+
email:
|
43
|
+
- josh.starcher@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- cru_lib.gemspec
|
54
|
+
- lib/cru_lib.rb
|
55
|
+
- lib/cru_lib/async.rb
|
56
|
+
- lib/cru_lib/global_registry_methods.rb
|
57
|
+
- lib/cru_lib/global_registry_relationship_methods.rb
|
58
|
+
- lib/cru_lib/version.rb
|
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.2.1
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Misc libraries for Cru
|
83
|
+
test_files: []
|