object_identifier 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +11 -0
  3. data/.travis.yml +21 -0
  4. data/CHANGELOG.md +10 -0
  5. data/Gemfile +6 -0
  6. data/Gemfile.lock +51 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +82 -48
  9. data/Rakefile +6 -28
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/lib/object_identifier/core_ext/big_decimal.rb +4 -2
  13. data/lib/object_identifier/core_ext/object.rb +6 -7
  14. data/lib/object_identifier/core_ext/string.rb +6 -6
  15. data/lib/object_identifier/core_ext/symbol.rb +2 -2
  16. data/lib/object_identifier/identifier.rb +59 -59
  17. data/lib/object_identifier/version.rb +1 -1
  18. data/lib/object_identifier.rb +4 -5
  19. data/object_identifier.gemspec +40 -0
  20. metadata +105 -109
  21. data/MIT-LICENSE +0 -20
  22. data/lib/object_identifier/deidentifier.rb +0 -78
  23. data/test/core_ext/big_decimal_test.rb +0 -12
  24. data/test/core_ext/object_test.rb +0 -11
  25. data/test/core_ext/string_test.rb +0 -9
  26. data/test/core_ext/symbol_test.rb +0 -9
  27. data/test/deidentifier_test.rb +0 -28
  28. data/test/dummy/README.rdoc +0 -28
  29. data/test/dummy/Rakefile +0 -6
  30. data/test/dummy/app/assets/javascripts/application.js +0 -13
  31. data/test/dummy/app/assets/stylesheets/application.css +0 -13
  32. data/test/dummy/app/controllers/application_controller.rb +0 -5
  33. data/test/dummy/app/helpers/application_helper.rb +0 -2
  34. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  35. data/test/dummy/bin/bundle +0 -3
  36. data/test/dummy/bin/rails +0 -4
  37. data/test/dummy/bin/rake +0 -4
  38. data/test/dummy/config/application.rb +0 -23
  39. data/test/dummy/config/boot.rb +0 -5
  40. data/test/dummy/config/database.yml +0 -25
  41. data/test/dummy/config/environment.rb +0 -5
  42. data/test/dummy/config/environments/development.rb +0 -29
  43. data/test/dummy/config/environments/production.rb +0 -80
  44. data/test/dummy/config/environments/test.rb +0 -36
  45. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  46. data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  47. data/test/dummy/config/initializers/inflections.rb +0 -16
  48. data/test/dummy/config/initializers/mime_types.rb +0 -5
  49. data/test/dummy/config/initializers/secret_token.rb +0 -12
  50. data/test/dummy/config/initializers/session_store.rb +0 -3
  51. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  52. data/test/dummy/config/locales/en.yml +0 -23
  53. data/test/dummy/config/routes.rb +0 -56
  54. data/test/dummy/config.ru +0 -4
  55. data/test/dummy/db/test.sqlite3 +0 -0
  56. data/test/dummy/log/development.log +0 -0
  57. data/test/dummy/log/test.log +0 -0
  58. data/test/dummy/public/404.html +0 -58
  59. data/test/dummy/public/422.html +0 -58
  60. data/test/dummy/public/500.html +0 -57
  61. data/test/dummy/public/favicon.ico +0 -0
  62. data/test/object_identifier_test.rb +0 -70
  63. data/test/test_helper.rb +0 -15
@@ -1,10 +1,6 @@
1
1
  module ObjectIdentifier
2
2
  class Identifier
3
- def initialize(objects, *args)
4
- @objects = Array.wrap(objects)
5
- @options = args.extract_options!
6
- @attributes = args.empty? ? [:id] : args
7
- end
3
+ NO_OBJECTS_INDICATOR = "[no objects]".freeze
8
4
 
9
5
  # Class method for constructing a self-identifying string for any given
10
6
  # object or collection of objects.
@@ -20,25 +16,33 @@ module ObjectIdentifier
20
16
  # @param [Hash] options the options for building a customized
21
17
  # self-identifier
22
18
  # @option options [String, nil] :klass object class name override
23
- # @option options [Fixnum] :limit maximum number of objects to display
19
+ # @option options [Integer] :limit maximum number of objects to display
24
20
  # from a collection
25
21
  #
26
22
  # @return [String] a self-identifying string like `Class[id:1, name:'temp']`
27
23
  #
28
24
  # @example
