Class: FieldMaskedModel::Base
- Inherits:
-
Object
- Object
- FieldMaskedModel::Base
- Defined in:
- lib/field_masked_model/base.rb
Class Attribute Summary collapse
-
.inaccessible_error_callback ⇒ Object
readonly
Returns the value of attribute inaccessible_error_callback.
-
.model_pool ⇒ Object
readonly
Returns the value of attribute model_pool.
-
.msg ⇒ Object
readonly
Returns the value of attribute msg.
Class Method Summary collapse
- .dispatch(entry) ⇒ Symbol
- .entries ⇒ <Google::Protobuf::FieldDescriptor>
- .exclude_field(name) ⇒ Object
- .fields ⇒ <Symbol, { Symbol => Array }>
- .msgclass(klass, exclude_fields: [], model_pool: ModelPool.generated_pool) ⇒ Object
- .set_inaccessible_error_callback(callback) ⇒ Object
Instance Method Summary collapse
-
#initialize(field_mask: nil, field_mask_node: nil, message:) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ String
- #to_h ⇒ Hash
Constructor Details
#initialize(field_mask: nil, field_mask_node: nil, message:) ⇒ Base
Returns a new instance of Base.
151 152 153 154 155 156 157 158 159 |
# File 'lib/field_masked_model/base.rb', line 151 def initialize(field_mask: nil, field_mask_node: nil, message:) if field_mask.nil? && field_mask_node.nil? raise ArgumentError.new("missing keyword: field_mask or field_mask_node") end @fm_node = field_mask_node || FMParser.parse(paths: field_mask.paths, root: self.class.msg) @message = @accessible_fields ||= Set.new(@fm_node.field_names) end |
Class Attribute Details
.inaccessible_error_callback ⇒ Object (readonly)
Returns the value of attribute inaccessible_error_callback.
11 12 13 |
# File 'lib/field_masked_model/base.rb', line 11 def inaccessible_error_callback @inaccessible_error_callback end |
.model_pool ⇒ Object (readonly)
Returns the value of attribute model_pool.
11 12 13 |
# File 'lib/field_masked_model/base.rb', line 11 def model_pool @model_pool end |
.msg ⇒ Object (readonly)
Returns the value of attribute msg.
11 12 13 |
# File 'lib/field_masked_model/base.rb', line 11 def msg @msg end |
Class Method Details
.dispatch(entry) ⇒ Symbol
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/field_masked_model/base.rb', line 76 def dispatch(entry) case entry.type when :message m = @model_pool.lookup(entry.subtype.msgclass) # NOTE: If msgclass is not registered in ModelPool, we treat it # as a one of attribute classes. if m.nil? return :attribute end case entry.label when :repeated :repeated_association else :association end else # eum or scalar :attribute end end |
.entries ⇒ <Google::Protobuf::FieldDescriptor>
68 69 70 71 72 |
# File 'lib/field_masked_model/base.rb', line 68 def entries @msg.descriptor.entries.select do |e| !_excluded_fields.include?(e.name) end end |
.exclude_field(name) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/field_masked_model/base.rb', line 33 def exclude_field(name) if defined?(@msg) Kernel.warn("exclude_field must be called before msgclass", uplevel: 1) end _excluded_fields << name.to_s end |
.fields ⇒ <Symbol, { Symbol => Array }>
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/field_masked_model/base.rb', line 46 def fields r = [] children = {} self.entries.each do |entry| n = entry.name.to_sym type = self.dispatch(entry) case type when :attribute r << n when :association modelclass = @model_pool.lookup(entry.subtype.msgclass) children[n] = modelclass.fields when :repeated_association modelclass = @model_pool.lookup(entry.subtype.msgclass) children[n] = modelclass.fields end end r << children if children.size > 0 r end |
.msgclass(klass, exclude_fields: [], model_pool: ModelPool.generated_pool) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/field_masked_model/base.rb', line 16 def msgclass(klass, exclude_fields: [], model_pool: ModelPool.generated_pool) if defined?(@msg) raise "msgclass is already registered!" end @model_pool = model_pool @model_pool.add(klass, self) exclude_fields.each do |field| exclude_field field end @msg = klass define_accessors! end |
.set_inaccessible_error_callback(callback) ⇒ Object
41 42 43 |
# File 'lib/field_masked_model/base.rb', line 41 def set_inaccessible_error_callback(callback) @inaccessible_error_callback = callback end |
Instance Method Details
#inspect ⇒ String
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/field_masked_model/base.rb', line 187 def inspect h = {} self.class.entries.each do |entry| n = entry.name.to_sym if !@accessible_fields.include?(n) h[n] = "-" next end v = self.send(n) type = self.class.dispatch(entry) case type when :attribute case v when NilClass h[n] = "nil" when String h[n] = "\"#{v}\"" else h[n] = v end when :association case v when NilClass h[n] = "nil" else h[n] = v.class.name.split("::").last end when :repeated_association if v.size > 0 h[n] = "[#{v[0].class.name.split("::").last}]" else h[n] = "[]" end end end "<#{self.class.name}#{h.map { |k, v| "\n #{k}: #{v}" }.join(',')}>" end |
#to_h ⇒ Hash
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/field_masked_model/base.rb', line 162 def to_h r = {} self.class.entries.each do |entry| n = entry.name.to_sym next if !@accessible_fields.include?(n) v = self.send(n) type = self.class.dispatch(entry) case type when :attribute r[n] = v when :association if v.nil? r[n] = nil else r[n] = v.to_h end when :repeated_association r[n] = v.map(&:to_h) end end r end |