easy_reference_data 0.1.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 797948128bb29186093a3c03a061440ada7d8022
4
+ data.tar.gz: 3d27734336da1da1609c831788c1a3b6a4b133a0
5
+ SHA512:
6
+ metadata.gz: 634a7afb6d365a4efea98214540762e6e2ad8a7393a62b61200b9c78a8797d8cc594b6caa6155786ae4e14ca17bad67bf77e156cc465742c102689c55de3c68c
7
+ data.tar.gz: 1cf44764bbd14bce2e61fbe9cf35b1156622b782f1aa461746c3d49afbc20c10fe86b33c23de4736e98c295e8c2561550a2f46802ac252f397b709dc09eee01e
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ sudo: false
3
+ branches:
4
+ rvm:
5
+ - 2.2.7
6
+ - 2.3.4
7
+ - 2.4.1
data/ChangeLog CHANGED
@@ -1,3 +1,10 @@
1
+ 1.0.0 - 2017-07-18
2
+
3
+ Enhancements
4
+
5
+ * added .update_or_create method to replace the implementation of .refresh
6
+ .update_or_create allows you to use a combination of keys to identity records that have no single unique attribute.
7
+
1
8
  0.1.2 - 2012-11-15
2
9
 
3
10
  Bug fixes
data/README.md CHANGED
@@ -23,6 +23,7 @@ Place references in 'db/reference/'
23
23
  References will be loaded in ascending order, so if an order is desired, prepend 000, 001, 002... etc to the filename.
24
24
 
25
25
  Run with:
26
+
26
27
  rake easy:reference_data:refresh
27
28
 
28
29
  ## Deployment
@@ -36,9 +37,16 @@ Add this line to your application's deploy.rb file:
36
37
  The below example ensures that there are 3 users existing in the database after running the 'rake reference_data:load'
37
38
 
38
39
  ### db/reference/000_users.rb
39
- Easy::ReferenceData.refresh User, :system_code, 'nigel', name: 'Nigel Ramsay', email: 'nigel.ramsay@mailinator.com'
40
- Easy::ReferenceData.refresh User, :system_code, 'fred', name: 'Fred Schmitt', email: 'fred.schmitt@mailinator.com'
41
- Easy::ReferenceData.refresh User, :system_code, 'bert', name: 'Bert Symthe', email: 'bert.smythe@mailinator.com'
40
+ Easy::ReferenceData.update_or_create User, {system_code: 'nigel', name: 'Nigel Ramsay', email: 'nigel.ramsay@mailinator.com'}, keys: [:system_code]
41
+ Easy::ReferenceData.update_or_create User, {system_code: 'fred', name: 'Fred Schmitt', email: 'fred.schmitt@mailinator.com'}, keys: [:system_code]
42
+ Easy::ReferenceData.update_or_create User, {system_code: 'bert', name: 'Bert Symthe', email: 'bert.smythe@mailinator.com'}, keys: [:system_code]
43
+
44
+ Multiple keys can be used to identify records that would otherwise not have a unique attribute
45
+
46
+ ### db/reference/000_prices.rb
47
+ Easy::ReferenceData.update_or_create Price, {product_id: 1, type: "Price::RetailPrice", price: 5}, keys: [:product_id, :type]
48
+ Easy::ReferenceData.update_or_create Price, {product_id: 1, type: "Price::CostPrice", price: 4}, keys: [:product_id, :type]
49
+ Easy::ReferenceData.update_or_create Price, {product_id: 2, type: "Price::RetailPrice", price: 5}, keys: [:product_id, :type]
42
50
 
43
51
  ## Contributing
44
52
 
data/Rakefile CHANGED
@@ -1 +1,5 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
@@ -18,4 +18,8 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_runtime_dependency 'rails', '>= 3.0.0'
21
+
22
+ gem.add_development_dependency 'rspec', '~> 3.6.0'
23
+ gem.add_development_dependency 'sqlite3', '~> 1.3'
24
+ gem.add_development_dependency 'database_cleaner', '~> 1.6'
21
25
  end
@@ -1,31 +1,31 @@
1
+ require 'active_support'
2
+
1
3
  module Easy
2
4
  module ReferenceData
