kushojin 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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b284f87c77853b11396c7b90b28ba80a237f19af
4
+ data.tar.gz: 29dfb5f5842aeb0735ce95c89ccd668da388c00f
5
+ SHA512:
6
+ metadata.gz: da3383afe02b3227e110530c38cf3c70a0b37689f9bf1a2f96f910c621cebecbe528d2cf18ef9cdfe3cb9ce6401677a6a0da1bc15c78ba4e38d6ee568cda14dd
7
+ data.tar.gz: 3a7dad0856810d4069aa539c403bc9340f90f46aeced4818b84ebb0e5d45c3fdda1be60dbf1984a403ba974e6a51b674c7792a664cff5dffe9dd4a4cd1ee049d
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,30 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+ DisplayCopNames: true
4
+ Exclude:
5
+ - 'kushojin.gemspec'
6
+
7
+ Style/Documentation:
8
+ Enabled: false
9
+
10
+ Style/FrozenStringLiteralComment:
11
+ EnforcedStyle: never
12
+
13
+ Style/StringLiterals:
14
+ EnforcedStyle: double_quotes
15
+
16
+ Style/TrailingCommaInArguments:
17
+ EnforcedStyleForMultiline: comma
18
+
19
+ Style/TrailingCommaInLiteral:
20
+ EnforcedStyleForMultiline: comma
21
+
22
+ Layout/MultilineMethodCallIndentation:
23
+ EnforcedStyle: indented
24
+
25
+ Metrics/BlockLength:
26
+ Exclude:
27
+ - 'spec/**/*'
28
+
29
+ Metrics/LineLength:
30
+ Max: 120
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in kushojin.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 TODO: Write your name
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ # Kushojin [![Build Status](https://travis-ci.org/nowlinuxing/kushojin.svg?branch=master)](https://travis-ci.org/nowlinuxing/kushojin.svg?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/33c293ed9b4f9f25ab2c/maintainability)](https://codeclimate.com/github/nowlinuxing/kushojin/maintainability)
2
+
3
+ Kushojin aims to record changed attributes of ActiveRecord model.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'kushojin'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install kushojin
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ Kushojin::Config.logger = Fluent::Logger::FluentLogger.new(nil, host: "localhost", port: 24224)
25
+
26
+ class User < ApplicationRecord
27
+ record_changes
28
+ end
29
+
30
+ class UsersController < ApplicationController
31
+ send_changes
32
+
33
+ def create
34
+ User.create(params[:user])
35
+ end
36
+ end
37
+ ```
38
+
39
+ $ curl -X POST -d "user[name]=bill&user[age]=20" http://localhost:3000/users
40
+ # output: users.create {"event":"create","request_id":"4afd0731-dd25-4668-b769-2017dbdd3642","table_name":"users","id":1,"changes":{"name":[null,"bill"],"age":[null,20]}}
41
+
42
+ ## Development
43
+
44
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
45
+
46
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nowlinuxing/kushojin.
51
+
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
56
+
@@ -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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kushojin"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kushojin/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kushojin"
8
+ spec.version = Kushojin::VERSION
9
+ spec.authors = ["Loose coupling"]
10
+ spec.email = ["loosecplg@gmail.com"]
11
+
12
+ spec.summary = %q{Detect changes of model attributes and send to outside.}
13
+ spec.description = %q{Detect changes of model attributes and send to outside.}
14
+ spec.homepage = "https://github.com/nowlinuxing/kushojin/"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "activerecord", "~> 5.1"
25
+ spec.add_dependency "fluent-logger"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.14"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency "sqlite3", "~> 1.3"
31
+ spec.add_development_dependency "pry-byebug"
32
+ spec.add_development_dependency "rubocop"
33
+ end
@@ -0,0 +1,13 @@
1
+ require "active_record"
2
+ require "kushojin/version"
3
+ require "kushojin/config"
4
+ require "kushojin/model_methods"
5
+ require "kushojin/recorder"
6
+ require "kushojin/model_methods"
7
+ require "kushojin/sender"
8
+ require "kushojin/controller_methods" if defined?(AbstractController)
9
+
10
+ module Kushojin
11
+ end
12
+
13
+ ActiveRecord::Base.extend Kushojin::ModelMethods::Callback::ClassMethods
@@ -0,0 +1,12 @@
1
+ require "fluent-logger"
2
+ require "kushojin/sender"
3
+
4
+ module Kushojin
5
+ module Config
6
+ mattr_accessor :logger
7
+ self.logger = Fluent::Logger::ConsoleLogger.open(STDOUT)
8
+
9
+ mattr_accessor :sender
10
+ self.sender = Sender::EachSender.new(logger)
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ require "kushojin/controller_methods/filter"
2
+ require "kushojin/controller_methods/send_change_filter"
3
+
4
+ ActionController::Metal.extend Kushojin::ControllerMethods::Filter
@@ -0,0 +1,10 @@
1
+ module Kushojin
2
+ module ControllerMethods
3
+ module Filter
4
+ def send_changes(**args)
5
+ filter = args.delete(:filter) || Kushojin::ControllerMethods::SendChangeFilter.new
6
+ around_action filter, args
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ module Kushojin
2
+ module ControllerMethods
3
+ class SendChangeFilter
4
+ def initialize(sender: nil)
5
+ @sender = sender || Config.sender
6
+ end
7
+
8
+ def around(controller)
9
+ Recorder.build
10
+ yield
11
+ @sender.send(Recorder.changes, controller: controller)
12
+ Recorder.destroy
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ require "kushojin/model_methods/change"
2
+ require "kushojin/model_methods/callback"
3
+ require "kushojin/model_methods/record_changes_callbacks"
@@ -0,0 +1,16 @@
1
+ module Kushojin
2
+ module ModelMethods
3
+ module Callback
4
+ RECORD_EVENTS = %i[before_destroy after_create after_update].freeze
5
+
6
+ module ClassMethods
7
+ def record_changes
8
+ kushojin_callbacks = RecordChangesCallbacks.new
9
+ RECORD_EVENTS.each do |event|
10
+ public_send(event, kushojin_callbacks)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module Kushojin
2
+ module ModelMethods
3
+ class Change
4
+ attr_reader :event, :model, :changes
5
+
6
+ delegate :primary_key, :table_name, to: "@model.class"
7
+
8
+ def initialize(event, model)
9
+ @event = event
10
+ @model = model.class.new(model.attributes).freeze
11
+ @changes = model.saved_changes.freeze
12
+ end
13
+
14
+ def changes_without_primary_key
15
+ @changes.reject { |attr_name, _| attr_name == primary_key }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ module Kushojin
2
+ module ModelMethods
3
+ class RecordChangesCallbacks
4
+ Callback::RECORD_EVENTS.each do |event|
5
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
6
+ def #{event}(model) # def after_create
7
+ Recorder.record(:#{event.to_s.split(/_/).last}, model) if Recorder.current # Recorder.record(:create, model) if Recorder.current
8
+ end # end
9
+ RUBY
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ module Kushojin
2
+ class Recorder
3
+ THREAD_KEY = "kushojin.recorder".freeze
4
+
5
+ attr_reader :changes
6
+
7
+ (class << self; self; end).delegate :changes, :record, to: :current
8
+
9
+ def self.build
10
+ Thread.current[THREAD_KEY] = new
11
+ end
12
+
13
+ def self.current
14
+ Thread.current[THREAD_KEY]
15
+ end
16
+
17
+ def self.destroy
18
+ Thread.current[THREAD_KEY] = nil
19
+ end
20
+
21
+ def initialize
22
+ @changes = []
23
+ end
24
+
25
+ def record(event, model)
26
+ @changes << ModelMethods::Change.new(event, model)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ require "kushojin/sender/base"
2
+ require "kushojin/sender/each_sender"
3
+ require "kushojin/sender/serializer"
@@ -0,0 +1,13 @@
1
+ module Kushojin
2
+ module Sender
3
+ class Base
4
+ def initialize(logger, **_args)
5
+ @logger = logger
6
+ end
7
+
8
+ def send(_changes, **_args)
9
+ raise NotImplementedError
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,33 @@
1
+ module Kushojin
2
+ module Sender
3
+ class EachSender < Base
4
+ def initialize(logger, serializer: Serializer::SimpleSerializer)
5
+ super
6
+ @serializer = serializer
7
+ end
8
+
9
+ def send(changes, controller:)
10
+ changes.each do |change|
11
+ @logger.post(tag(controller), @serializer.serialize(change, controller: controller))
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def tag(controller)
18
+ "#{controller.controller_name}.#{controller.action_name}"
19
+ end
20
+
21
+ def map(change, controller:)
22
+ changes = change.changes_without_primary_key.reject { |attr_name, _| ignore_columns.include?(attr_name) }
23
+ {
24
+ "event" => change.event.to_s,
25
+ "request_id" => controller.request.request_id,
26
+ "table_name" => change.table_name,
27
+ change.primary_key => change.model.id,
28
+ "changes" => changes,
29
+ }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,2 @@
1
+ require "kushojin/sender/serializer/base"
2
+ require "kushojin/sender/serializer/simple_serializer"
@@ -0,0 +1,21 @@
1
+ module Kushojin
2
+ module Sender
3
+ module Serializer
4
+ class Base
5
+ def self.default_ignore_columns
6
+ c = Class.new
7
+ c.extend(ActiveRecord::Timestamp::ClassMethods)
8
+ c.class_eval { timestamp_attributes_for_create + timestamp_attributes_for_update }
9
+ end
10
+ private_class_method :default_ignore_columns
11
+
12
+ class_attribute :ignore_columns
13
+ self.ignore_columns = default_ignore_columns
14
+
15
+ def self.serialize(_change, **_args)
16
+ raise NotImplementedError
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Kushojin
2
+ module Sender
3
+ module Serializer
4
+ class SimpleSerializer < Base
5
+ def self.serialize(change, controller:)
6
+ {
7
+ "event" => change.event.to_s,
8
+ "request_id" => controller.request.request_id,
9
+ "table_name" => change.table_name,
10
+ change.primary_key => change.model.id,
11
+ "changes" => changes_hash(change),
12
+ }
13
+ end
14
+
15
+ def self.changes_hash(change)
16
+ change.changes_without_primary_key.reject { |attr_name, _| ignore_columns.include?(attr_name) }
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Kushojin
2
+ VERSION = "0.1.1".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kushojin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Loose coupling
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-26 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.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fluent-logger
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.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: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.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: '1.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Detect changes of model attributes and send to outside.
126
+ email:
127
+ - loosecplg@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".rubocop.yml"
135
+ - ".travis.yml"
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - bin/console
141
+ - bin/setup
142
+ - kushojin.gemspec
143
+ - lib/kushojin.rb
144
+ - lib/kushojin/config.rb
145
+ - lib/kushojin/controller_methods.rb
146
+ - lib/kushojin/controller_methods/filter.rb
147
+ - lib/kushojin/controller_methods/send_change_filter.rb
148
+ - lib/kushojin/model_methods.rb
149
+ - lib/kushojin/model_methods/callback.rb
150
+ - lib/kushojin/model_methods/change.rb
151
+ - lib/kushojin/model_methods/record_changes_callbacks.rb
152
+ - lib/kushojin/recorder.rb
153
+ - lib/kushojin/sender.rb
154
+ - lib/kushojin/sender/base.rb
155
+ - lib/kushojin/sender/each_sender.rb
156
+ - lib/kushojin/sender/serializer.rb
157
+ - lib/kushojin/sender/serializer/base.rb
158
+ - lib/kushojin/sender/serializer/simple_serializer.rb
159
+ - lib/kushojin/version.rb
160
+ homepage: https://github.com/nowlinuxing/kushojin/
161
+ licenses:
162
+ - MIT
163
+ metadata: {}
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.6.13
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: Detect changes of model attributes and send to outside.
184
+ test_files: []