Class: Ibex::Configuration::Origin

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/configuration.rb

Overview

Where a selected configuration value came from.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, location: nil) ⇒ Origin

Returns a new instance of Origin.

Raises:

  • (ArgumentError)


128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ibex/configuration.rb', line 128

def initialize(kind, location: nil)
  raise ArgumentError, "unknown configuration origin: #{kind.inspect}" unless ORIGIN_KINDS.include?(kind)
  unless location.nil? || location.is_a?(Location)
    raise ArgumentError, "configuration location must be an Ibex::Location"
  end
  if location && !%i[grammar project].include?(kind)
    raise ArgumentError, "#{kind} configuration cannot have a source location"
  end

  @kind = kind
  @location = location
  freeze
end

Instance Attribute Details

#kindObject (readonly)

: Symbol



124
125
126
# File 'lib/ibex/configuration.rb', line 124

def kind
  @kind
end

#locationObject (readonly)

: Location?



125
126
127
# File 'lib/ibex/configuration.rb', line 125

def location
  @location
end

Instance Method Details

#labelObject



151
152
153
154
155
156
157
# File 'lib/ibex/configuration.rb', line 151

def label
  location = @location
  return @kind.to_s unless location

  file = location.file || "(source)"
  "#{@kind} #{file}:#{location.line}:#{location.column}"
end

#to_hObject



143
144
145
146
147
148
# File 'lib/ibex/configuration.rb', line 143

def to_h
  result = { "kind" => @kind.to_s } #: Hash[String, json_value]
  location = @location
  result["location"] = stringify_location(location) if location
  result
end