3
5
  def self.refresh(clazz, unique_attribute_symbol, unique_attribute_value, attributes)
4
- record = clazz.where(unique_attribute_symbol => unique_attribute_value).first
6
+ self.update_or_create(clazz, attributes.merge(unique_attribute_symbol => unique_attribute_value), keys: [unique_attribute_symbol])
7
+ end
5
8
 
6
- if record.nil?
7
- record = clazz.new
8
- record.send "#{unique_attribute_symbol}=", unique_attribute_value
9
- end
9
+ def self.update_or_create(clazz, attributes, options)
10
+ unique_attribute_keys = options.fetch(:keys)
10
11
 
11
- attributes.each_pair do |key, value|
12
- record.send "#{key}=", value
13
- end
12
+ record = clazz.where(attributes.slice(*unique_attribute_keys)).first_or_initialize
14
13
 
15
14
  if record.new_record?
16
- puts "..creating #{clazz}(#{unique_attribute_value})"
17
- elsif record.changed?
18
- puts "..updating #{clazz}(#{unique_attribute_value})"
15
+ $stderr.puts "..creating #{clazz}(#{attributes.slice(*unique_attribute_keys)})"
16
+ else
17
+ $stderr.puts "..updating #{clazz}(#{attributes.slice(*unique_attribute_keys)})"
19
18
  end
20
19
 
21
20
  begin
22
- record.save!
21
+ record.update_attributes!(attributes)
23
22
  rescue
24
- puts "Save failed for #{record.class}[#{unique_attribute_symbol}: #{unique_attribute_value}] with attributes #{attributes.inspect}"
23
+ $stderr.puts "Save failed for #{record.class} with attributes #{attributes.inspect}"
25
24
  raise
26
25
  end
27
26
 
28
27
  record
29
28
  end
29
+
30
30
  end
31
31
  end
@@ -1,5 +1,5 @@
1
1
  module Easy
2
2
  module ReferenceData
3
- VERSION = "0.1.2"
3
+ VERSION = "1.0.0"
4
4
  end
