repository-support 0.1.1

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.
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'ldiff' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("diff-lcs", "ldiff")
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rake' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rake", "rake")
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rspec-core", "rspec")
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rubocop' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rubocop", "rubocop")
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'ruby-parse' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("parser", "ruby-parse")
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'ruby-rewrite' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("parser", "ruby-rewrite")
@@ -0,0 +1,36 @@
1
+ #!/bin/bash
2
+
3
+ # This works for MRI; it may or may not work for other Rubies.
4
+ RUBY_VERSION=`ruby --version | cut -d ' ' -f 2 | sed 's/p.*//'`
5
+
6
+ ##
7
+ ## Initialise Gemset and setup if `rbenv` is available
8
+ ##
9
+
10
+ rm -f Gemfile.lock .rbenv-gemsets
11
+
12
+ if [[ `rbenv --version 2>/dev/null` ]]; then
13
+ rbenv gemset delete $RUBY_VERSION ./tmp/gemset 2>/dev/null || true
14
+ find ./tmp/gemset -delete 2>/dev/null || true
15
+ rbenv rehash
16
+ rbenv gemset active
17
+ rbenv gemset create $RUBY_VERSION ./tmp/gemset
18
+ echo ./tmp/gemset > .rbenv-gemsets
19
+ rbenv rehash
20
+ fi
21
+
22
+ ##
23
+ ## Install Gems
24
+ ##
25
+
26
+ gem install bundler --no-doc -v 1.16.1
27
+ gem install yard -v 0.9.12
28
+ yard config --gem-install-yri
29
+ gem install json -v 2.1.0
30
+ gem install activemodel -v 4.2.10
31
+ gem install rake -v 12.3.0
32
+ gem install rspec -v 3.7.0
33
+ gem install rubocop -v 0.52.1
34
+ gem install simplecov -v 0.15.1
35
+ gem install awesome_print -v 1.8.0
36
+ bundle install --binstubs --local
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'repository/support/version'
4
+
5
+ require 'repository/support/error_factory'
6
+ require 'repository/support/result_builder'
7
+ require 'repository/support/store_result'
8
+ require 'repository/support/test_attribute_container'
9
+
10
+ # Support classes for Repository::Base and subclasses.
11
+ module Repository
12
+ # Support classes for Repository::Base and subclasses.
13
+ module Support
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Support classes for Repository::Base and subclasses.
4
+ module Repository
5
+ # Support classes for Repository::Base and subclasses.
6
+ module Support
7
+ # Factory for our funky error-info hashes.
8
+ class ErrorFactory
9
+ class << self
10
+ def create(errors)
11
+ errors.map do |field, message|
12
+ { field: field.to_s, message: message }
13
+ end
14
+ end
15
+ end
16
+ end # class Repository::Support::ErrorFactory
17
+ end
18
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'store_result'
4
+
5
+ module Repository
6
+ module Support
7
+ # Builds a successful or failed StoreResult depending on a record existing.
8
+ class ResultBuilder
9
+ def initialize(record)
10
+ @record = record
11
+ end
12
+
13
+ def build(&_block)
14
+ return successful_result if record
15
+ failed_result yield(record)
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :record
21
+
22
+ def failed_result(errors)
23
+ StoreResult::Failure.new errors
24
+ end
25
+
26
+ def successful_result
27
+ StoreResult::Success.new record
28
+ end
29
+ end # class Repository::Support::ResultBuilder
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Repository
4
+ module Support
5
+ # Uniform representation of success or failure of a storage-command action.
6
+ class StoreResult
7
+ # Convenience class to instantiate a "successful" StoreResult.
8
+ class Success < StoreResult
9
+ def initialize(entity)
10
+ super entity: entity, errors: [], success: true
11
+ end
12
+ end # class Repository::Support::StoreResult::Success
13
+
14
+ # Convenience class to instantiate a "failed" StoreResult.
15
+ class Failure < StoreResult
16
+ def initialize(errors)
17
+ super entity: nil, errors: errors, success: false
18
+ end
19
+ end # class Repository::Support::StoreResult::Failure
20
+
21
+ attr_reader :entity, :errors, :success
22
+ alias_method :success?, :success # rubocop:disable Style/Alias
23
+
24
+ def initialize(entity:, errors:, success:)
25
+ @entity = entity
26
+ @errors = errors
27
+ @success = success
28
+ end
29
+ end # class Repository::Support::StoreResult
30
+ end
31
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Support classes for Repository::Base and subclasses.
4
+ module Repository
5
+ # Support classes for Repository::Base and subclasses.
6
+ module Support
7
+ # Adds :attributes property to caller, and enhanced attribute getter/setter.
8
+ module TestAttributeContainer
9
+ # rubocop:disable Metrics/AbcSize
10
+ # rubocop:disable Metrics/CyclomaticComplexity
11
+ # rubocop:disable Metrics/MethodLength
12
+ def init_empty_attribute_container
13
+ define_method :attributes do
14
+ @attributes ||= {}
15
+ end
16
+
17
+ define_method(:attributes=) do |attribs|
18
+ @attributes = attribs.to_h
19
+ end
20
+
21
+ define_method(:method_missing) do |method_sym, *arguments, &block|
22
+ method_or_type = attribute_key_for?(method_sym)
23
+ case method_or_type
24
+ when :none
25
+ super method_sym, arguments, block
26
+ when :reader
27
+ attributes[method_sym]
28
+ else
29
+ attributes[method_or_type] = arguments.first
30
+ end
31
+ end
32
+
33
+ define_method(:respond_to?) do |method_sym, include_private = false|
34
+ return true unless attribute_key_for?(method_sym) == :none
35
+ super method_sym, include_private
36
+ end
37
+
38
+ # private
39
+ define_method(:attribute_key_for?) do |key_sym|
40
+ if attributes.to_h.key? key_sym
41
+ :reader
42
+ else
43
+ setter_for_or_none(key_sym)
44
+ end
45
+ end
46
+
47
+ # private
48
+ define_method :setter_for_or_none do |key|
49
+ match = key.to_s.match(/(\S+?)=/)
50
+ has_setter = match && attributes.key?(match[1].to_sym)
51
+ has_setter ? match[1].to_sym : :none
52
+ end
53
+ instance_eval { private :attribute_key_for?, :setter_for_or_none }
54
+ self
55
+ end # end method .init_empty_attribute_container
56
+ # rubocop:enable Metrics/MethodLength
57
+ # rubocop:enable Metrics/CyclomaticComplexity
58
+ # rubocop:enable Metrics/AbcSize
59
+ self
60
+ end # module Repository::Support::TestAttributeContainer
61
+ end
62
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nocov:
4
+ module Repository
5
+ module Support
6
+ VERSION = '0.1.1'
7
+ end
8
+ end
9
+ # :nocov:
@@ -0,0 +1,33 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'repository/support/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'repository-support'
8
+ spec.version = Repository::Support::VERSION
9
+ spec.authors = ['Jeff Dickey']
10
+ spec.email = ['jdickey@seven-sigma.com']
11
+ spec.summary = %q{Support classes for Repository::Base and subclasses.}
12
+ spec.description = %q{Support classes for Repository::Base and subclasses.}
13
+ spec.homepage = 'https://github.com/jdickey/repository-support'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ # This Gem installs no executables of its own, and developer stubs for bin/setup will be overwritten
18
+ # spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.required_ruby_version = '>= 2.4.0'
23
+
24
+ spec.add_dependency 'activemodel', '~> 4.2', '>= 4.2.10'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.16' # '10.5.0'
27
+ spec.add_development_dependency 'rake', '~> 12.3', '>= 12.3.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.7'
29
+ spec.add_development_dependency 'rubocop', '~> 0.52', '>= 0.52.1'
30
+ spec.add_development_dependency 'simplecov', '~> 0.15', '>= 0.15.1'
31
+ spec.add_development_dependency 'awesome_print', '>= 1.8.0'
32
+ # spec.add_development_dependency 'pry-byebug'
33
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'active_model'
5
+
6
+ describe Repository::Support::ErrorFactory do
7
+ it 'has a .create class method' do
8
+ expect { described_class.method :create }.not_to raise_error
9
+ end
10
+
11
+ describe 'has a .create class method that, when called' do
12
+ let(:errors) { ActiveModel::Errors.new self }
13
+ let(:actual) { described_class.create errors }
14
+
15
+ context 'with an empty Errors object' do
16
+ it 'returns an empty array' do
17
+ expect(actual).to respond_to :to_ary
18
+ expect(actual).to be_empty
19
+ end
20
+ end # context 'with an empty Errors object'
21
+
22
+ context 'with an Errors object containing errors' do
23
+ let(:samples) do
24
+ [
25
+ { field: 'field1', message: 'is invalid' },
26
+ { field: 'field1', message: 'is empty or blank' },
27
+ { field: 'field2', message: 'is :field2' }
28
+ ]
29
+ end
30
+
31
+ before :each do
32
+ samples.each { |sample| errors.add sample[:field], sample[:message] }
33
+ end
34
+
35
+ it 'returns an array with the correct error hashes' do
36
+ expect(actual).to eq samples
37
+ end
38
+ end # context 'with an Errors object containing errors'
39
+ end # describe 'has a .create class method that, when called'
40
+ end # describe Repository::Support::ErrorFactory