提交 44c3fb6e 编写于 作者: A Ahmad Sherif

Add an attributes bag class as a GitalyClient helper

上级 f31fb4cd
module Gitlab
module GitalyClient
# This module expects an `ATTRS` const to be defined on the subclass
# See GitalyClient::WikiFile for an example
module AttributesBag
extend ActiveSupport::Concern
included do
attr_accessor(*const_get(:ATTRS))
end
def initialize(params)
params = params.with_indifferent_access
attributes.each do |attr|
instance_variable_set("@#{attr}", params[attr])
end
end
def ==(other)
attributes.all? do |field|
instance_variable_get("@#{field}") == other.instance_variable_get("@#{field}")
end
end
def attributes
self.class.const_get(:ATTRS)
end
end
end
end
module Gitlab
module GitalyClient
class Diff
FIELDS = %i(from_path to_path old_mode new_mode from_id to_id patch overflow_marker collapsed).freeze
ATTRS = %i(from_path to_path old_mode new_mode from_id to_id patch overflow_marker collapsed).freeze
attr_accessor(*FIELDS)
def initialize(params)
params.each do |key, val|
public_send(:"#{key}=", val) # rubocop:disable GitlabSecurity/PublicSend
end
end
def ==(other)
FIELDS.all? do |field|
public_send(field) == other.public_send(field) # rubocop:disable GitlabSecurity/PublicSend
end
end
include AttributesBag
end
end
end
......@@ -12,7 +12,7 @@ module Gitlab
@rpc_response.each do |diff_msg|
if current_diff.nil?
diff_params = diff_msg.to_h.slice(*GitalyClient::Diff::FIELDS)
diff_params = diff_msg.to_h.slice(*GitalyClient::Diff::ATTRS)
# gRPC uses frozen strings by default, and we need to have an unfrozen string as it
# gets processed further down the line. So we unfreeze the first chunk of the patch
# in case it's the only chunk we receive for this diff.
......
module Gitlab
module GitalyClient
class WikiFile
FIELDS = %i(name mime_type path raw_data).freeze
ATTRS = %i(name mime_type path raw_data).freeze
attr_accessor(*FIELDS)
def initialize(params)
params = params.with_indifferent_access
FIELDS.each do |field|
instance_variable_set("@#{field}", params[field])
end
end
include AttributesBag
end
end
end
module Gitlab
module GitalyClient
class WikiPage
FIELDS = %i(title format url_path path name historical raw_data).freeze
ATTRS = %i(title format url_path path name historical raw_data).freeze
attr_accessor(*FIELDS)
include AttributesBag
def initialize(params)
params = params.with_indifferent_access
FIELDS.each do |field|
instance_variable_set("@#{field}", params[field])
end
super
# All gRPC strings in a response are frozen, so we get an unfrozen
# version here so appending to `raw_data` doesn't blow up.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册