29
- # ObjectIdentifier::Identifier.identify(OpenStruct.new(a: 1, b: '2', c: :"3"), :a, :b, :c)
25
+ # ObjectIdentifier::Identifier.identify(
26
+ # OpenStruct.new(a: 1, b: '2', c: :"3"), :a, :b, :c)
30
27
  # # => "OpenStruct[a:1, b:\"2\", c::\"3\"]"
31
28
  #
32
- # ObjectIdentifier::Identifier.identify(1, :to_s) # => "Fixnum[to_s:\"1\"]"
33
- # ObjectIdentifier::Identifier.identify(nil) # => "[no objects]"
29
+ # ObjectIdentifier::Identifier.identify(1, :to_s) # => "Integer[to_s:\"1\"]"
30
+ # ObjectIdentifier::Identifier.identify(nil) # => "[no objects]"
34
31
  #
35
- # ObjectIdentifier::Identifier.identify(%w(1 2 3), :to_i, :to_f)
36
- # # => "String[to_i:1, to_f:1.0], String[to_i:2, to_f:2.0], String[to_i:3, to_f:3.0]"
32
+ # ObjectIdentifier::Identifier.identify(%w(1 2), :to_i, :to_f)
33
+ # # => "String[to_i:1, to_f:1.0], String[to_i:2, to_f:2.0]"
37
34
  #
38
35
  # ObjectIdentifier::Identifier.identify((1..10).to_a, :to_f, limit: 2)
39
- # # => "Fixnum[to_f:1.0], Fixnum[to_f:2.0], ... (8 more)"
40
- def self.identify(obj, *args)
41
- new(obj, *args).to_s
36
+ # # => "Integer[to_f:1.0], Integer[to_f:2.0], ... (8 more)"
37
+ def self.identify(object, *args)
38
+ new(object, *args).to_s
39
+ end
40
+
41
+ def initialize(objects, *args, limit: nil, klass: :not_given)
42
+ @objects = Array(objects)
43
+ @attributes = args.empty? ? [:id] : args
44
+ @limit = (limit || @objects.size).to_i
45
+ @klass = klass
42
46
  end
43
47
 
44
48
  # Output the self-identifying string for an instance of
@@ -48,85 +52,81 @@ module ObjectIdentifier
48
52
  #
49
53
  # @return [String] a string representing the object or list of objects
50
54
  def to_s
51
- if multiple_objects_to_identify?
55
+ if many?
52
56
  format_multiple_objects
53
57
  else
54
58
  format_single_object
55
59
  end
56
60
  end
57
61
 
58
- private
62
+ private
59
63
 
60
64
  def format_multiple_objects
61
- objects = @objects.first(limit).map do |obj|
62
- format_with_attributes(obj)
63
- end.join(", ")
65
+ objects =
66
+ @objects.first(@limit).map { |obj| format(obj) }
64
67
 
65
- if any_objects_abbreviated?
66
- objects << ", ... (#{number_of_abbreviated_objects} more)"
68
+ if truncated?
69
+ objects << "... (#{truncated_objects_count} more)"
67
70
  end
68
71
 
69
- objects
72
+ objects.join(", ")
70
73
  end
71
74
 
72
75
  def format_single_object
73
- obj = if nil_or_empty_array?(@objects)
74
- @objects
75
- else
76
- @objects.first
77
- end
78
- format_with_attributes(obj)
79
- end
76
+ object = @objects.first if @objects.respond_to?(:first)
80
77
 
81
- def multiple_objects_to_identify?
82
- @objects.many?
78
+ format(object)
83
79
  end
84
80
 
85
- def limit
86
- @options.fetch(:limit) { @objects.size }.to_i
81
+ def format(object)
82
+ return NO_OBJECTS_INDICATOR if blank?(object)
83
+
84
+ "#{class_name(object)}[#{format_attributes(evaluate_attributes(object))}]"
87
85
  end
88
86
 
89
- def limit_given?
90
- @options.key?(:limit)
87
+ def format_attributes(attributes_hash)
88
+ return if attributes_hash.empty?
89
+
90
+ attributes_hash.map { |(key, value)|
91
+ "#{key}:#{value.inspect_lit}"
92
+ }.join(", ")
91
93
  end
92
94
 
93
- def any_objects_abbreviated?
94
- limit_given? && number_of_abbreviated_objects > 0
95
+ # @return [Hash]
96
+ def evaluate_attributes(object)
97
+ @attributes.each_with_object({}) { |key, acc|
98
+ if object.respond_to?(key, :include_private)
99
+ acc[key] = object.send(key)
100
+ end
101
+ }
95
102
  end
96
103
 
