Class: Ibex::Configuration::Origin

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

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.

RBS:

  • (Symbol kind, ?location: Location?) -> void

Parameters:

  • kind (Symbol)
  • location: (Location, nil) (defaults to: nil)


119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ibex/configuration.rb', line 119

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

#kindSymbol (readonly)

Signature:

  • Symbol

Returns:

  • (Symbol)


115
116
117
# File 'lib/ibex/configuration.rb', line 115

def kind
  @kind
end

#locationLocation? (readonly)

Signature:

  • Location?

Returns:



116
117
118
# File 'lib/ibex/configuration.rb', line 116

def location
  @location
end

Instance Method Details

#labelString

RBS:

  • () -> String

Returns:

  • (String)


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

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

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

#stringify_location(location) ⇒ Hash[String, json_value]

RBS:

  • (Location location) -> Hash[String, json_value]

Parameters:

Returns:

  • (Hash[String, json_value])


153
154
155
# File 'lib/ibex/configuration.rb', line 153

def stringify_location(location)
  location.to_h.transform_keys(&:to_s)
end

#to_hHash[String, json_value]

RBS:

  • () -> Hash[String, json_value]

Returns:

  • (Hash[String, json_value])


134
135
136
137
138
139
# File 'lib/ibex/configuration.rb', line 134

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