Class: OsCtld::Assets::Base

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

Direct Known Subclasses

BaseFile, Dataset, Entry

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
# File 'lib/osctld/assets/base.rb', line 18

def initialize(path, opts, &block)
  @path = path
  @opts = opts
  @validators = []
  @errors = []

  block.call(self) if block
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

.typeObject



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

def self.type
  @type
end

Instance Method Details

#add_error(error) ⇒ Object (protected)



64
65
66
# File 'lib/osctld/assets/base.rb', line 64

def add_error(error)
  @errors << error
end

#stateObject



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

def state
  if validate?
    valid? ? :valid : :invalid
  else
    :unknown
  end
end

#typeObject



27
28
29
# File 'lib/osctld/assets/base.rb', line 27

def type
  self.class.type
end

#valid?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/osctld/assets/base.rb', line 47

def valid?
  @validators.each do |validator|
    validator.call(self)
  end

  errors.empty?
end

#validate(&block) ⇒ Object



43
44
45
# File 'lib/osctld/assets/base.rb', line 43

def validate(&block)
  @validators << block
end

#validate?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/osctld/assets/base.rb', line 31

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