97
- def number_of_abbreviated_objects
98
- @objects.size - @options[:limit].to_i
104
+ def class_name(object)
105
+ klass_given? ? @klass : object.class.name
99
106
  end
100
107
 
101
- def format_with_attributes(object)
102
- if nil_or_empty_array?(object)
103
- format_empty(object)
104
- else
105
- attrs = @attributes.each_with_object({}) do |key, memo|
106
- memo[key] = object.send(key) if object.respond_to?(key, :include_all)
107
- end
108
- "#{class_name_of(object)}[#{attribute_formatter(attrs)}]"
109
- end
108
+ def truncated_objects_count
109
+ objects_count - @limit
110
110
  end
111
111
 
112
- def format_empty(object)
113
- @options.key?(:klass) ? "#{@options[:klass]}[]" : "[no objects]"
112
+ def objects_count
113
+ @objects_count ||= @objects.size
114
114
  end
115
115
 
116
- def attribute_formatter(hash)
117
- return if hash.empty?
116
+ def klass_given?
117
+ @klass != :not_given
118
+ end
118
119
 
119
- hash.inject([]) do |memo, (key, value)|
120
- memo << "#{key}:#{value.inspect_lit}"
121
- end.join(", ")
120
+ def blank?(object)
121
+ object.nil? || object == [] || object == {}
122
122
  end
123
123
 
124
- def nil_or_empty_array?(object)
125
- object.nil? || object == []
124
+ def many?
125
+ objects_count > 1
126
126
  end
127
127
 
128
- def class_name_of(object)
129
- @options.fetch(:klass) { object.class.name }
128
+ def truncated?
129
+ truncated_objects_count > 0
130
130
  end
131
131
  end
132
132
  end
@@ -1,3 +1,3 @@
1
1
  module ObjectIdentifier
2
- VERSION = "0.0.6"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,9 +1,8 @@
1
+ module ObjectIdentifier
2
+ end
3
+
4
+ require "object_identifier/identifier"
1
5
  require "object_identifier/core_ext/object"
2
6
  require "object_identifier/core_ext/string"
3
7
  require "object_identifier/core_ext/symbol"
4
8
  require "object_identifier/core_ext/big_decimal"
5
-
6
- module ObjectIdentifier
7
- autoload :Identifier, "object_identifier/identifier"
8
- autoload :Deidentifier, "object_identifier/deidentifier"
9
- end
@@ -0,0 +1,40 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "object_identifier/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "object_identifier"
7
+ spec.version = ObjectIdentifier::VERSION
8
+ spec.authors = ["Paul Dobbins", "Evan Sherwood"]
9
+ spec.email = ["paul.dobbins@icloud.com"]
10
+
11
+ spec.summary = %q{ObjectIdentifier identifies an object by its class name and attributes.}
12
+ spec.description = %q{Object Identifier allows quick, easy, and uniform identification of an object by inspecting its class name and outputting any desirable attributes/methods. It is great for quickly logging, sending descriptive notification messages, etc.}
13
+ spec.homepage = "https://github.com/pdobb/object_identifier"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against " \
22
+ # "public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.16"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "minitest", "~> 5.0"
35
+ spec.add_development_dependency "minitest-reporters", "~> 1.2"
36
+ spec.add_development_dependency "simplecov", "~> 0.16"
37
+ spec.add_development_dependency "byebug", "~> 10.0"
38
+ spec.add_development_dependency "pry", "~> 0.11"
39
+ spec.add_development_dependency "pry-byebug", "~> 3.6"
40
+ end
metadata CHANGED
@@ -1,117 +1,155 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object_identifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dobbins
8
8
  - Evan Sherwood
9
9
  autorequire:
10
- bindir: bin
10
+ bindir: exe
11
11
  cert_chain: []
12
- date: 2016-02-06 00:00:00.000000000 Z
12
+ date: 2018-04-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rails
15
+ name: bundler
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ">="
18
+ - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: 3.0.0
21
- type: :runtime
20
+ version: '1.16'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.16'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: minitest
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '5.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '5.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: minitest-reporters
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '1.2'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.2'
70
+ - !ruby/object:Gem::Dependency
71
+ name: simplecov
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '0.16'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '0.16'
84
+ - !ruby/object:Gem::Dependency
85
+ name: byebug
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '10.0'
91
+ type: :development
22
92
  prerelease: false
23
93
  version_requirements: !ruby/object:Gem::Requirement
24
94
  requirements:
