kagetsu 1.0.0

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,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 98c696c7aadabd5a7c7e8245d62cf7979f2d3455e40a12087527974c47882f6c
4
+ data.tar.gz: 7ccda21865064be051c172869484a8f4e70524c018d97a2797c3c0cccc74bfed
5
+ SHA512:
6
+ metadata.gz: adf44a6f3a60bbdd950b035f8fd17e52cb51c21ab05d3f164261280451539fc8272360b3ebfcd22cd9aa4369f63906c57a96657e4ea8e4c3afd617aebd1e8471
7
+ data.tar.gz: 12230ffbd87f9f552eb135aec85b2d06253b794ec158f816485c3a52f8472bf94aa194a41f07b8c2addb17aafbb32f7b6de1c5c9354461734bc8673585b0153a
@@ -0,0 +1,11 @@
1
+ version: 2
2
+ jobs:
3
+ build:
4
+ docker:
5
+ - image: circleci/ruby:2.5.1-node
6
+ steps:
7
+ - checkout
8
+ - run: bundle install
9
+
10
+ - type: shell
11
+ command: bundle exec rake spec
@@ -0,0 +1,3 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format FoodFormatter
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in Kagetsu.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2018 taki
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,21 @@
1
+ # 花月(Kagetsu) [![CircleCI](https://circleci.com/gh/taki/kagetsu.svg?style=svg)](https://circleci.com/gh/taki/kagetsu)
2
+
3
+ kagetsu is a library for create or update method on ActiveRecord.
4
+
5
+ ## Requirement
6
+
7
+ - Rails 5.2 or upper
8
+
9
+ ## Usage
10
+
11
+ ```ruby
12
+ User.create!(name: 'kotoko', email: 'old@example.com')
13
+ user = User.create_or_update(key: { name: 'kotoko' }, update_attributes: { email: 'new@example.com' })
14
+
15
+ user.email #=> new@example.com
16
+ ```
17
+
18
+ ## Available Methods
19
+
20
+ - ActiveRecord::Base#create_or_update
21
+ - ActiveRecord::Base#create_or_update!
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'kagetsu/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'kagetsu'
7
+ spec.version = Kagetsu::VERSION
8
+ spec.authors = ['taki']
9
+ spec.summary = 'Implement ActiveRecord::Base#create_or_update'
10
+ spec.description = 'Protect your record easily'
11
+ spec.homepage = 'http://github.com/taki/kagetsu'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_dependency 'activerecord', '~> 5.2'
20
+ spec.add_development_dependency 'bundler'
21
+ spec.add_development_dependency 'food_formatter'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'sqlite3'
25
+ end
@@ -0,0 +1,17 @@
1
+ require 'kagetsu/version'
2
+ require 'active_record'
3
+
4
+ module Kagetsu
5
+ def create_or_update(key:, update_attributes:)
6
+ create_or_update!(key: key, update_attributes: update_attributes)
7
+ rescue => ex
8
+ false
9
+ end
10
+
11
+ def create_or_update!(key:, update_attributes:)
12
+ object = find_or_initialize_by(key)
13
+ object.update(update_attributes)
14
+ end
15
+ end
16
+
17
+ ActiveRecord::Base.extend Kagetsu
@@ -0,0 +1,3 @@
1
+ module Kagetsu
2
+ VERSION = '1.0.0'.freeze
3
+ end
@@ -0,0 +1,28 @@
1
+ class User < ActiveRecord::Base
2
+ end
3
+
4
+ RSpec.describe Kagetsu do
5
+ after { User.delete_all }
6
+
7
+ describe '.create_or_update' do
8
+ context 'when user do not exist' do
9
+ specify do
10
+ expect { User.create_or_update(key: { name: 'kotoko' }, update_attributes: { email: 'kotoko@example.com' }) }
11
+ .to change { User.count }
12
+ .from(0)
13
+ .to(1)
14
+ end
15
+ end
16
+
17
+ context 'when user exist' do
18
+ let!(:user) { User.create!(name: 'kotoko', email: 'old@example.com') }
19
+
20
+ specify 'email is changed' do
21
+ expect { User.create_or_update(key: { name: 'kotoko' }, update_attributes: { email: 'new@example.com' }) }
22
+ .to change { user.reload.email }
23
+ .from('old@example.com')
24
+ .to('new@example.com')
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,4 @@
1
+ require 'bundler/setup'
2
+ Bundler.require
3
+
4
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
2
+
3
+ ActiveRecord::Schema.define do
4
+ create_table(:users) do |t|
5
+ t.string :name
6
+ t.string :email
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kagetsu
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - taki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: food_formatter
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Protect your record easily
98
+ email:
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - ".circleci/config.yml"
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - kienaide.gemspec
111
+ - lib/kagetsu.rb
112
+ - lib/kagetsu/version.rb
113
+ - spec/kagetsu_spec.rb
114
+ - spec/spec_helper.rb
115
+ - spec/support/setup_database.rb
116
+ homepage: http://github.com/taki/kagetsu
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.7.7
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Implement ActiveRecord::Base#create_or_update
140
+ test_files:
141
+ - spec/kagetsu_spec.rb
142
+ - spec/spec_helper.rb
143
+ - spec/support/setup_database.rb