cru_lib 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/cru_lib.gemspec +1 -0
- data/lib/cru_lib/global_registry_methods.rb +27 -10
- data/lib/cru_lib/global_registry_relationship_methods.rb +4 -3
- data/lib/cru_lib/version.rb +1 -1
- data/spec/shared_examples_for_global_registry_models.rb +28 -0
- metadata +27 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d936cc7dc3e60b9974dc18f658c65105ace08cd6
|
4
|
+
data.tar.gz: 908ce243da74e7f5ca825079e5c13574f9a485d4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 905a67249419b9ff92a0badc0b27f868fce3de6d65c40f625092dd650adf1d42e06d4ee37db163293868a89aad208a90689678129db5f6b6aee8ab655fb576c0
|
7
|
+
data.tar.gz: f9b90db5152e55630b8d7cdac145a33236043b8622c0e54b08818e1b8b7979914297febfce977744cf20f818ca23d5545f2cd797761283ce384689b6f0bd65a7
|
data/.gitignore
CHANGED
data/cru_lib.gemspec
CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
|
+
spec.add_dependency "global_registry"
|
20
21
|
|
21
22
|
spec.add_development_dependency "bundler", "~> 1.6"
|
22
23
|
spec.add_development_dependency "rake"
|
@@ -23,17 +23,18 @@ module CruLib
|
|
23
23
|
async(:async_push_to_global_registry)
|
24
24
|
end
|
25
25
|
|
26
|
-
def async_push_to_global_registry(parent_id = nil, parent_type = nil)
|
26
|
+
def async_push_to_global_registry(parent_id = nil, parent_type = nil, parent = nil)
|
27
27
|
self.class.push_structure_to_global_registry
|
28
28
|
|
29
29
|
if global_registry_id
|
30
30
|
begin
|
31
|
-
update_in_global_registry(parent_id, parent_type)
|
31
|
+
update_in_global_registry(parent_id, parent_type, parent)
|
32
32
|
rescue RestClient::ResourceNotFound
|
33
|
-
|
33
|
+
self.global_registry_id = nil
|
34
|
+
async_push_to_global_registry
|
34
35
|
end
|
35
36
|
else
|
36
|
-
create_in_global_registry(parent_id, parent_type)
|
37
|
+
create_in_global_registry(parent_id, parent_type, parent)
|
37
38
|
end
|
38
39
|
end
|
39
40
|
|
@@ -42,24 +43,24 @@ module CruLib
|
|
42
43
|
@attributes_to_push = {}
|
43
44
|
attributes_to_push['client_integration_id'] = id unless self.class.skip_fields_for_gr.include?('client_integration_id')
|
44
45
|
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.collect {|k, v| @attributes_to_push[k.underscore] = self.class.gr_value(k.underscore, v)}
|
46
47
|
@attributes_to_push.select! {|k, v| v.present? && !self.class.skip_fields_for_gr.include?(k)}
|
47
48
|
end
|
48
49
|
@attributes_to_push
|
49
50
|
end
|
50
51
|
|
51
|
-
def update_in_global_registry(parent_id = nil, parent_type = nil)
|
52
|
+
def update_in_global_registry(parent_id = nil, parent_type = nil, parent = nil)
|
52
53
|
if parent_type
|
53
|
-
create_in_global_registry(parent_id, parent_type)
|
54
|
+
create_in_global_registry(parent_id, parent_type, parent)
|
54
55
|
else
|
55
56
|
GlobalRegistry::Entity.put(global_registry_id, {entity: attributes_to_push})
|
56
57
|
end
|
57
58
|
end
|
58
59
|
|
59
|
-
def create_in_global_registry(parent_id = nil, parent_type = nil)
|
60
|
+
def create_in_global_registry(parent_id = nil, parent_type = nil, parent = nil)
|
60
61
|
entity_attributes = { self.class.global_registry_entity_type_name => attributes_to_push }
|
61
62
|
if parent_type.present?
|
62
|
-
entity_attributes = {parent_type => entity_attributes}
|
63
|
+
entity_attributes = {parent_type => entity_attributes.merge(client_integration_id: parent.id)}
|
63
64
|
GlobalRegistry::Entity.put(parent_id, {entity: entity_attributes})
|
64
65
|
else
|
65
66
|
entity = GlobalRegistry::Entity.post(entity: entity_attributes)
|
@@ -69,6 +70,22 @@ module CruLib
|
|
69
70
|
end
|
70
71
|
|
71
72
|
module ClassMethods
|
73
|
+
def gr_value(column_name, value)
|
74
|
+
return value if value.blank?
|
75
|
+
|
76
|
+
column = columns_to_push.detect { |c| c[:name] == column_name }
|
77
|
+
return unless column
|
78
|
+
|
79
|
+
case column[:field_type].to_s
|
80
|
+
when 'datetime', 'date'
|
81
|
+
value.to_s(:db)
|
82
|
+
when 'boolean'
|
83
|
+
value ? 'true' : 'false'
|
84
|
+
else
|
85
|
+
value.to_s.strip
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
72
89
|
def push_structure_to_global_registry(parent_id = nil)
|
73
90
|
# Make sure all columns exist
|
74
91
|
entity_type = Rails.cache.fetch(global_registry_entity_type_name, expires_in: 1.hour) do
|
@@ -94,7 +111,7 @@ module CruLib
|
|
94
111
|
@columns_to_push ||= columns.select { |c|
|
95
112
|
!skip_fields_for_gr.include?(c.name.underscore)
|
96
113
|
}.collect {|c|
|
97
|
-
{ name: c.name.underscore,
|
114
|
+
{ name: c.name.underscore, field_type: normalize_column_type(c.type, c.name.underscore) }
|
98
115
|
}
|
99
116
|
end
|
100
117
|
|
@@ -17,7 +17,7 @@ module CruLib
|
|
17
17
|
# @param [String] relationship_name
|
18
18
|
# @param [String] related_name
|
19
19
|
# @param [Object] related_object
|
20
|
-
def attributes_to_push(relationship_name
|
20
|
+
def attributes_to_push(relationship_name: nil, related_name: nil, related_object: nil, base_object: nil)
|
21
21
|
if global_registry_id
|
22
22
|
attributes_to_push = super
|
23
23
|
attributes_to_push
|
@@ -26,7 +26,8 @@ module CruLib
|
|
26
26
|
"#{relationship_name}:relationship" => {
|
27
27
|
client_integration_id: id,
|
28
28
|
related_name => related_object.global_registry_id
|
29
|
-
}
|
29
|
+
},
|
30
|
+
client_integration_id: base_object.id
|
30
31
|
}
|
31
32
|
end
|
32
33
|
end
|
@@ -36,7 +37,7 @@ module CruLib
|
|
36
37
|
def create_in_global_registry(base_object, relationship_name)
|
37
38
|
entity = GlobalRegistry::Entity.put(
|
38
39
|
base_object.global_registry_id,
|
39
|
-
entity: {base_object.class.global_registry_entity_type_name => attributes_to_push}
|
40
|
+
entity: { base_object.class.global_registry_entity_type_name => attributes_to_push(base_object: base_object) }
|
40
41
|
)
|
41
42
|
|
42
43
|
base_object_id = entity['entity'][base_object.class.global_registry_entity_type_name]['id']
|
data/lib/cru_lib/version.rb
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
# To include some tests for global_registry_methods automatically, copy this file to
|
2
|
+
# spec/support/shared_examples_for_global_registry_models.rb in the enclosing app then
|
3
|
+
# add include_examples "global_registry_methods" to the enclosing spec
|
4
|
+
#
|
5
|
+
# for example:
|
6
|
+
#
|
7
|
+
# RSpec.describe Address, :type => :model do
|
8
|
+
# include_examples "global_registry_methods"
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
# there should be some lines in your spec_helper.rb like this:
|
12
|
+
#
|
13
|
+
# # Requires supporting ruby files with custom matchers and macros, etc,
|
14
|
+
# # in spec/support/ and its subdirectories.
|
15
|
+
# Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
|
16
|
+
#
|
17
|
+
# that will automatically include the file once it's copied to spec/support
|
18
|
+
#
|
19
|
+
shared_examples "global_registry_methods" do
|
20
|
+
context "self.skip_fields_for_gr" do
|
21
|
+
it "should run" do
|
22
|
+
expect(described_class.skip_fields_for_gr.class).to eq(Array), "Expected #{described_class}.skip_fields_for_gr to return an Array"
|
23
|
+
expect(described_class.skip_fields_for_gr.collect(&:class).uniq).to eq([String]), "Expected #{described_class}.skip_fields_for_gr to return an Array of Strings"
|
24
|
+
expect(described_class.skip_fields_for_gr.uniq).to eq(described_class.skip_fields_for_gr), "Expected #{described_class}.skip_fields_for_gr to return an Array with no duplicates (#{described_class.skip_fields_for_gr.find_all{ |c| described_class.skip_fields_for_gr.count(c) > 1 }.uniq} are all in the array at least twice)"
|
25
|
+
expect(described_class.skip_fields_for_gr - described_class.column_names - described_class.attribute_aliases.keys).to be_empty, "Expected #{described_class}.skip_fields_for_gr to return an Array of Strings where each string is a column or attribute alias (#{described_class.skip_fields_for_gr - described_class.column_names - described_class.attribute_aliases.keys} are not columns or attribute aliases)"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
CHANGED
@@ -1,41 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cru_lib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Starcher
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-09-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: global_registry
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
|
-
- - ~>
|
31
|
+
- - "~>"
|
18
32
|
- !ruby/object:Gem::Version
|
19
33
|
version: '1.6'
|
20
34
|
type: :development
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
|
-
- - ~>
|
38
|
+
- - "~>"
|
25
39
|
- !ruby/object:Gem::Version
|
26
40
|
version: '1.6'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
|
-
- -
|
45
|
+
- - ">="
|
32
46
|
- !ruby/object:Gem::Version
|
33
47
|
version: '0'
|
34
48
|
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
|
-
- -
|
52
|
+
- - ">="
|
39
53
|
- !ruby/object:Gem::Version
|
40
54
|
version: '0'
|
41
55
|
description: Collection of common ruby logic used by a number of Cru apps
|
@@ -45,7 +59,7 @@ executables: []
|
|
45
59
|
extensions: []
|
46
60
|
extra_rdoc_files: []
|
47
61
|
files:
|
48
|
-
- .gitignore
|
62
|
+
- ".gitignore"
|
49
63
|
- Gemfile
|
50
64
|
- LICENSE.txt
|
51
65
|
- README.md
|
@@ -56,6 +70,7 @@ files:
|
|
56
70
|
- lib/cru_lib/global_registry_methods.rb
|
57
71
|
- lib/cru_lib/global_registry_relationship_methods.rb
|
58
72
|
- lib/cru_lib/version.rb
|
73
|
+
- spec/shared_examples_for_global_registry_models.rb
|
59
74
|
homepage: ''
|
60
75
|
licenses:
|
61
76
|
- MIT
|
@@ -66,18 +81,19 @@ require_paths:
|
|
66
81
|
- lib
|
67
82
|
required_ruby_version: !ruby/object:Gem::Requirement
|
68
83
|
requirements:
|
69
|
-
- -
|
84
|
+
- - ">="
|
70
85
|
- !ruby/object:Gem::Version
|
71
86
|
version: '0'
|
72
87
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
88
|
requirements:
|
74
|
-
- -
|
89
|
+
- - ">="
|
75
90
|
- !ruby/object:Gem::Version
|
76
91
|
version: '0'
|
77
92
|
requirements: []
|
78
93
|
rubyforge_project:
|
79
|
-
rubygems_version: 2.
|
94
|
+
rubygems_version: 2.4.5
|
80
95
|
signing_key:
|
81
96
|
specification_version: 4
|
82
97
|
summary: Misc libraries for Cru
|
83
|
-
test_files:
|
98
|
+
test_files:
|
99
|
+
- spec/shared_examples_for_global_registry_models.rb
|