25
- - - ">="
95
+ - - "~>"
26
96
  - !ruby/object:Gem::Version
27
- version: 3.0.0
97
+ version: '10.0'
28
98
  - !ruby/object:Gem::Dependency
29
- name: sqlite3
99
+ name: pry
30
100
  requirement: !ruby/object:Gem::Requirement
31
101
  requirements:
32
- - - ">="
102
+ - - "~>"
33
103
  - !ruby/object:Gem::Version
34
- version: '0'
104
+ version: '0.11'
35
105
  type: :development
36
106
  prerelease: false
37
107
  version_requirements: !ruby/object:Gem::Requirement
38
108
  requirements:
39
- - - ">="
109
+ - - "~>"
40
110
  - !ruby/object:Gem::Version
41
- version: '0'
111
+ version: '0.11'
42
112
  - !ruby/object:Gem::Dependency
43
- name: minitest-rails
113
+ name: pry-byebug
44
114
  requirement: !ruby/object:Gem::Requirement
45
115
  requirements:
46
- - - ">="
116
+ - - "~>"
47
117
  - !ruby/object:Gem::Version
48
- version: '0'
118
+ version: '3.6'
49
119
  type: :development
50
120
  prerelease: false
51
121
  version_requirements: !ruby/object:Gem::Requirement
52
122
  requirements:
53
- - - ">="
123
+ - - "~>"
54
124
  - !ruby/object:Gem::Version
55
- version: '0'
56
- description:
125
+ version: '3.6'
126
+ description: Object Identifier allows quick, easy, and uniform identification of an
127
+ object by inspecting its class name and outputting any desirable attributes/methods.
128
+ It is great for quickly logging, sending descriptive notification messages, etc.
57
129
  email:
58
- - pdobbins@gmail.com
130
+ - paul.dobbins@icloud.com
59
131
  executables: []
60
132
  extensions: []
61
133
  extra_rdoc_files: []
62
134
  files:
63
- - MIT-LICENSE
135
+ - ".gitignore"
136
+ - ".travis.yml"
137
+ - CHANGELOG.md
138
+ - Gemfile
139
+ - Gemfile.lock
140
+ - LICENSE.txt
64
141
  - README.md
65
142
  - Rakefile
143
+ - bin/console
144
+ - bin/setup
66
145
  - lib/object_identifier.rb
67
146
  - lib/object_identifier/core_ext/big_decimal.rb
68
147
  - lib/object_identifier/core_ext/object.rb
69
148
  - lib/object_identifier/core_ext/string.rb
70
149
  - lib/object_identifier/core_ext/symbol.rb
71
- - lib/object_identifier/deidentifier.rb
72
150
  - lib/object_identifier/identifier.rb
73
151
  - lib/object_identifier/version.rb
74
- - test/core_ext/big_decimal_test.rb
75
- - test/core_ext/object_test.rb
76
- - test/core_ext/string_test.rb
77
- - test/core_ext/symbol_test.rb
78
- - test/deidentifier_test.rb
79
- - test/dummy/README.rdoc
80
- - test/dummy/Rakefile
81
- - test/dummy/app/assets/javascripts/application.js
82
- - test/dummy/app/assets/stylesheets/application.css
83
- - test/dummy/app/controllers/application_controller.rb
84
- - test/dummy/app/helpers/application_helper.rb
85
- - test/dummy/app/views/layouts/application.html.erb
86
- - test/dummy/bin/bundle
87
- - test/dummy/bin/rails
88
- - test/dummy/bin/rake
89
- - test/dummy/config.ru
90
- - test/dummy/config/application.rb
91
- - test/dummy/config/boot.rb
92
- - test/dummy/config/database.yml
93
- - test/dummy/config/environment.rb
94
- - test/dummy/config/environments/development.rb
95
- - test/dummy/config/environments/production.rb
96
- - test/dummy/config/environments/test.rb
97
- - test/dummy/config/initializers/backtrace_silencers.rb
98
- - test/dummy/config/initializers/filter_parameter_logging.rb
99
- - test/dummy/config/initializers/inflections.rb
100
- - test/dummy/config/initializers/mime_types.rb
101
- - test/dummy/config/initializers/secret_token.rb
102
- - test/dummy/config/initializers/session_store.rb
103
- - test/dummy/config/initializers/wrap_parameters.rb
104
- - test/dummy/config/locales/en.yml
105
- - test/dummy/config/routes.rb
106
- - test/dummy/db/test.sqlite3
107
- - test/dummy/log/development.log
108
- - test/dummy/log/test.log
109
- - test/dummy/public/404.html
110
- - test/dummy/public/422.html
111
- - test/dummy/public/500.html
112
- - test/dummy/public/favicon.ico
113
- - test/object_identifier_test.rb
114
- - test/test_helper.rb
152
+ - object_identifier.gemspec
115
153
  homepage: https://github.com/pdobb/object_identifier
