object_identifier 0.0.5 → 0.0.6
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 +4 -4
- data/lib/object_identifier/deidentifier.rb +78 -0
- data/lib/object_identifier/identifier.rb +2 -2
- data/lib/object_identifier/version.rb +1 -1
- data/lib/object_identifier.rb +1 -0
- data/test/deidentifier_test.rb +28 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89be2d486507ddbd5182f7fb39bcfcd9afaa6a18
|
4
|
+
data.tar.gz: 9b6da56732eb50db9e65ae9f70364d4a64173650
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6649983366ea699aa1f1ae45645b22cb3304a3f8f6a561f73e15a330a73222b084cc664f2cbb43790e6d0fd09b36b93eeaebb843a0a966f60d6ec717c208cca
|
7
|
+
data.tar.gz: 8f250fed936c1c3f2f22473832af284c389f5ce013a0de36203739df57200e779b04d2cdd8bceed85606faa8963e705e6b8c32f969ec6d85c9d9336eac2cf79a
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module ObjectIdentifier
|
2
|
+
class Deidentifier
|
3
|
+
attr_accessor :str, :options
|
4
|
+
|
5
|
+
def initialize(str, options = {})
|
6
|
+
@str = str.to_s
|
7
|
+
@options = options
|
8
|
+
end
|
9
|
+
|
10
|
+
# Class method for reconstructing an object or set of objects from its/their
|
11
|
+
# self-identifying string(s).
|
12
|
+
#
|
13
|
+
# @overload self.identify(str)
|
14
|
+
# @param str [String] the String to deidentify
|
15
|
+
# @overload self.identify(str, options)
|
16
|
+
# @param str [String] the object to identify
|
17
|
+
# @param [Hash] options the options for building a customized
|
18
|
+
# self-identifier
|
19
|
+
# @option options [String, nil] :klass object class name override
|
20
|
+
# @option options [Fixnum] :limit maximum number of objects to display
|
21
|
+
# from a collection
|
22
|
+
#
|
23
|
+
# @return [Object] the object represented by the identifier String
|
24
|
+
def self.deidentify(obj, options = {})
|
25
|
+
new(obj, options).deidentify
|
26
|
+
end
|
27
|
+
|
28
|
+
# @return [Object] the object represented by the identifier String
|
29
|
+
def deidentify
|
30
|
+
if multiple_objects_to_deidentify?
|
31
|
+
# deidentify_multiple_objects
|
32
|
+
else
|
33
|
+
deidentify_single_object(str)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def deidentify_multiple_objects
|
40
|
+
end
|
41
|
+
|
42
|
+
def deidentify_single_object(object_str)
|
43
|
+
klass = determine_class(object_str)
|
44
|
+
attributes = extract_attributes(object_str)
|
45
|
+
|
46
|
+
case (model = klass.constantize)
|
47
|
+
when ActiveRecord::Base
|
48
|
+
model.where(select_core_attributes(attributes)).first
|
49
|
+
else
|
50
|
+
model.new(attributes)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def multiple_objects_to_deidentify?
|
55
|
+
str.starts_with?("[") && str.ends_with?("]")
|
56
|
+
end
|
57
|
+
|
58
|
+
def determine_class(object_str)
|
59
|
+
options.fetch(:klass) {
|
60
|
+
object_str[/\A(\w+)/, 1]
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
64
|
+
def extract_attributes(object_str)
|
65
|
+
result = object_str[/\[(.+)\]/, 1]
|
66
|
+
return if result == "no objects"
|
67
|
+
JSON.parse(jsonify_attributes_str(result))
|
68
|
+
end
|
69
|
+
|
70
|
+
def jsonify_attributes_str(attributes_str)
|
71
|
+
"{ #{attributes_str} }".gsub(/ (\w+):/, ' "\1":')
|
72
|
+
end
|
73
|
+
|
74
|
+
def select_core_attributes(attributes)
|
75
|
+
attributes.select { |key, _| key.in?(klass.column_names) }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -103,7 +103,7 @@ module ObjectIdentifier
|
|
103
103
|
format_empty(object)
|
104
104
|
else
|
105
105
|
attrs = @attributes.each_with_object({}) do |key, memo|
|
106
|
-
memo[key] = object.send(key) if object.respond_to?(key)
|
106
|
+
memo[key] = object.send(key) if object.respond_to?(key, :include_all)
|
107
107
|
end
|
108
108
|
"#{class_name_of(object)}[#{attribute_formatter(attrs)}]"
|
109
109
|
end
|
@@ -126,7 +126,7 @@ module ObjectIdentifier
|
|
126
126
|
end
|
127
127
|
|
128
128
|
def class_name_of(object)
|
129
|
-
@options.
|
129
|
+
@options.fetch(:klass) { object.class.name }
|
130
130
|
end
|
131
131
|
end
|
132
132
|
end
|
data/lib/object_identifier.rb
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
describe ObjectIdentifier::Deidentifier do
|
4
|
+
describe ".deidentify" do
|
5
|
+
it "returns the object, GIVEN just an :id attribute" do
|
6
|
+
subject = OpenStruct.new(id: 1)
|
7
|
+
result = ObjectIdentifier::Deidentifier.deidentify(subject.identify)
|
8
|
+
result.must_be_kind_of OpenStruct
|
9
|
+
result.must_equal subject
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#determine_class" do
|
14
|
+
it "returns the class string" do
|
15
|
+
subject = "TheClass[id: 1]"
|
16
|
+
result = ObjectIdentifier::Deidentifier.new("").send(:determine_class, subject)
|
17
|
+
result.must_equal "TheClass"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#extract_attributes" do
|
22
|
+
it "returns the class string" do
|
23
|
+
subject = "TheClass[id: 1]"
|
24
|
+
result = ObjectIdentifier::Deidentifier.new("").send(:extract_attributes, subject)
|
25
|
+
result.must_equal({ id: 1 }.stringify_keys)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: object_identifier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Dobbins
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2016-02-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -68,12 +68,14 @@ files:
|
|
68
68
|
- lib/object_identifier/core_ext/object.rb
|
69
69
|
- lib/object_identifier/core_ext/string.rb
|
70
70
|
- lib/object_identifier/core_ext/symbol.rb
|
71
|
+
- lib/object_identifier/deidentifier.rb
|
71
72
|
- lib/object_identifier/identifier.rb
|
72
73
|
- lib/object_identifier/version.rb
|
73
74
|
- test/core_ext/big_decimal_test.rb
|
74
75
|
- test/core_ext/object_test.rb
|
75
76
|
- test/core_ext/string_test.rb
|
76
77
|
- test/core_ext/symbol_test.rb
|
78
|
+
- test/deidentifier_test.rb
|
77
79
|
- test/dummy/README.rdoc
|
78
80
|
- test/dummy/Rakefile
|
79
81
|
- test/dummy/app/assets/javascripts/application.js
|
@@ -130,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
132
|
version: '0'
|
131
133
|
requirements: []
|
132
134
|
rubyforge_project:
|
133
|
-
rubygems_version: 2.4.
|
135
|
+
rubygems_version: 2.4.5
|
134
136
|
signing_key:
|
135
137
|
specification_version: 4
|
136
138
|
summary: Identify an object by inspecting its class name and attributes.
|
@@ -139,6 +141,7 @@ test_files:
|
|
139
141
|
- test/core_ext/object_test.rb
|
140
142
|
- test/core_ext/string_test.rb
|
141
143
|
- test/core_ext/symbol_test.rb
|
144
|
+
- test/deidentifier_test.rb
|
142
145
|
- test/dummy/app/assets/javascripts/application.js
|
143
146
|
- test/dummy/app/assets/stylesheets/application.css
|
144
147
|
- test/dummy/app/controllers/application_controller.rb
|