repositorish 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MjE1ZDNkMmEyNmE1MzU2NzYyYjU5NjliZmUwNjcxNzE1ZDczZTkyNQ==
5
+ data.tar.gz: !binary |-
6
+ ZWMwODRmMDNlYTgwMDlhZjZiMTJkYjg2NGMwMTI5ZjhjNGFhNWY5OQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NjFmNmRiNDA2ZjllMDJlYTk2YTAxNDk2OGIyMzY5MjkwZjdmZDlhNTgwMTky
10
+ NGNkZmE1M2Q1NmYwZDk5NzU1MjU2MjkxNWRjOTU3MjFjNTk4NjA1NTNhMjZk
11
+ N2Q4Yjc1NDgwNmZhMWNmZWMxNTllN2VhMjRlYjJmNDhlN2U1NDE=
12
+ data.tar.gz: !binary |-
13
+ MTVjZGFmNzczMDYzMTk1MzFiM2I0YTMwNjc1YzhhMjYyMzBkYzM3YzQwZTY1
14
+ NTk5YWJiZmQ1YThlYjJiOTBmMzc3ZmU4OGZmZDk3ZWY2MTIwMTc3ZjI4OGI0
15
+ MDFkYmFjZWQ4NGFkMWY2NTFjZGI5ODI2YWUxODUxNTZkMzAxOWM=
data/.codeclimate.yml ADDED
@@ -0,0 +1,14 @@
1
+ ---
2
+ engines:
3
+ bundler-audit:
4
+ enabled: false
5
+ fixme:
6
+ enabled: true
7
+ rubocop:
8
+ enabled: true
9
+ ratings:
10
+ paths:
11
+ - Gemfile.lock
12
+ - "**.rb"
13
+ exclude_paths:
14
+ - spec/**/*
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .rspec_persistence
2
+ Gemfile.lock
data/.rubocop.yml ADDED
@@ -0,0 +1,7 @@
1
+ Metrics/LineLength:
2
+ Max: 110
3
+
4
+ Style/Documentation:
5
+ Exclude:
6
+ - spec/**/*
7
+ - lib/repositorish/version.rb
data/.travis.yml ADDED
@@ -0,0 +1,23 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.2
5
+ - ruby-head
6
+ git:
7
+ depth: 1
8
+ script:
9
+ - bundle exec rspec
10
+ - bundle exec rubocop
11
+ add_ons:
12
+ code_climate:
13
+ repo_token:
14
+ secure: S8SpUvj3QfLt+IpeupqWffChzlP+UcI9aOa5CMlhNq1VN7mK02fTWlAMc/hIOR7av8yUrljVynrafYp6c+oh2XZSw458Rs+OaERUcB8kvAdR2bGSfB7zHvVsu6CbEWLiDRdoDxuF8Au8G8lISAi8xqs4KxuXBLUzuYiFt0ZSPQP8+PqKoX8L7ksMq37Rcnp0DrmyRVK9jfbWKo8wW/WchsI0lZHwVty4NKQyGw9Z5kfiQfqcP6kqMU810dE/YOrSbEuYqMk/vIX1GNngvKygrXjTs9yJ/tY46gjgoy+lvOPKvM/LvgkbLMXZrBWjCb8peh4glJADfYGcXvGLqRozA3C+g1uAb4llaiPwiqGT9i+RFB/sWer4M5rmTNwv0fleo9iOylZFcSBtSTxcrOg5zubH9CrDPQOM/TnMSVor1LsDYd3D9OCeYIzPqDphYRtqAtr48jdzosRohCuHN0HmD2fMTuU80lZy75+HBlWdZgHG+8wG/IiNtuxe7/4XtMkR4zBb7GHhCtPq6AEf18TfHMxrTU/PwGe6lACiP7f0EY1lFDfhrEu634JatCIUWZcTMkJyjoPTKGEXXaKcnTpF+JbxyJRHZ5SfRfIfZ7aiVNP9aECpYHUX1pgh4NCWgkaDkyk+lvqQ5xGLZePf8sYxOW0ENCXynebar0NqUtBgHlY=
15
+ deploy:
16
+ provider: rubygems
17
+ api_key:
18
+ secure: WutIgn21Jgy0Q3E/y47pALrxmyEEPMNWg7a0WEcGL72EfEhge0BG9FtiSds5uXbeA77PPQ4bNtZlkfv2wvm6ceJmQLO8HMnhqptZLAPTUqnyhlX9g2Cn0qHpm2heujlT2ORoE1+x4GB1f03OH3WaBliDat5hbd/CS0CJsZI9AJOo0TnbCJZ66E+TMfQAr+tDMDaBfNnWQAg0cXd3g2qfa1tmHnVhbpRahqgBxaC7XKYyHT7Y2TwSYePjc7yK/e9HxRF3zdBEGiq8tGFzvVSsT1NjujdJGyXRKRmDrl/yCHx5NwTXLVBuu/eitIP0zuVSIUrnSptU+Yo654DpVILzlLhdnuL5vhrl280YE3lrknYVh9udp2H9W7GKnCqRKEesUhNGHUHXsp1eBm+4h05B0ZiqYSKbNokeCwvG0aZpM13qa7mE1nnPa+gHznxk/Z0y5lzm9b/NNB2kzsp5tHTAi+ANedZ7K0nqk1gI3IMonUtujq8xe2sio0JQ7NGRp6zTL3eF3HJr6yTLxEYHfoXq7TNxPUX8BwskH24TAF0/HtRTSM47z5voBiGOUttFa1rTz+WmUu7V5cpgCFWjEfUiHgwDu35VmHZe8LWXclQFJU6mHm49p25asrnwxqTiFHG0hX31RaxwT4QhKOpB+y5PlELrPeQvpnrIE8z1jegcB98=
19
+ gem: repositorish
20
+ on:
21
+ tags: true
22
+ repo: LoveMondays/repositorish
23
+ branch: master
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Love Mondays
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # Repositorish
2
+ [![Build Status](https://travis-ci.org/LoveMondays/repositorish.svg)](https://travis-ci.org/LoveMondays/repositorish) [![Code Climate](https://codeclimate.com/github/LoveMondays/repositorish/badges/gpa.svg)](https://codeclimate.com/github/LoveMondays/repositorish) [![Test Coverage](https://codeclimate.com/github/LoveMondays/repositorish/badges/coverage.svg)](https://codeclimate.com/github/LoveMondays/repositorish/coverage)
3
+
4
+ Simple Repository(ish) solution to hold query and command logic into self contained objects
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'repositorish'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install repositorish
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ class User < ActiveRecord::Base
26
+ scope :alphabetically, -> { order(name: :asc) }
27
+ end
28
+ ```
29
+
30
+ ```ruby
31
+ class UserRepository
32
+ include Repositorish
33
+
34
+ repositorish :user, scope: :all
35
+
36
+ def confirmed
37
+ where.not(confirmed_at: nil)
38
+ end
39
+
40
+ def last_sign_in_after(date)
41
+ where(arel_table[:last_sign_in_at].gt(date))
42
+ end
43
+
44
+ def active
45
+ confirmed.last_sign_in_after(1.week.ago).alphabetically
46
+ end
47
+ end
48
+ ```
49
+
50
+ ```ruby
51
+ john = User.new(name: 'John')
52
+ john.new_record?
53
+ # => true
54
+
55
+ UserRepository.create(user)
56
+ # => true
57
+
58
+ john.persisted?
59
+ # => true
60
+
61
+ john.last_sign_in_at = 2.week.ago
62
+ john.confirmed_at = 1.month.ago
63
+ UserRepository.update(user)
64
+ # => true
65
+
66
+ mary = User.new(name: 'Mary', last_sign_in_at: 1.day.ago, confirmed_at: 2.day.ago)
67
+ UserRepository.create(mary)
68
+ # => true
69
+
70
+ UserRepository.confirmed
71
+ # => [#<User name: 'John'>]
72
+
73
+ UserRepository.active
74
+ # => [#<User name: 'Mary'>]
75
+
76
+ UserRepository.alphabetically
77
+ # => Repositorish::DomainMethodError: Direct call on domain's methods is not allowed
78
+
79
+ UserRepository.active.destroy_all
80
+ # => [#<User name: 'Mary'>]
81
+
82
+ UserRepository.destroy(john)
83
+ # => #<User name: 'John'>
84
+ ```
85
+
86
+ ## Development
87
+
88
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
89
+
90
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
91
+
92
+ ## Contributing
93
+
94
+ 1. Fork it ( https://github.com/LoveMondays/repositorish/fork )
95
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
96
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
97
+ 4. Push to the branch (`git push origin my-new-feature`)
98
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ RuboCop::RakeTask.new(:rubocop)
7
+
8
+ task default: [:rubocop, :spec]
@@ -0,0 +1,3 @@
1
+ module Repositorish
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,108 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+ require 'repositorish/version'
3
+
4
+ # Simple Repository(ish) solution to hold query and command logic into
5
+ # self contained objects
6
+ #
7
+ # # Example:
8
+ #
9
+ # ```ruby
10
+ # class UserRepository
11
+ # include Repositorish
12
+ #
13
+ # repositorish :user, scope: :all
14
+ #
15
+ # def confirmed
16
+ # where.not(confirmed_at: nil)
17
+ # end
18
+ # end
19
+ #
20
+ # UserRepository.confirmed
21
+ # # => <User::ActiveRecord_Relation ...>
22
+ # ```
23
+ module Repositorish
24
+ CHAINABLE_NAMESPACES = %w(ActiveRecord ActiveRecord_Relation ActiveRecord_AssociationRelation)
25
+ CHAINABLE_NAMESPACES_REGEX = /(?:^|::)(?:#{CHAINABLE_NAMESPACES.join('|')})(?:$|::)/.freeze
26
+
27
+ def self.included(base)
28
+ base.send :extend, ClassMethods
29
+ end
30
+
31
+ def initialize(domain)
32
+ @domain = domain
33
+ end
34
+
35
+ def method_missing(method, *args, &block)
36
+ return super unless domain.respond_to?(method)
37
+
38
+ result = domain.public_send(method, *args, &block)
39
+ return result unless chainable?(result)
40
+
41
+ @domain = result
42
+ self
43
+ end
44
+
45
+ def to_ary
46
+ @domain.to_ary
47
+ end
48
+
49
+ private
50
+
51
+ attr_reader :domain
52
+
53
+ def arel_table
54
+ domain.arel_table
55
+ end
56
+
57
+ def chainable?(domain)
58
+ return true if CHAINABLE_NAMESPACES_REGEX =~ domain.class.to_s
59
+
60
+ domain.class == @domain.class
61
+ end
62
+
63
+ # :nodoc:
64
+ module ClassMethods
65
+ def repositorish(model, options = {})
66
+ @domain = model.to_s.classify.constantize
67
+ @domain = @domain.public_send(options[:scope]) if options[:scope]
68
+ self
69
+ end
70
+
71
+ def create(model, *args, &block)
72
+ return false if model.persisted?
73
+
74
+ model.save(*args, &block)
75
+ end
76
+
77
+ def update(model, *args, &block)
78
+ return false if model.new_record?
79
+
80
+ model.save(*args, &block)
81
+ end
82
+
83
+ def destroy(model)
84
+ return if model.new_record?
85
+
86
+ model.destroy
87
+ end
88
+
89
+ def query(domain = @domain)
90
+ new(domain)
91
+ end
92
+
93
+ def method_missing(method, *args, &block)
94
+ return query.public_send(method, *args, &block) if method_defined?(method)
95
+
96
+ fail DomainMethodError, method if @domain.respond_to?(method)
97
+ super
98
+ end
99
+ end
100
+
101
+ # Custom error that warns users when calling domain's methods directly on repository
102
+ # It avoid users to use repositories as actual models
103
+ class DomainMethodError < NoMethodError
104
+ def initialize(method, *args)
105
+ super("Direct call on domain's methods is not allowed", method, *args)
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'repositorish/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'repositorish'
8
+ s.version = Repositorish::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+
11
+ s.date = '2015-11-10'
12
+ s.summary = 'Simple Repository(ish) solution to hold query and ' \
13
+ 'command logic into self contained objects'
14
+ s.homepage = 'https://github.com/LoveMondays/repositorish'
15
+ s.license = 'MIT'
16
+
17
+ s.authors = ['Glauber Campinho', 'Brenno Costa']
18
+ s.email = ['ggcampinho@gmail.com', 'brennolncosta@gmail.com']
19
+ s.files = `git ls-files -z`.split("\x0")
20
+
21
+ s.add_dependency 'activesupport'
22
+
23
+ s.add_development_dependency 'activerecord'
24
+ s.add_development_dependency 'byebug'
25
+ s.add_development_dependency 'codeclimate-test-reporter'
26
+ s.add_development_dependency 'rake'
27
+ s.add_development_dependency 'rspec', '~> 3.3.0'
28
+ s.add_development_dependency 'rubocop'
29
+ s.add_development_dependency 'sqlite3'
30
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+ require 'active_record'
3
+ require 'sqlite3'
4
+
5
+ RSpec.describe 'ActiveRecord integration' do
6
+ before(:all) do
7
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
8
+
9
+ ActiveRecord::Schema.define do
10
+ create_table(:users, force: true) do |t|
11
+ t.string :name
12
+ t.datetime :last_sign_in_at
13
+ t.datetime :confirmed_at
14
+ end
15
+ end
16
+
17
+ class User < ActiveRecord::Base
18
+ scope :alphabetically, -> { order(name: :asc) }
19
+ end
20
+
21
+ class UserRepository
22
+ include Repositorish
23
+
24
+ repositorish :user, scope: :all
25
+
26
+ def confirmed
27
+ where.not(confirmed_at: nil)
28
+ end
29
+
30
+ def last_sign_in_after(date)
31
+ where(arel_table[:last_sign_in_at].gt(date))
32
+ end
33
+
34
+ def active
35
+ confirmed.last_sign_in_after(1.week.ago).alphabetically
36
+ end
37
+ end
38
+ end
39
+
40
+ it 'creates a new user' do
41
+ john = User.new(name: 'John')
42
+
43
+ expect(UserRepository.create(john)).to be true
44
+ expect(john).to be_persisted
45
+ end
46
+
47
+ it 'updates an existing user' do
48
+ john = User.create(name: 'John')
49
+ john.name = 'John Will'
50
+
51
+ expect(UserRepository.update(john)).to be true
52
+ expect(john.reload.name).to eq('John Will')
53
+ end
54
+
55
+ it 'destroy an existing user' do
56
+ john = User.create(name: 'John')
57
+
58
+ expect(UserRepository.destroy(john)).to eq(john)
59
+ expect { john.reload }.to raise_error(ActiveRecord::RecordNotFound)
60
+ end
61
+
62
+ it 'delegates the relation methods to domain' do
63
+ mary = User.create(name: 'Mary', last_sign_in_at: 1.day.ago, confirmed_at: 2.day.ago)
64
+ john = User.create(name: 'John', last_sign_in_at: 2.week.ago, confirmed_at: 1.month.ago)
65
+
66
+ expect(UserRepository.confirmed).to contain_exactly(john, mary)
67
+ expect(UserRepository.active).to contain_exactly(mary)
68
+ end
69
+
70
+ it 'raises domain method error when directly calls domain methods' do
71
+ expect { UserRepository.alphabetically }.to raise_error(Repositorish::DomainMethodError)
72
+ end
73
+ end
@@ -0,0 +1,93 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ if ENV['CODECLIMATE_REPO_TOKEN']
4
+ require 'codeclimate-test-reporter'
5
+
6
+ CodeClimate::TestReporter.start
7
+ end
8
+
9
+ require 'rspec'
10
+ require 'repositorish'
11
+ Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each { |f| require f }
12
+
13
+ RSpec.configure do |config|
14
+ config.color = true
15
+
16
+ # The settings below are suggested to provide a good initial experience
17
+ # with RSpec, but feel free to customize to your heart's content.
18
+
19
+ # Exclude tests marked as broken
20
+ config.filter_run_excluding :broken
21
+
22
+ # These two settings work together to allow you to limit a spec run
23
+ # to individual examples or groups you care about by tagging them with
24
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
25
+ # get run.
26
+ config.filter_run :focus
27
+ config.run_all_when_everything_filtered = true
28
+
29
+ # Many RSpec users commonly either run the entire suite or an individual
30
+ # file, and it's useful to allow more verbose output when running an
31
+ # individual spec file.
32
+ if config.files_to_run.one?
33
+ # Use the documentation formatter for detailed output,
34
+ # unless a formatter has already been configured
35
+ # (e.g. via a command-line flag).
36
+ config.default_formatter = 'doc'
37
+ end
38
+
39
+ # Print the 10 slowest examples and example groups at the
40
+ # end of the spec run, to help surface which specs are running
41
+ # particularly slow.
42
+ config.profile_examples = 5
43
+
44
+ # Run specs in random order to surface order dependencies. If you find an
45
+ # order dependency and want to debug it, you can fix the order by providing
46
+ # the seed, which is printed after each run.
47
+ # --seed 1234
48
+ config.order = :random
49
+
50
+ # Seed global randomization in this process using the `--seed` CLI option.
51
+ # Setting this allows you to use `--seed` to deterministically reproduce
52
+ # test failures related to randomization by passing the same `--seed` value
53
+ # as the one that triggered the failure.
54
+ Kernel.srand config.seed
55
+
56
+ # rspec-expectations config goes here. You can use an alternate
57
+ # assertion/expectation library such as wrong or the stdlib/minitest
58
+ # assertions if you prefer.
59
+ config.expect_with :rspec do |expectations|
60
+ # Enable only the newer, non-monkey-patching expect syntax.
61
+ # For more details, see:
62
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
63
+ expectations.syntax = :expect
64
+ end
65
+
66
+ # Disable RSpec monkey patches
67
+ #
68
+ # - It force to use `RSpec.describe` at the top level spec
69
+ config.disable_monkey_patching!
70
+
71
+ # rspec-mocks config goes here. You can use an alternate test double
72
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
73
+ config.mock_with :rspec do |mocks|
74
+ # Enable only the newer, non-monkey-patching expect syntax.
75
+ # For more details, see:
76
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
77
+ mocks.syntax = :expect
78
+
79
+ # Prevents you from mocking or stubbing a method that does not exist on
80
+ # a real object. This is generally recommended.
81
+ mocks.verify_partial_doubles = true
82
+ end
83
+
84
+ config.example_status_persistence_file_path = '.rspec_persistence'
85
+
86
+ config.alias_it_should_behave_like_to :it_has_behavior, 'has behavior:'
87
+
88
+ # Turn on aggregate_failures by default, if you don't want it specify `aggregate_failures: false`
89
+ # into your example
90
+ config.define_derived_metadata do |meta|
91
+ meta[:aggregate_failures] = true unless meta.key?(:aggregate_failures)
92
+ end
93
+ end
@@ -0,0 +1,137 @@
1
+ require 'spec_helper'
2
+ require 'ostruct'
3
+
4
+ RSpec.describe Repositorish do
5
+ before(:all) do
6
+ class Model
7
+ def new_record?; end
8
+
9
+ def persisted?
10
+ !new_record?
11
+ end
12
+
13
+ def destroy
14
+ self
15
+ end
16
+
17
+ def save(*_args, &_block)
18
+ true
19
+ end
20
+
21
+ def self.all
22
+ OpenStruct.new(all: true)
23
+ end
24
+
25
+ def self.where(options)
26
+ OpenStruct.new(where: options)
27
+ end
28
+ end
29
+ end
30
+
31
+ subject(:repository_class) do
32
+ Class.new do
33
+ def custom_scope
34
+ OpenStruct.new(custom_scope: true)
35
+ end
36
+ end.include(described_class)
37
+ end
38
+
39
+ it "delegates calls on class to instance's defined domains" do
40
+ repository_class.repositorish :model, scope: :all
41
+ expect(repository_class.custom_scope).to eq(repository_class.new(Model.all).custom_scope)
42
+ end
43
+
44
+ it 'delegates calls on instance to domain' do
45
+ relation = class_double('Model')
46
+ expect(Model).to receive(:all) { relation }
47
+ expect(relation).to receive(:where).with(test: true)
48
+
49
+ repository_class.repositorish :model, scope: :all
50
+ repository = repository_class.query
51
+ repository.where(test: true)
52
+ end
53
+
54
+ describe '.repositorish' do
55
+ subject { repository_class }
56
+
57
+ it 'defines repository domain with the model passed if no option is passed' do
58
+ subject.repositorish :model
59
+ expect(subject.instance_variable_get('@domain')).to eq(Model)
60
+ end
61
+
62
+ it 'defines repository domain with the model passed contextualized in the scope option' do
63
+ subject.repositorish :model, scope: :all
64
+ expect(subject.instance_variable_get('@domain')).to eq(Model.all)
65
+ end
66
+ end
67
+
68
+ describe '.create' do
69
+ subject { repository_class.repositorish(:model, scope: :all) }
70
+
71
+ it 'does not create if the model is already persisted' do
72
+ model = Model.new
73
+ allow(model).to receive(:new_record?) { false }
74
+ expect(model).not_to receive(:save)
75
+ expect(subject.create(model)).to be false
76
+ end
77
+
78
+ it 'creates if the model is a new record' do
79
+ model = Model.new
80
+ allow(model).to receive(:new_record?) { true }
81
+ expect(model).to receive(:save) { true }
82
+ expect(subject.create(model)).to be true
83
+ end
84
+ end
85
+
86
+ describe '.update' do
87
+ subject { repository_class.repositorish(:model, scope: :all) }
88
+
89
+ it 'does not update if the model is a new record' do
90
+ model = Model.new
91
+ allow(model).to receive(:new_record?) { true }
92
+ expect(model).not_to receive(:save)
93
+ expect(subject.update(model)).to be false
94
+ end
95
+
96
+ it 'updates if the model is already persisted' do
97
+ model = Model.new
98
+ allow(model).to receive(:new_record?) { false }
99
+ expect(model).to receive(:save) { true }
100
+ expect(subject.update(model)).to be true
101
+ end
102
+ end
103
+
104
+ describe '.destroy' do
105
+ subject { repository_class.repositorish(:model, scope: :all) }
106
+
107
+ it 'destroys if the model is already persisted' do
108
+ model = Model.new
109
+ allow(model).to receive(:new_record?) { false }
110
+ expect(model).to receive(:destroy) { model }
111
+ expect(subject.destroy(model)).to eq(model)
112
+ end
113
+
114
+ it 'does not destroy if the model is a new record' do
115
+ model = Model.new
116
+ allow(model).to receive(:new_record?) { true }
117
+ expect(model).not_to receive(:destroy)
118
+ expect(subject.destroy(model)).to be_nil
119
+ end
120
+ end
121
+
122
+ describe '.query' do
123
+ subject { repository_class.repositorish(:model, scope: :all) }
124
+
125
+ it 'returns a repositorish instance with configured domain' do
126
+ query = subject.query
127
+ expect(query).to be_kind_of(Repositorish)
128
+ expect(query.instance_variable_get('@domain')).to eq(Model.all)
129
+ end
130
+
131
+ it 'returns a repositorish instance with custom domain passed as argument' do
132
+ query = subject.query(Model.where(test: true))
133
+ expect(query).to be_kind_of(Repositorish)
134
+ expect(query.instance_variable_get('@domain')).to eq(Model.where(test: true))
135
+ end
136
+ end
137
+ end
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: repositorish
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Glauber Campinho
8
+ - Brenno Costa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-11-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ! '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: activerecord
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: byebug
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: codeclimate-test-reporter
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rake
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ~>
89
+ - !ruby/object:Gem::Version
90
+ version: 3.3.0
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ~>
96
+ - !ruby/object:Gem::Version
97
+ version: 3.3.0
98
+ - !ruby/object:Gem::Dependency
99
+ name: rubocop
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: sqlite3
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description:
127
+ email:
128
+ - ggcampinho@gmail.com
129
+ - brennolncosta@gmail.com
130
+ executables: []
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - .codeclimate.yml
135
+ - .gitignore
136
+ - .rubocop.yml
137
+ - .travis.yml
138
+ - CODE_OF_CONDUCT.md
139
+ - Gemfile
140
+ - LICENSE
141
+ - README.md
142
+ - Rakefile
143
+ - lib/repositorish.rb
144
+ - lib/repositorish/version.rb
145
+ - repositorish.gemspec
146
+ - spec/integration/active_record_spec.rb
147
+ - spec/spec_helper.rb
148
+ - spec/warehouse_spec.rb
149
+ homepage: https://github.com/LoveMondays/repositorish
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.4.5
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: Simple Repository(ish) solution to hold query and command logic into self
173
+ contained objects
174
+ test_files: []