Skip to content

Commit

Permalink
move dynamic rule checks to the initialize method
Browse files Browse the repository at this point in the history
  • Loading branch information
abrandoned committed Mar 6, 2017
1 parent 40e3c61 commit 611a9ca
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/protobuf/field/base_field.rb
Expand Up @@ -47,6 +47,13 @@ def initialize(message_class, rule, type_class, fully_qualified_name, tag, simpl
set_option(option_name, value)
end

@extension = options.key?(:extension)
@deprecated = options.key?(:deprecated)
@required = rule == :required
@repeated = rule == :repeated
@optional = rule == :optional
@packed = @repeated && options.key?(:packed)

validate_packed_field if packed?
define_accessor(simple_name, fully_qualified_name) if simple_name
tag_encoded
Expand Down Expand Up @@ -83,7 +90,7 @@ def default_value
end

def deprecated?
options.key?(:deprecated)
@deprecated
end

def encode(_value)
Expand All @@ -95,7 +102,7 @@ def encode_to_stream(value, stream)
end

def extension?
options.key?(:extension)
@extension
end

def enum?
Expand All @@ -107,23 +114,23 @@ def message?
end

def optional?
rule == :optional
@optional
end

def packed?
repeated? && options.key?(:packed)
@packed
end

def repeated?
rule == :repeated
@repeated
end

def repeated_message?
repeated? && message?
end

def required?
rule == :required
@required
end

# FIXME: need to cleanup (rename) this warthog of a method.
Expand Down

0 comments on commit 611a9ca

Please sign in to comment.