simple_rewriter 0.0.2

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.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +57 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +25 -0
  5. data/Gemfile +5 -0
  6. data/Gemfile.lock +70 -0
  7. data/LICENSE.md +8 -0
  8. data/README.md +115 -0
  9. data/Rakefile +4 -0
  10. data/bin/pry +29 -0
  11. data/bin/rake +29 -0
  12. data/bin/rspec +29 -0
  13. data/bin/rubocop +29 -0
  14. data/examples/DB_ANONYMIZER.md +57 -0
  15. data/examples/db_anonymizer/mappings.rb +13 -0
  16. data/examples/db_anonymizer.rb +52 -0
  17. data/lib/simple_rewriter/attribute.rb +24 -0
  18. data/lib/simple_rewriter/attribute_resolver.rb +51 -0
  19. data/lib/simple_rewriter/base_service.rb +5 -0
  20. data/lib/simple_rewriter/class_resolver.rb +68 -0
  21. data/lib/simple_rewriter/configuration.rb +15 -0
  22. data/lib/simple_rewriter/reader.rb +48 -0
  23. data/lib/simple_rewriter/readers/active_record_reader.rb +11 -0
  24. data/lib/simple_rewriter/readers/base_reader.rb +21 -0
  25. data/lib/simple_rewriter/readers/hash_reader.rb +11 -0
  26. data/lib/simple_rewriter/readers/open_struct_reader.rb +6 -0
  27. data/lib/simple_rewriter/readers.rb +6 -0
  28. data/lib/simple_rewriter/rewriter.rb +57 -0
  29. data/lib/simple_rewriter/rewriters/active_support/time_with_zone_rewriter.rb +2 -0
  30. data/lib/simple_rewriter/rewriters/active_support.rb +3 -0
  31. data/lib/simple_rewriter/rewriters/array_rewriter.rb +18 -0
  32. data/lib/simple_rewriter/rewriters/base.rb +42 -0
  33. data/lib/simple_rewriter/rewriters/date_rewriter.rb +11 -0
  34. data/lib/simple_rewriter/rewriters/float_rewriter.rb +14 -0
  35. data/lib/simple_rewriter/rewriters/generators.rb +69 -0
  36. data/lib/simple_rewriter/rewriters/hash_rewriter.rb +36 -0
  37. data/lib/simple_rewriter/rewriters/integer_rewriter.rb +7 -0
  38. data/lib/simple_rewriter/rewriters/options_resolver.rb +21 -0
  39. data/lib/simple_rewriter/rewriters/string_rewriter.rb +18 -0
  40. data/lib/simple_rewriter/rewriters.rb +12 -0
  41. data/lib/simple_rewriter/type_detector.rb +41 -0
  42. data/lib/simple_rewriter/version.rb +3 -0
  43. data/lib/simple_rewriter/writer.rb +30 -0
  44. data/lib/simple_rewriter.rb +31 -0
  45. data/simple_rewriter.gemspec +24 -0
  46. data/spec/simple_rewriter/attribute_spec.rb +16 -0
  47. data/spec/simple_rewriter/rewriters/hash_rewriter_spec.rb +25 -0
  48. data/spec/simple_rewriter/rewriters/string_rewriter_spec.rb +62 -0
  49. data/spec/simple_rewriter/type_detector_spec.rb +23 -0
  50. data/spec/simple_rewriter_spec.rb +102 -0
  51. data/spec/spec_helper.rb +104 -0
  52. data/spec/support/simple_rewriter/base_checkers.rb +12 -0
  53. metadata +180 -0
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ describe SimpleRewriter::Rewriters::HashRewriter do
4
+ subject { described_class.new(value, options).generate }
5
+ let(:value) { { example: "example" } }
6
+
7
+ describe "#generate" do
8
+ context "without options" do
9
+ let(:options) { {} }
10
+
11
+ it "generates new hash" do
12
+ check_base_expectations(Hash)
13
+ end
14
+ end
15
+
16
+ context "with address option" do
17
+ let(:options) { { address: true } }
18
+
19
+ it "generates new hash" do
20
+ check_address_expectations
21
+ check_base_expectations(Hash)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,62 @@
1
+ require "spec_helper"
2
+
3
+ describe SimpleRewriter::Rewriters::StringRewriter do
4
+ subject { described_class.new(value, options).generate }
5
+ let(:email_regex) { /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i }
6
+ let(:value) { "example" }
7
+
8
+ describe "#generate" do
9
+ context "without options" do
10
+ let(:options) { {} }
11
+
12
+ it("generates new value") { check_base_expectations(String) }
13
+ end
14
+
15
+ context "with email option" do
16
+ let(:options) { { email: true } }
17
+
18
+ it "generates new value" do
19
+ check_base_expectations(String)
20
+ expect(subject).to match(email_regex)
21
+ end
22
+ end
23
+
24
+ context "with address option" do
25
+ let(:options) { { address: true } }
26
+
27
+ it "generates new value" do
28
+ check_address_expectations
29
+ check_base_expectations(String)
30
+ end
31
+ end
32
+
33
+ context "with hex option" do
34
+ let(:options) { { hex: true } }
35
+
36
+ it "generates new value" do
37
+ expect(SecureRandom).to receive(:hex).and_call_original
38
+ check_base_expectations(String)
39
+ end
40
+ end
41
+
42
+ context "with name option" do
43
+ let(:options) { { name: true } }
44
+
45
+ it "generates new value" do
46
+ expect(Faker::Name).to receive(:name).and_call_original
47
+ check_base_expectations(String)
48
+ end
49
+ end
50
+
51
+ context "with name option" do
52
+ let(:options) { { sample_pdf: true } }
53
+
54
+ it "generates new value" do
55
+ check_base_expectations(String)
56
+ expect do
57
+ URI.parse(subject)
58
+ end.not_to raise_error
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+
3
+ describe SimpleRewriter::TypeDetector do
4
+ subject { described_class.call(value) }
5
+
6
+ context "when value is numeric" do
7
+ let(:value) { "123" }
8
+
9
+ it { expect(subject).to be(Integer) }
10
+ end
11
+
12
+ context "when value is float" do
13
+ let(:value) { "0.1" }
14
+
15
+ it { expect(subject).to be(Float) }
16
+ end
17
+
18
+ context "when value is date" do
19
+ let(:value) { "12.12.2000" }
20
+
21
+ it { expect(subject).to be(Date) }
22
+ end
23
+ end
@@ -0,0 +1,102 @@
1
+ require "spec_helper"
2
+
3
+ describe SimpleRewriter do
4
+ let(:record) { OpenStruct.new(attribute: attribute, "attribute=": true) }
5
+ let(:email_regex) { /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i }
6
+
7
+ %i(email address hex name first_name last_name phonenumber zip street city date sample_pdf).each do |option|
8
+ context "with option #{option}" do
9
+ let(:attribute) { "" }
10
+ let(:attributes) { { attribute: [option] } }
11
+
12
+ subject { described_class.call(record: record, attributes: [attributes]) }
13
+
14
+ it "generates output" do
15
+ expect(subject.record.attribute).not_to be_empty
16
+ expect(subject.success?).to eq(true)
17
+ end
18
+ end
19
+ end
20
+
21
+ [1, {}, rand].each do |attr|
22
+ context "rewrites #{attr.class}" do
23
+ let(:attribute) { attr }
24
+
25
+ subject { described_class.call(record: record, attributes: [:attribute]) }
26
+
27
+ it do
28
+ expect(record).to receive(:attribute).at_least(:once).and_call_original
29
+
30
+ expect(subject.success?).to eq(true)
31
+ end
32
+ end
33
+ end
34
+
35
+ context "default value" do
36
+ let(:attribute) { "example" }
37
+
38
+ it do
39
+ expect(record).to receive(:attribute).at_least(:once).and_call_original
40
+
41
+ result = described_class.call(record: record, attributes: [{ attribute: { default: [] } }])
42
+
43
+ expect(result.success?).to eq(true)
44
+ expect(result.record.attribute).to be_a_kind_of(Array)
45
+ end
46
+ end
47
+
48
+ context "configuration" do
49
+ let(:user) { OpenStruct.new(created_at: "example", save: true) }
50
+
51
+ context "when save is disabled" do
52
+ before { SimpleRewriter.configure { |config| config.save = false } }
53
+
54
+ it "doesnt save given record" do
55
+ expect(user).not_to receive(:save)
56
+
57
+ expect(described_class.call(record: user, attributes: [:created_at]).success?).to eq(true)
58
+ end
59
+ end
60
+
61
+ context "when save is enabled" do
62
+ before { SimpleRewriter.configure { |config| config.save = true } }
63
+
64
+ it "doesnt save given record" do
65
+ expect(user).to receive(:save).and_return(true)
66
+
67
+ expect(described_class.call(record: user, attributes: [:created_at]).success?).to eq(true)
68
+ end
69
+ end
70
+ end
71
+
72
+ context "deep structures" do
73
+ let(:record) do
74
+ hash = {}
75
+ hash["first_key"] = {}
76
+ hash["first_key"]["second_key"] = {}
77
+ hash["first_key"]["second_key"]["third_key"] = "deep_value"
78
+ hash
79
+ end
80
+ let(:attributes) do
81
+ [{
82
+ first_key: {
83
+ deep: true,
84
+ deep_attributes_map: [{
85
+ second_key: {
86
+ deep: true,
87
+ deep_attributes_map: [{
88
+ third_key: :email
89
+ }]
90
+ }
91
+ }]
92
+ }
93
+ }]
94
+ end
95
+
96
+ it "rewrites only deep nested value" do
97
+ result = described_class.call(record: record, attributes: attributes)
98
+
99
+ expect(result.record["first_key"]["second_key"]["third_key"]).to match(email_regex)
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,104 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16
+ require "simple_rewriter"
17
+
18
+ Dir[File.expand_path(File.join(File.dirname(__FILE__), "support", "**", "*.rb"))].each { |f| require f }
19
+
20
+ RSpec.configure do |config|
21
+ # rspec-expectations config goes here. You can use an alternate
22
+ # assertion/expectation library such as wrong or the stdlib/minitest
23
+ # assertions if you prefer.
24
+ config.include SimpleRewriter::BaseCheckers
25
+
26
+ config.expect_with :rspec do |expectations|
27
+ # This option will default to `true` in RSpec 4. It makes the `description`
28
+ # and `failure_message` of custom matchers include text for helper methods
29
+ # defined using `chain`, e.g.:
30
+ # be_bigger_than(2).and_smaller_than(4).description
31
+ # # => "be bigger than 2 and smaller than 4"
32
+ # ...rather than:
33
+ # # => "be bigger than 2"
34
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
35
+ end
36
+
37
+ # rspec-mocks config goes here. You can use an alternate test double
38
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
39
+ config.mock_with :rspec do |mocks|
40
+ # Prevents you from mocking or stubbing a method that does not exist on
41
+ # a real object. This is generally recommended, and will default to
42
+ # `true` in RSpec 4.
43
+ mocks.verify_partial_doubles = true
44
+ end
45
+
46
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
47
+ # have no way to turn it off -- the option exists only for backwards
48
+ # compatibility in RSpec 3). It causes shared context metadata to be
49
+ # inherited by the metadata hash of host groups and examples, rather than
50
+ # triggering implicit auto-inclusion in groups with matching metadata.
51
+ config.shared_context_metadata_behavior = :apply_to_host_groups
52
+
53
+ # The settings below are suggested to provide a good initial experience
54
+ # with RSpec, but feel free to customize to your heart's content.
55
+ # # This allows you to limit a spec run to individual examples or groups
56
+ # # you care about by tagging them with `:focus` metadata. When nothing
57
+ # # is tagged with `:focus`, all examples get run. RSpec also provides
58
+ # # aliases for `it`, `describe`, and `context` that include `:focus`
59
+ # # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
60
+ # config.filter_run_when_matching :focus
61
+ #
62
+ # # Allows RSpec to persist some state between runs in order to support
63
+ # # the `--only-failures` and `--next-failure` CLI options. We recommend
64
+ # # you configure your source control system to ignore this file.
65
+ # config.example_status_persistence_file_path = "spec/examples.txt"
66
+ #
67
+ # # Limits the available syntax to the non-monkey patched syntax that is
68
+ # # recommended. For more details, see:
69
+ # # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
70
+ # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
71
+ # # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
72
+ # config.disable_monkey_patching!
73
+ #
74
+ # # This setting enables warnings. It's recommended, but in some cases may
75
+ # # be too noisy due to issues in dependencies.
76
+ # config.warnings = true
77
+ #
78
+ # # Many RSpec users commonly either run the entire suite or an individual
79
+ # # file, and it's useful to allow more verbose output when running an
80
+ # # individual spec file.
81
+ # if config.files_to_run.one?
82
+ # # Use the documentation formatter for detailed output,
83
+ # # unless a formatter has already been configured
84
+ # # (e.g. via a command-line flag).
85
+ # config.default_formatter = "doc"
86
+ # end
87
+ #
88
+ # # Print the 10 slowest examples and example groups at the
89
+ # # end of the spec run, to help surface which specs are running
90
+ # # particularly slow.
91
+ # config.profile_examples = 10
92
+ #
93
+ # # Run specs in random order to surface order dependencies. If you find an
94
+ # # order dependency and want to debug it, you can fix the order by providing
95
+ # # the seed, which is printed after each run.
96
+ # # --seed 1234
97
+ # config.order = :random
98
+ #
99
+ # # Seed global randomization in this process using the `--seed` CLI option.
100
+ # # Setting this allows you to use `--seed` to deterministically reproduce
101
+ # # test failures related to randomization by passing the same `--seed` value
102
+ # # as the one that triggered the failure.
103
+ # Kernel.srand config.seed
104
+ end
@@ -0,0 +1,12 @@
1
+ module SimpleRewriter::BaseCheckers
2
+ def check_address_expectations
3
+ expect(Faker::Address).to receive(:street_address).and_call_original
4
+ expect(Faker::Address).to receive(:city).and_call_original
5
+ expect(Faker::Address).to receive(:zip).and_call_original
6
+ end
7
+
8
+ def check_base_expectations(klass)
9
+ expect(subject).to be_a_kind_of(klass)
10
+ expect(subject).not_to eq(value)
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_rewriter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Adam Grzybowski
8
+ - Piotr Misiurek
9
+ - Michał Szymański
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2019-04-19 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: faker
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: 1.9.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: 1.9.3
29
+ - !ruby/object:Gem::Dependency
30
+ name: pry
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: 0.12.2
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: 0.12.2
43
+ - !ruby/object:Gem::Dependency
44
+ name: ragnarson-stylecheck
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 0.7.0
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: 0.7.0
57
+ - !ruby/object:Gem::Dependency
58
+ name: rake
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: 12.3.2
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: 12.3.2
71
+ - !ruby/object:Gem::Dependency
72
+ name: rspec
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: 3.8.0
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - "~>"
83
+ - !ruby/object:Gem::Version
84
+ version: 3.8.0
85
+ description: Simple rewriter for rails based apps
86
+ email:
87
+ - adam@ragnarson.com
88
+ - piotr@ragnarson.com
89
+ - michal.t.szymanski@gmail.com
90
+ executables:
91
+ - pry
92
+ - rake
93
+ - rspec
94
+ - rubocop
95
+ extensions: []
96
+ extra_rdoc_files: []
97
+ files:
98
+ - ".circleci/config.yml"
99
+ - ".rspec"
100
+ - ".rubocop.yml"
101
+ - Gemfile
102
+ - Gemfile.lock
103
+ - LICENSE.md
104
+ - README.md
105
+ - Rakefile
106
+ - bin/pry
107
+ - bin/rake
108
+ - bin/rspec
109
+ - bin/rubocop
110
+ - examples/DB_ANONYMIZER.md
111
+ - examples/db_anonymizer.rb
112
+ - examples/db_anonymizer/mappings.rb
113
+ - lib/simple_rewriter.rb
114
+ - lib/simple_rewriter/attribute.rb
115
+ - lib/simple_rewriter/attribute_resolver.rb
116
+ - lib/simple_rewriter/base_service.rb
117
+ - lib/simple_rewriter/class_resolver.rb
118
+ - lib/simple_rewriter/configuration.rb
119
+ - lib/simple_rewriter/reader.rb
120
+ - lib/simple_rewriter/readers.rb
121
+ - lib/simple_rewriter/readers/active_record_reader.rb
122
+ - lib/simple_rewriter/readers/base_reader.rb
123
+ - lib/simple_rewriter/readers/hash_reader.rb
124
+ - lib/simple_rewriter/readers/open_struct_reader.rb
125
+ - lib/simple_rewriter/rewriter.rb
126
+ - lib/simple_rewriter/rewriters.rb
127
+ - lib/simple_rewriter/rewriters/active_support.rb
128
+ - lib/simple_rewriter/rewriters/active_support/time_with_zone_rewriter.rb
129
+ - lib/simple_rewriter/rewriters/array_rewriter.rb
130
+ - lib/simple_rewriter/rewriters/base.rb
131
+ - lib/simple_rewriter/rewriters/date_rewriter.rb
132
+ - lib/simple_rewriter/rewriters/float_rewriter.rb
133
+ - lib/simple_rewriter/rewriters/generators.rb
134
+ - lib/simple_rewriter/rewriters/hash_rewriter.rb
135
+ - lib/simple_rewriter/rewriters/integer_rewriter.rb
136
+ - lib/simple_rewriter/rewriters/options_resolver.rb
137
+ - lib/simple_rewriter/rewriters/string_rewriter.rb
138
+ - lib/simple_rewriter/type_detector.rb
139
+ - lib/simple_rewriter/version.rb
140
+ - lib/simple_rewriter/writer.rb
141
+ - simple_rewriter.gemspec
142
+ - spec/simple_rewriter/attribute_spec.rb
143
+ - spec/simple_rewriter/rewriters/hash_rewriter_spec.rb
144
+ - spec/simple_rewriter/rewriters/string_rewriter_spec.rb
145
+ - spec/simple_rewriter/type_detector_spec.rb
146
+ - spec/simple_rewriter_spec.rb
147
+ - spec/spec_helper.rb
148
+ - spec/support/simple_rewriter/base_checkers.rb
149
+ homepage: https://github.com/Verivox/simple_rewriter
150
+ licenses:
151
+ - MIT
152
+ metadata: {}
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubyforge_project:
169
+ rubygems_version: 2.7.9
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: Simple rewriter for rails based apps
173
+ test_files:
174
+ - spec/simple_rewriter/attribute_spec.rb
175
+ - spec/simple_rewriter/rewriters/hash_rewriter_spec.rb
176
+ - spec/simple_rewriter/rewriters/string_rewriter_spec.rb
177
+ - spec/simple_rewriter/type_detector_spec.rb
178
+ - spec/simple_rewriter_spec.rb
179
+ - spec/spec_helper.rb
180
+ - spec/support/simple_rewriter/base_checkers.rb