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.
-
#heredocs ⇒ String?
readonly
Definitions of the heredocs used in the value, if any.
-
#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, heredocs: nil, &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, heredocs: nil, &block) ⇒ Constant
Creates a new constant definition.
23 24 25 26 27 28 29 |
# File 'lib/parlour/rbi_generator/constant.rb', line 23 def initialize(generator, name: '', value: '', eigen_constant: false, heredocs: nil, &block) super(generator, name) @value = value @heredocs = heredocs @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.
37 38 39 |
# File 'lib/parlour/rbi_generator/constant.rb', line 37 def eigen_constant @eigen_constant end |
#heredocs ⇒ String? (readonly)
Returns Definitions of the heredocs used in the value, if any.
40 41 42 |
# File 'lib/parlour/rbi_generator/constant.rb', line 40 def heredocs @heredocs end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
33 34 35 |
# File 'lib/parlour/rbi_generator/constant.rb', line 33 def value @value end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if this instance is equal to another extend.
48 49 50 51 |
# File 'lib/parlour/rbi_generator/constant.rb', line 48 def ==(other) Constant === other && name == other.name && value == other.value \ && eigen_constant == other.eigen_constant && heredocs == other.heredocs end |
#describe_attrs ⇒ Object
109 110 111 |
# File 'lib/parlour/rbi_generator/constant.rb', line 109 def describe_attrs [:value, :eigen_constant, :heredocs] end |
#generalize_from_rbi! ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/parlour/rbi_generator/constant.rb', line 114 def generalize_from_rbi! if @value.is_a?(String) # There's a good chance this is an untyped constant, so rescue # ParseError and use untyped @value = TypeParser.parse_single_type(@value) rescue Types::Untyped.new end end |
#generate_rbi(indent_level, options) ⇒ Array<String>
Generates the RBI lines for this constant.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/parlour/rbi_generator/constant.rb', line 64 def generate_rbi(indent_level, ) if String === @value [ .indented(indent_level, "#{name} = #{@value}"), ] + [heredocs].compact else [ .indented(indent_level, "#{name} = T.let(nil, #{@value.generate_rbi})"), ] + [heredocs].compact 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.
104 105 106 |
# File 'lib/parlour/rbi_generator/constant.rb', line 104 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.
87 88 89 |
# File 'lib/parlour/rbi_generator/constant.rb', line 87 def mergeable?(others) others.all? { |other| self == other } end |