Module: StateMate

Defined in:
lib/state_mate.rb,
lib/state_mate/version.rb,
lib/state_mate/adapters/nvram.rb,
lib/state_mate/adapters/scutil.rb,
lib/state_mate/adapters/defaults.rb,
lib/state_mate/adapters/git_config.rb,
lib/state_mate/adapters/time_machine.rb

Defined Under Namespace

Modules: Adapters, Error Classes: StateSet

Constant Summary collapse

DIRECTIVES =
Set.new [
  'set',
  'unset',
  'array_contains',
  'array_missing',
]
VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.array_contains(key, current, value, options) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/state_mate.rb', line 366

def self.array_contains key, current, value, options
  case current
  when Array
    current + [value]

  when nil
    # it needs to be created
    if options[:create]
      [value]
    else
      raise <<-BLOCK.unblock
        can not ensure #{ key.inspect } contains #{ value.inspect } because
        the key does not exist and options[:create] is not true.
      BLOCK
    end

  else
    # there is something there, but it's not an array. out only option
    # to achieve the declared state is to replace it with a new array
    # where value is the only element, but we don't want to do that unless
    # we've been told to clobber
    if options[:clobber]
      [value]
    else
      raise <<-BLOCK.unblock
        can not ensure #{ key.inspect } contains #{ value.inspect } because
        the value is #{ current.inspect } and options[:clobber] is not true.
      BLOCK
    end
  end # case current
end

.array_contains?(key, current, value, adapter) ⇒ Boolean

Returns:

  • (Boolean)


360
361
362
363
364
# File 'lib/state_mate.rb', line 360

def self.array_contains? key, current, value, adapter
  current.is_a?(Array) && current.any? {|v|
    values_equal? v, value, adapter
  }
end

.array_missing(key, current, value, options) ⇒ Object



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/state_mate.rb', line 404

def self.array_missing key, current, value, options
  case current
  when Array
    current - [value]

  when nil
    # there is no value, only option is to create a new empty array there
    if options[:create]
      []
    else
      raise <<-BLOCK.unblock
        can not ensure #{ key.inspect } missing #{ value.inspect } because
        the key does not exist and options[:create] is not true.
      BLOCK
    end

  else
    # there is something there, but it's not an array. out only option
    # to achieve the declared state is to replace it with a new empty array
    # but we don't want to do that unless we've been told to clobber
    if options[:clobber]
      []
    else
      raise <<-BLOCK.unblock
        can not ensure #{ key.inspect } missing #{ value.inspect } because
        the value is #{ current.inspect } and options[:clobber] is not true.
      BLOCK
    end
  end # case current
end

.array_missing?(key, current, value, adapter) ⇒ Boolean

array_contians

Returns:

  • (Boolean)


398
399
400
401
402
# File 'lib/state_mate.rb', line 398

def self.array_missing? key, current, value, adapter
  current.is_a?(Array) && !current.any? {|v|
    values_equal? v, value, adapter
  }
end

.cast(type_name, value) ⇒ Object

pure

casts a value to a type, or raises an error if not possible. this is useful because ansible in particular likes to pass things as strings.

Parameters:

  • type_name (String)

    the 'name' of the type to cast to.



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/state_mate.rb', line 297

def self.cast type_name, value
  case type_name
  when 'string', 'str'
    value.to_s
  when 'integer', 'int'
    value.to_i
  when 'float'
    value.to_f
  when 'boolean', 'bool'
    if value.to_s.downcase == 'true'
      true
    elsif value.to_s.downcase == 'false'
      false
    else
      raise ArgumentError.new "can't cast to boolean: #{ value.inspect }"
    end
  else 
    raise ArgumentError.new "bad type name: #{ type_name.inspect }"
  end
end

.execute(spec) ⇒ Object



329
330
331
# File 'lib/state_mate.rb', line 329

def self.execute spec
  StateSet.from_spec(spec).execute
end

.get_adapter(adapter_name) ⇒ Object



318
319
320
321
322
323
324
325
326
327
# File 'lib/state_mate.rb', line 318

def self.get_adapter adapter_name
  begin
    require "state_mate/adapters/#{ adapter_name }"
    StateMate::Adapters.constants.find {|sym|
      sym.to_s.downcase == adapter_name.gsub('_', '')
    }.pipe {|sym| StateMate::Adapters.const_get sym}
  rescue Exception => e
    raise "can't find adapter #{ adapter_name.inspect }: #{ e }"
  end
end

.set(key, current, value, options) ⇒ Object



345
346
347
348
# File 'lib/state_mate.rb', line 345

def self.set key, current, value, options
  # TODO: handle options
  value
end

.set?(key, current, value, adapter) ⇒ Boolean

Returns:

  • (Boolean)


341
342
343
# File 'lib/state_mate.rb', line 341

def self.set? key, current, value, adapter
  values_equal? current, value, adapter
end

.unset(key, current, value, options) ⇒ Object



354
355
356
357
358
# File 'lib/state_mate.rb', line 354

def self.unset key, current, value, options
  # TODO: handle options
  raise "value most be nil to unset" unless value.nil?
  nil
end

.unset?(key, current, value, adapter) ⇒ Boolean

Returns:

  • (Boolean)


350
351
352
# File 'lib/state_mate.rb', line 350

def self.unset? key, current, value, adapter
  current.nil?
end

.values_equal?(current, desired, adapter) ⇒ Boolean

Returns:

  • (Boolean)


333
334
335
336
337
338
339
# File 'lib/state_mate.rb', line 333

def self.values_equal? current, desired, adapter
  if adapter.respond_to? :values_equal?
    adapter.values_equal? current, desired
  else
    current == desired
  end
end