Class: Parlour::RbiGenerator::Constant
- Inherits:
-
RbiObject
- Object
- TypedObject
- RbiObject
- Parlour::RbiGenerator::Constant
- Defined in:
- lib/parlour/rbi_generator/constant.rb
Overview
Represents a constant definition.
Instance Attribute Summary collapse
-
#eigen_constant ⇒ Boolean
readonly
Whether this constant is defined on the eigenclass of the current namespace.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Attributes inherited from RbiObject
Attributes inherited from TypedObject
#comments, #generated_by, #name
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns true if this instance is equal to another extend.
- #describe_attrs ⇒ Object
- #generalize_from_rbi! ⇒ Object
-
#generate_rbi(indent_level, options) ⇒ Array<String>
Generates the RBI lines for this constant.
-
#initialize(generator, name: '', value: '', eigen_constant: false, &block) ⇒ Constant
constructor
Creates a new constant definition.
-
#merge_into_self(others) ⇒ void
Given an array of Constant instances, merges them into this one.
-
#mergeable?(others) ⇒ Boolean
Given an array of Constant instances, returns true if they may be merged into this instance using #merge_into_self.
Methods inherited from TypedObject
#add_comment, #describe, #describe_tree
Constructor Details
#initialize(generator, name: '', value: '', eigen_constant: false, &block) ⇒ Constant
Creates a new constant definition.
21 22 23 24 25 26 |
# File 'lib/parlour/rbi_generator/constant.rb', line 21 def initialize(generator, name: '', value: '', eigen_constant: false, &block) super(generator, name) @value = value @eigen_constant = eigen_constant yield_self(&block) if block end |
Instance Attribute Details
#eigen_constant ⇒ Boolean (readonly)
Returns Whether this constant is defined on the eigenclass of the current namespace.
34 35 36 |
# File 'lib/parlour/rbi_generator/constant.rb', line 34 def eigen_constant @eigen_constant end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
30 31 32 |
# File 'lib/parlour/rbi_generator/constant.rb', line 30 def value @value end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if this instance is equal to another extend.
42 43 44 45 |
# File 'lib/parlour/rbi_generator/constant.rb', line 42 def ==(other) Constant === other && name == other.name && value == other.value \ && eigen_constant == other.eigen_constant end |
#describe_attrs ⇒ Object
99 100 101 |
# File 'lib/parlour/rbi_generator/constant.rb', line 99 def describe_attrs [:value, :eigen_constant] end |
#generalize_from_rbi! ⇒ Object
104 105 106 107 108 |
# File 'lib/parlour/rbi_generator/constant.rb', line 104 def generalize_from_rbi! # There's a good change this is an untyped constant, so rescue # ParseError and use untyped @value = (TypeParser.parse_single_type(@value) if String === @value) rescue Types::Untyped.new end |
#generate_rbi(indent_level, options) ⇒ Array<String>
Generates the RBI lines for this constant.
58 59 60 61 62 63 64 |
# File 'lib/parlour/rbi_generator/constant.rb', line 58 def generate_rbi(indent_level, ) if String === @value [.indented(indent_level, "#{name} = #{@value}")] else [.indented(indent_level, "#{name} = T.let(nil, #{@value.generate_rbi})")] end end |
#merge_into_self(others) ⇒ void
This method returns an undefined value.
Given an array of Parlour::RbiGenerator::Constant instances, merges them into this one. This particular implementation will simply do nothing, as instances are only mergeable if they are indentical. You MUST ensure that #mergeable? is true for those instances.
94 95 96 |
# File 'lib/parlour/rbi_generator/constant.rb', line 94 def merge_into_self(others) # We don't need to change anything! We only merge identical constants end |
#mergeable?(others) ⇒ Boolean
Given an array of Parlour::RbiGenerator::Constant instances, returns true if they may be merged into this instance using #merge_into_self. This is always false.
77 78 79 |
# File 'lib/parlour/rbi_generator/constant.rb', line 77 def mergeable?(others) others.all? { |other| self == other } end |