5
- end
5
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+ require 'easy/reference_data/refresh'
3
+
4
+ RSpec.describe Easy::ReferenceData do
5
+
6
+ describe ".update_or_create" do
7
+ context "with a single unique attribute" do
8
+ context "and an existing record" do
9
+
10
+ it "does not change the record" do
11
+ user = User.create(system_code: 1)
12
+
13
+ expect{ Easy::ReferenceData.update_or_create(User, {system_code: 1}, keys: [:system_code])}.not_to change{ User.count }
14
+ end
15
+
16
+ context "with additional attribues" do
17
+ it "updates the existing record" do
18
+ user = User.create(system_code: 1, name: "Jo")
19
+
20
+ expect{ Easy::ReferenceData.update_or_create(User, {system_code: 1, name: "Jane"}, keys: [:system_code])}.to change{ user.reload.name }.to "Jane"
21
+ end
22
+ end
23
+ end
24
+
25
+ context "and no existing record" do
26
+ it "creates a new record" do
27
+ expect{ Easy::ReferenceData.update_or_create(User, {system_code: 1}, keys: [:system_code])}.to change{ User.count }
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ context "with multiple attributes" do
34
+ it "updates the matching record" do
35
+ jo_smith = User.create(system_code: 1, name: "Jo", email: "jo.smith@example.com")
36
+ jo_brown = User.create(system_code: 1, name: "Jo", email: "jo.brown@example.com")
37
+
38
+ expect{ Easy::ReferenceData.update_or_create(User, {name: "Jo", email: "jo.brown@example.com", system_code: 2}, keys: [:name, :email])}.to change{ jo_brown.reload.system_code }.to 2
39
+ end
40
+
41
+ end
42
+ end
43
+
44
+ describe ".refresh" do
45
+
46
+ context "with a unique attribue" do
47
+ context "and no exisitng record" do
48
+
49
+ it "creates a new record" do
50
+ expect{
51
+ Easy::ReferenceData.refresh User, :system_code, 1, name: "Jane", email: "jane@example.com"
52
+ }.to change{ User.count }
53
+ end
54
+
55
+ end
56
+
57
+ context "and an exisitng record" do
58
+ it "updates the existing record" do
59
+ user = User.create(system_code: 1, name: "Jo")
60
+
61
+ expect{ Easy::ReferenceData.refresh User, :system_code, 1, name: "Jane", email: "jane@example.com" }.to change{ user.reload.name }.to "Jane"
62
+ end
63
+
64
+ it "does not create duplicate records" do
65
+ user = User.create(system_code: 1, name: "Jo")
66
+
67
+ expect{ Easy::ReferenceData.refresh User, :system_code, 1, name: "Jo", email: "jane@example.com" }.not_to change{ User.count }
68
+ end
69
+ end
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,121 @@
1
+ require 'active_record'
2
+ require 'database_cleaner'
3
+
4
+ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
5
+
6
+ require 'support/models'
7
+
8
+ # This file was generated by the `rspec --init` command. Conventionally, all
9
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
10
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
11
+ # this file to always be loaded, without a need to explicitly require it in any
12
+ # files.
13
+ #
14
+ # Given that it is always loaded, you are encouraged to keep this file as
15
+ # light-weight as possible. Requiring heavyweight dependencies from this file
16
+ # will add to the boot time of your test suite on EVERY test run, even for an
17
+ # individual file that may not need all of that loaded. Instead, consider making
18
+ # a separate helper file that requires the additional dependencies and performs
19
+ # the additional setup, and require it from the spec files that actually need
20
+ # it.
21
+ #
22
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
23
+ RSpec.configure do |config|
24
+ # rspec-expectations config goes here. You can use an alternate
25
+ # assertion/expectation library such as wrong or the stdlib/minitest
26
+ # assertions if you prefer.
27
+ config.expect_with :rspec do |expectations|
28
+ # This option will default to `true` in RSpec 4. It makes the `description`
29
+ # and `failure_message` of custom matchers include text for helper methods
30
+ # defined using `chain`, e.g.:
31
+ # be_bigger_than(2).and_smaller_than(4).description
32
+ # # => "be bigger than 2 and smaller than 4"
33
+ # ...rather than:
34
+ # # => "be bigger than 2"
35
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
36
+ end
37
+
38
+ # rspec-mocks config goes here. You can use an alternate test double
39
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
40
+ config.mock_with :rspec do |mocks|
41
+ # Prevents you from mocking or stubbing a method that does not exist on
42
+ # a real object. This is generally recommended, and will default to
43
+ # `true` in RSpec 4.
44
+ mocks.verify_partial_doubles = true
45
+ end
46
+
47
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
48
+ # have no way to turn it off -- the option exists only for backwards
49
+ # compatibility in RSpec 3). It causes shared context metadata to be
50
+ # inherited by the metadata hash of host groups and examples, rather than
51
+ # triggering implicit auto-inclusion in groups with matching metadata.
52
+ config.shared_context_metadata_behavior = :apply_to_host_groups
53
+
54
+ # The settings below are suggested to provide a good initial experience
55
+ # with RSpec, but feel free to customize to your heart's content.
56
+ =begin
57
+ # This allows you to limit a spec run to individual examples or groups
58
+ # you care about by tagging them with `:focus` metadata. When nothing
59
+ # is tagged with `:focus`, all examples get run. RSpec also provides
60
+ # aliases for `it`, `describe`, and `context` that include `:focus`
61
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
62
+ config.filter_run_when_matching :focus
63
+
64
+ # Allows RSpec to persist some state between runs in order to support
65
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
66
+ # you configure your source control system to ignore this file.
67
+ config.example_status_persistence_file_path = "spec/examples.txt"
68
+
69
+ # Limits the available syntax to the non-monkey patched syntax that is
70
+ # recommended. For more details, see:
71
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
72
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
73
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
74
+ config.disable_monkey_patching!
75
+
76
+ # This setting enables warnings. It's recommended, but in some cases may
77
+ # be too noisy due to issues in dependencies.
78
+ config.warnings = true
79
+
80
+ # Many RSpec users commonly either run the entire suite or an individual
81
+ # file, and it's useful to allow more verbose output when running an
82
+ # individual spec file.
83
+ if config.files_to_run.one?
84
+ # Use the documentation formatter for detailed output,
85
+ # unless a formatter has already been configured
86
+ # (e.g. via a command-line flag).
87
+ config.default_formatter = "doc"
88
+ end
89
+
90
+ # Print the 10 slowest examples and example groups at the
91
+ # end of the spec run, to help surface which specs are running
92
+ # particularly slow.
93
+ config.profile_examples = 10
94
+
95
+ # Run specs in random order to surface order dependencies. If you find an
96
+ # order dependency and want to debug it, you can fix the order by providing
97
+ # the seed, which is printed after each run.
98
+ # --seed 1234
99
+ config.order = :random
100
+
101
+ # Seed global randomization in this process using the `--seed` CLI option.
102
+ # Setting this allows you to use `--seed` to deterministically reproduce
103
+ # test failures related to randomization by passing the same `--seed` value
104
+ # as the one that triggered the failure.
105
+ Kernel.srand config.seed
106
+ =end
107
+
108
+ config.before(:suite) do
109
+ DatabaseCleaner.strategy = :transaction
110
+ DatabaseCleaner.clean_with(:transaction)
111
+ end
112
+
113
+ config.before(:each) do
114
+ DatabaseCleaner.start
115
+ end
116
+
117
+ config.after(:each) do
118
+ DatabaseCleaner.clean
119
+ end
120
+
121
+ end
@@ -0,0 +1,5 @@
1
+ require 'active_record'
2
+ load 'support/schema.rb'
3
+
4
+ class User < ActiveRecord::Base
5
+ end
@@ -0,0 +1,11 @@
1
+ require 'active_record'
2
+
3
+ ActiveRecord::Schema.define do
4
+ self.verbose = false
5
+
6
+ create_table :users, force: true do |t|
7
+ t.integer :system_code
8
+ t.string :name
9
+ t.string :email
10
+ end
11
+ end
metadata CHANGED
@@ -1,32 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_reference_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
5
- prerelease:
4
+ version: 1.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Nigel Ramsay
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-11-15 00:00:00.000000000 Z
11
+ date: 2017-07-28 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rails
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: 3.0.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.6.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.6.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: database_cleaner
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
30
69
  description: Used to create a predefined set of model instances
