Class: Dotenv::Merge::EnvLine

Inherits:
Ast::Merge::AstNode
  • Object
show all
Defined in:
lib/dotenv/merge/env_line.rb

Overview

Represents a single line in a dotenv file.
Parses and categorizes lines as assignments, comments, blank lines, or invalid.

Inherits from Ast::Merge::AstNode for a normalized API across all ast-merge
content nodes. This provides TreeHaver::Node protocol compatibility including
#slice, #location, #unwrap, #type, #text, and other standard methods.

Dotenv files follow a simple format where each line is one of:

  • KEY=value - Environment variable assignment
  • export KEY=value - Assignment with export prefix
  • # comment - Comment line
  • Empty/whitespace - Blank line

Examples:

Parse a simple assignment

line = EnvLine.new("API_KEY=secret123", 1)
line.assignment? # => true
line.key         # => "API_KEY"
line.value       # => "secret123"

Parse an export statement

line = EnvLine.new("export DATABASE_URL=postgres://localhost/db", 2)
line.assignment? # => true
line.export?     # => true
line.key         # => "DATABASE_URL"

Parse a comment

line = EnvLine.new("# Database configuration", 3)
line.comment?    # => true
line.comment     # => "# Database configuration"

Quoted values with escape sequences

line = EnvLine.new('MESSAGE="Hello\nWorld"', 4)
line.value       # => "Hello\nWorld" (with actual newline)

Constant Summary collapse

EXPORT_PREFIX =

Prefix for exported environment variables

Returns:

  • (String)
"export "

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, line_number) ⇒ EnvLine

Initialize a new EnvLine by parsing the raw content

Parameters:

  • raw (String)

    The raw line content from the dotenv file

  • line_number (Integer)

    The 1-indexed line number



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dotenv/merge/env_line.rb', line 65

def initialize(raw, line_number)
  @raw = raw
  @line_number = line_number
  @line_type = nil
  @key = nil
  @value = nil
  @export = false
  parse!

  location = Ast::Merge::AstNode::Location.new(
    start_line: line_number,
    end_line: line_number,
    start_column: 0,
    end_column: @raw.length,
  )

  super(slice: @raw, location: location)
end

Instance Attribute Details

#exportBoolean (readonly)

Returns Whether the line has an export prefix.

Returns:

  • (Boolean)

    Whether the line has an export prefix



59
60
61
# File 'lib/dotenv/merge/env_line.rb', line 59

def export
  @export
end

#keyString? (readonly)

Returns The environment variable key (for assignments).

Returns:

  • (String, nil)

    The environment variable key (for assignments)



53
54
55
# File 'lib/dotenv/merge/env_line.rb', line 53

def key
  @key
end

#line_numberInteger (readonly)

Returns The 1-indexed line number in the source file.

Returns:

  • (Integer)

    The 1-indexed line number in the source file



47
48
49
# File 'lib/dotenv/merge/env_line.rb', line 47

def line_number
  @line_number
end

#line_typeSymbol? (readonly)

Returns The line type (:assignment, :comment, :blank, :invalid).

Returns:

  • (Symbol, nil)

    The line type (:assignment, :comment, :blank, :invalid)



50
51
52
# File 'lib/dotenv/merge/env_line.rb', line 50

def line_type
  @line_type
end

#rawString (readonly)

Returns The original raw line content.

Returns:

  • (String)

    The original raw line content



44
45
46
# File 'lib/dotenv/merge/env_line.rb', line 44

def raw
  @raw
end

#valueString? (readonly)

Returns The environment variable value (for assignments).

Returns:

  • (String, nil)

    The environment variable value (for assignments)



56
57
58
# File 'lib/dotenv/merge/env_line.rb', line 56

def value
  @value
end

Instance Method Details

#assignment?Boolean

Check if this line is an environment variable assignment

Returns:

  • (Boolean)

    true if the line is a valid KEY=value assignment



102
103
104
# File 'lib/dotenv/merge/env_line.rb', line 102

def assignment?
  @line_type == :assignment
end

#blank?Boolean

Check if this line is blank (empty or whitespace only)

Returns:

  • (Boolean)

    true if the line is blank



116
117
118
# File 'lib/dotenv/merge/env_line.rb', line 116

def blank?
  @line_type == :blank
end

#commentString?

Get the raw comment text (for comment lines only)

Returns:

  • (String, nil)

    The raw line content if this is a comment, nil otherwise



137
138
139
140
141
# File 'lib/dotenv/merge/env_line.rb', line 137

def comment
  return @raw if comment?

  nil
end

#comment?Boolean

Check if this line is a comment

Returns:

  • (Boolean)

    true if the line starts with #



109
110
111
# File 'lib/dotenv/merge/env_line.rb', line 109

def comment?
  @line_type == :comment
end

#export?Boolean

Check if this line has the export prefix

Returns:

  • (Boolean)

    true if the line starts with “export “



130
131
132
# File 'lib/dotenv/merge/env_line.rb', line 130

def export?
  @export
end

#inspectString

Inspect for debugging

Returns:

  • (String)

    A debug representation of this EnvLine



153
154
155
# File 'lib/dotenv/merge/env_line.rb', line 153

def inspect
  "#<#{self.class.name} line=#{@line_number} line_type=#{@line_type} key=#{@key.inspect}>"
end

#invalid?Boolean

Check if this line is invalid (unparseable)

Returns:

  • (Boolean)

    true if the line could not be parsed



123
124
125
# File 'lib/dotenv/merge/env_line.rb', line 123

def invalid?
  @line_type == :invalid
end

#signatureArray<Symbol, String>?

Generate a unique signature for this line (used for merge matching)

Returns:

  • (Array<Symbol, String>, nil)

    Signature array [:env, key] for assignments, nil otherwise



93
94
95
96
97
# File 'lib/dotenv/merge/env_line.rb', line 93

def signature
  return unless @line_type == :assignment

  [:env, @key]
end

#to_sString

Convert to string representation (returns raw content)

Returns:

  • (String)

    The original raw line content



146
147
148
# File 'lib/dotenv/merge/env_line.rb', line 146

def to_s
  @raw
end

#typeString

TreeHaver::Node protocol: type

Returns:

  • (String)

    “env_line”



86
87
88
# File 'lib/dotenv/merge/env_line.rb', line 86

def type
  "env_line"
end