116
154
  licenses:
117
155
  - MIT
@@ -132,50 +170,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
170
  version: '0'
133
171
  requirements: []
134
172
  rubyforge_project:
135
- rubygems_version: 2.4.5
173
+ rubygems_version: 2.7.6
136
174
  signing_key:
137
175
  specification_version: 4
138
- summary: Identify an object by inspecting its class name and attributes.
139
- test_files:
140
- - test/core_ext/big_decimal_test.rb
141
- - test/core_ext/object_test.rb
142
- - test/core_ext/string_test.rb
143
- - test/core_ext/symbol_test.rb
144
- - test/deidentifier_test.rb
145
- - test/dummy/app/assets/javascripts/application.js
146
- - test/dummy/app/assets/stylesheets/application.css
147
- - test/dummy/app/controllers/application_controller.rb
148
- - test/dummy/app/helpers/application_helper.rb
149
- - test/dummy/app/views/layouts/application.html.erb
150
- - test/dummy/bin/bundle
151
- - test/dummy/bin/rails
152
- - test/dummy/bin/rake
153
- - test/dummy/config/application.rb
154
- - test/dummy/config/boot.rb
155
- - test/dummy/config/database.yml
156
- - test/dummy/config/environment.rb
157
- - test/dummy/config/environments/development.rb
158
- - test/dummy/config/environments/production.rb
159
- - test/dummy/config/environments/test.rb
160
- - test/dummy/config/initializers/backtrace_silencers.rb
161
- - test/dummy/config/initializers/filter_parameter_logging.rb
162
- - test/dummy/config/initializers/inflections.rb
163
- - test/dummy/config/initializers/mime_types.rb
164
- - test/dummy/config/initializers/secret_token.rb
165
- - test/dummy/config/initializers/session_store.rb
166
- - test/dummy/config/initializers/wrap_parameters.rb
167
- - test/dummy/config/locales/en.yml
168
- - test/dummy/config/routes.rb
169
- - test/dummy/config.ru
170
- - test/dummy/db/test.sqlite3
171
- - test/dummy/log/development.log
172
- - test/dummy/log/test.log
173
- - test/dummy/public/404.html
174
- - test/dummy/public/422.html
175
- - test/dummy/public/500.html
176
- - test/dummy/public/favicon.ico
177
- - test/dummy/Rakefile
178
- - test/dummy/README.rdoc
179
- - test/object_identifier_test.rb
180
- - test/test_helper.rb
181
- has_rdoc:
176
+ summary: ObjectIdentifier identifies an object by its class name and attributes.
177
+ test_files: []
data/MIT-LICENSE DELETED
@@ -1,20 +0,0 @@
1
- Copyright 2013 Paul Dobbins
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.
@@ -1,78 +0,0 @@
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
@@ -1,12 +0,0 @@
1
- require "test_helper"
2
-
3
- describe BigDecimal do
4
- describe "#inspect_lit" do
5
- it "returns the same as #to_s" do
6
- [1, 1.0, 0, 99.9999].each do |val|
7
- bd_val = BigDecimal.new(val, 10)
8
- bd_val.inspect_lit.must_equal "<BD:#{bd_val}>"
9
- end
10
- end
11
- end
12
- end
@@ -1,11 +0,0 @@
1
- require "test_helper"
2
-
3
- describe Object do
4
- it "responds to #inspect_lit" do
5
- assert Object.new.respond_to?(:inspect_lit)
6
- end
7
-
8
- it "responds to #identify" do
9
- assert Object.new.respond_to?(:identify)
10
- end
11
- end
@@ -1,9 +0,0 @@
1
- require "test_helper"
2
-
3
- describe String do
4
- describe "#inspect_lit" do
5
- it "quotes string values" do
6
- "string".inspect_lit.must_equal %("string")
7
- end
8
- end
9
- end
@@ -1,9 +0,0 @@
1
- require "test_helper"
2
-
3
- describe Symbol do
4
- describe "#inspect_lit" do
5
- it "quotes symbol values after colon" do
6
- :symbol.inspect_lit.must_equal %(:"symbol")
7
- end
8
- end
9
- end