31
70
  email:
32
71
  - nigel.ramsay@abletech.co.nz
@@ -34,7 +73,9 @@ executables: []
34
73
  extensions: []
35
74
  extra_rdoc_files: []
36
75
  files:
37
- - .gitignore
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
38
79
  - ChangeLog
39
80
  - Gemfile
40
81
  - LICENSE.txt
@@ -47,29 +88,35 @@ files:
47
88
  - lib/easy/reference_data/version.rb
48
89
  - lib/easy_reference_data.rb
49
90
  - lib/tasks/easy_reference_data.rake
91
+ - spec/easy/reference_data/refresh_spec.rb
92
+ - spec/spec_helper.rb
93
+ - spec/support/models.rb
94
+ - spec/support/schema.rb
50
95
  homepage: http://github.com/AbleTech/easy_reference_data
51
96
  licenses: []
97
+ metadata: {}
52
98
  post_install_message:
53
99
  rdoc_options: []
54
100
  require_paths:
55
101
  - lib
56
102
  required_ruby_version: !ruby/object:Gem::Requirement
57
- none: false
58
103
  requirements:
59
- - - ! '>='
104
+ - - ">="
60
105
  - !ruby/object:Gem::Version
61
106
  version: '0'
62
107
  required_rubygems_version: !ruby/object:Gem::Requirement
63
- none: false
64
108
  requirements:
65
- - - ! '>='
109
+ - - ">="
66
110
  - !ruby/object:Gem::Version
67
111
  version: '0'
68
112
  requirements: []
69
113
  rubyforge_project:
70
- rubygems_version: 1.8.24
114
+ rubygems_version: 2.6.11
71
115
  signing_key:
72
- specification_version: 3
116
+ specification_version: 4
73
117
  summary: Loads files from db/reference/*.rb
74
- test_files: []
75
- has_rdoc:
118
+ test_files:
119
+ - spec/easy/reference_data/refresh_spec.rb
120
+ - spec/spec_helper.rb
121
+ - spec/support/models.rb
122
+ - spec/support/schema.rb