Class: OsCtld::Assets::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/osctld/assets/base.rb

Direct Known Subclasses

BaseFile, CgroupDeviceList, CgroupProgram, Dataset, Entry

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, opts, &block) ⇒ Base

Returns a new instance of Base.

Parameters:

  • path (String)
  • opts (Hash)

    asset-specific options

Options Hash (opts):

  • desc (String)

    description

  • validate_if (Proc)


18
19
20
21
22
23
24
25
26
27
# File 'lib/osctld/assets/base.rb', line 18

def initialize(path, opts, &block)
  @path = path
  @opts = opts
  @block_validators = []
  @errors = []
  @valid = nil
  @state = nil

  block.call(self) if block
end

Class Attribute Details

.typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/osctld/assets/base.rb', line 9

def type
  @type
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



12
13
14
# File 'lib/osctld/assets/base.rb', line 12

def errors
  @errors
end

#optsObject (readonly)

Returns the value of attribute opts.



12
13
14
# File 'lib/osctld/assets/base.rb', line 12

def opts
  @opts
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/osctld/assets/base.rb', line 12

def path
  @path
end

Class Method Details

.register(type) ⇒ Object



3
4
5
6
# File 'lib/osctld/assets/base.rb', line 3

def self.register(type)
  @type = type
  Assets.register(type, self)
end

Instance Method Details

#add_error(error) ⇒ Object



72
73
74
# File 'lib/osctld/assets/base.rb', line 72

def add_error(error)
  @errors << error
end

#prefetch_zfsArray< Array<String>, Array<String> >

Return a list of datasets and a list properties needed for validation

Returns:

  • (Array< Array<String>, Array<String> >)


35
36
37
# File 'lib/osctld/assets/base.rb', line 35

def prefetch_zfs
  [[], []]
end

#run_validation(run) ⇒ Object (protected)

Parameters:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/osctld/assets/base.rb', line 79

def run_validation(run)
  @valid = nil
  @state = nil
  @errors = []

  unless validate?
    @state = :unknown
    return
  end

  validate(run)

  @block_validators.each do |block_validator|
    block_validator.call(self, run)
  end

  @valid = errors.empty?
  @state = @valid ? :valid : :invalid
end

#state:valid, ...

Returns:

  • (:valid, :invalid, :unknown)


66
67
68
69
70
# File 'lib/osctld/assets/base.rb', line 66

def state
  raise 'asset not validated' if @state.nil?

  @state
end

#typeObject



29
30
31
# File 'lib/osctld/assets/base.rb', line 29

def type
  self.class.type
end

#valid?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
# File 'lib/osctld/assets/base.rb', line 55

def valid?
  if @valid.nil?
    raise 'asset not validated'
  elsif !validate?
    raise 'asset cannot be validated'
  end

  @valid
end

#validate(run) ⇒ Object (protected)

Implement in subclasses to validate the asset

Parameters:



101
# File 'lib/osctld/assets/base.rb', line 101

def validate(run); end

#validate?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/osctld/assets/base.rb', line 39

def validate?
  if @opts.has_key?(:validate_if)
    if @opts[:validate_if].is_a?(Proc)
      @opts[:validate_if].call
    else
      @opts[:validate_if]
    end
  else
    true
  end
end

#validate_block(&block) ⇒ Object



51
52
53
# File 'lib/osctld/assets/base.rb', line 51

def validate_block(&block)
  @block_validators << block
end