Class: SublimeDSL::SublimeText::KeyMap::Context::Condition

Inherits:
Object
  • Object
show all
Includes:
Tools::ValueEquality
Defined in:
lib/sublime_dsl/sublime_text/keymap.rb

Overview

A condition. There are 3 types of conditions:

  • left_operand operator right_operand

  • left_operand right_operand

  • left_operand

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Tools::ValueEquality

#eql?, #hash

Constructor Details

#initialize(left, operator, right, match_all) ⇒ Condition

Returns a new instance of Condition.



290
291
292
293
294
295
296
297
298
299
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 290

def initialize(left, operator, right, match_all)
  @left = left
  @operator = operator && operator.to_sym
  if operator && operator =~ /regex/
    @right = Tools::RegexpWannabe.new(right)
  else
    @right = right
  end
  @match_all = match_all ? true : nil   # normalize match_all
end

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.



288
289
290
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 288

def left
  @left
end

#match_allObject (readonly)

Returns the value of attribute match_all.



288
289
290
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 288

def match_all
  @match_all
end

#operatorObject (readonly)

Returns the value of attribute operator.



288
289
290
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 288

def operator
  @operator
end

#rightObject (readonly)

Returns the value of attribute right.



288
289
290
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 288

def right
  @right
end

Class Method Details

.from_dsl(args) ⇒ Object

Returns a new condition from an array of captures.



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 241

def self.from_dsl(args)
  passed = args.dup
  match_all = args.first == :all
  if match_all
    args.shift
    args.empty? and raise Error, "'all' is not a valid condition"
  else
    args.empty? and raise Error, "condition missing"
  end
  left = args.shift.to_s
  if left == 'setting' && !args.empty?
    left = 'setting.' << args.shift.to_s
  end
  case args.length
  when 0
    op = nil
    right = nil
  when 2
    case args.first
    when :'=='
      if args.last.is_a?(Symbol)
        op = nil
        args[-1] = args.last.to_s
      else
        op = 'equal'
      end
    when :'!='
      op = 'not_equal'
    when :is
      op = nil
    when :'=~'
      op = 'regex_contains'
    when :'!~'
      op = 'not_regex_contains'
    when :regex_match, :not_regex_match
      op = args.first
    else
      raise Error, "invalid operator: #{args.first.inspect}"
    end
    right = args.last
    right = right.source if right.is_a?(Regexp)
  else
    raise Error, "expected [all.]<setting> [operator] [value]: #{passed.map(&:to_s).join(' ')}"
  end
  new(left, op, right, match_all)
end

Instance Method Details

#fixmesObject



301
302
303
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 301

def fixmes
  right && right.is_a?(Tools::RegexpWannabe) ? right.fixmes : []
end

#to_dslObject Also known as: to_s

DSL for the condition. The 3 types are rendered as:

left operator right

Same condition, with operator mapped to its ruby equivalent:

equal

==

not_equal

!=

regex_contains

=~

not_regex_contains

!~

regex_match

.regex_match + comment

not_regex_match

.not_regex_match + comment

The comment is ‘could use =~ with A and z’

left right
  • if right is a String: left == :right

  • otherwise: left is right

left

left

If #match_all is true, left is prefixed by “all.”.



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 326

def to_dsl
  ( match_all ? 'all.' : '' ) <<
  left << (
    case operator
    when nil
      if right.nil?
        ''
      elsif right.is_a?(String)
        ' == ' + right.to_sym.inspect
      else
        ' is ' << right.inspect
      end
    when :equal
      ' == ' << right.inspect
    when :not_equal
      ' != ' << right.inspect
    when :regex_contains
      ' =~ ' << right.inspect
    when :not_regex_contains
      ' !~ ' << right.inspect
    when :regex_match
      ' .regex_match ' << right.inspect(true) << ' # could use =~ with \A and \z'
    when :not_regex_match
      ' .not_regex_match ' << right.inspect(true) << ' # could use !~ with \A and \z'
    else
      raise Error, "unknown operator: #{operator.inspect} #{right.to_s}"
    end
  )
end

#to_hObject



356
357
358
359
360
361
362
363
364
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 356

def to_h
  h = { 'key' => left }
  h['operator'] = operator if operator
  unless right.nil?
    h['operand'] = right.is_a?(Tools::RegexpWannabe) ? right.to_s(true) : right
  end
  h['match_all'] = match_all if match_all
  h
end

#to_jsonObject



366
367
368
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 366

def to_json
  JSON.generate(to_h)
end

#value_idObject



374
375
376
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 374

def value_id
  to_h
end