-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor class methods of MaskedDatum
- Avoid using class variables https://github.com/cookpad/styleguide/blob/master/ruby.en.md#variables - But class_attribute with the default value which is calculated using an instance method is a bit difficult - Remove cache and query directly, in order to remove both class variables and class_attributes - The calculation cost will be roughly the same
- Loading branch information
1 parent
0d21f18
commit e50dc2b
Showing
3 changed files
with
23 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,32 @@ | ||
class MaskedDatum < ApplicationRecord | ||
validates :database_name, :table_name, :column_name, presence: true | ||
|
||
after_save :update_data! | ||
ANY_NAME = "*".freeze | ||
|
||
def self.data | ||
@@data ||= update_data! | ||
end | ||
validates :database_name, :table_name, :column_name, presence: true | ||
|
||
def self.update_data! | ||
@@data = all.map(&:pack) | ||
end | ||
scope :masking_database, ->(database_name) { | ||
where(database_name: database_name, table_name: ANY_NAME, column_name: ANY_NAME) | ||
} | ||
scope :masking_table, ->(database_name, table_name) { | ||
masking_database(database_name) | ||
.or(where(database_name: database_name, table_name: table_name, column_name: ANY_NAME)) | ||
.or(where(database_name: ANY_NAME, table_name: table_name, column_name: ANY_NAME)) | ||
} | ||
|
||
def self.masked_database?(database_name) | ||
data.include?("#{database_name}/*/*") | ||
self.masking_database(database_name).exists? | ||
end | ||
|
||
def self.masked_table?(database_name, table_name) | ||
return true if masked_database?(database_name) | ||
data.include?("#{database_name}/#{table_name}/*") | ||
self.masking_table(database_name, table_name).exists? | ||
end | ||
|
||
def self.masked_column?(database_name, table_name, column_name) | ||
return true if masked_database?(database_name) | ||
return true if masked_table?(database_name, table_name) | ||
data.include?("#{database_name}/#{table_name}/#{column_name}") || data.include?("#{database_name}/*/#{column_name}") || data.include?("*/*/#{column_name}") | ||
def self.masked_columns(database_name, table_name) | ||
return [ANY_NAME] if self.masked_table?(database_name, table_name) | ||
|
||
Set.new(self.where(database_name: database_name, table_name: table_name).pluck(:column_name)) | ||
end | ||
|
||
def pack | ||
"#{database_name}/#{table_name}/#{column_name}" | ||
end | ||
|
||
private | ||
|
||
def update_data! | ||
self.class.update_data! | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters