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 #slice, #location, #unwrap, 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



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

def initialize(raw, line_number)
  @raw = raw
  @line_number = line_number
  @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



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

def export
  @export
end

#keyString? (readonly)

Returns The environment variable key (for assignments).

Returns:

  • (String, nil)

    The environment variable key (for assignments)



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

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



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

def line_number
  @line_number
end

#rawString (readonly)

Returns The original raw line content.

Returns:

  • (String)

    The original raw line content



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

def raw
  @raw
end

#typeSymbol? (readonly)

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

Returns:

  • (Symbol, nil)

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



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

def type
  @type
end

#valueString? (readonly)

Returns The environment variable value (for assignments).

Returns:

  • (String, nil)

    The environment variable value (for assignments)



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

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



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

def assignment?
  @type == :assignment
end

#blank?Boolean

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

Returns:

  • (Boolean)

    true if the line is blank



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

def blank?
  @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



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

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 #



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

def comment?
  @type == :comment
end

#export?Boolean

Check if this line has the export prefix

Returns:

  • (Boolean)

    true if the line starts with “export ”



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

def export?
  @export
end

#inspectString

Inspect for debugging

Returns:

  • (String)

    A debug representation of this EnvLine



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

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

#invalid?Boolean

Check if this line is invalid (unparseable)

Returns:

  • (Boolean)

    true if the line could not be parsed



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

def invalid?
  @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



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

def signature
  return unless @type == :assignment

  [:env, @key]
end

#to_sString

Convert to string representation (returns raw content)

Returns:

  • (String)

    The original raw line content



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

def to_s
  @raw
end