Class: Serverkit::Resources::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/serverkit/resources/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recipe, attributes) ⇒ Base

Returns a new instance of Base.

Parameters:



33
34
35
36
# File 'lib/serverkit/resources/base.rb', line 33

def initialize(recipe, attributes)
  @attributes = attributes
  @recipe = recipe
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



25
26
27
# File 'lib/serverkit/resources/base.rb', line 25

def attributes
  @attributes
end

#backendObject

Returns the value of attribute backend.



23
24
25
# File 'lib/serverkit/resources/base.rb', line 23

def backend
  @backend
end

#recipeObject (readonly)

Returns the value of attribute recipe.



25
26
27
# File 'lib/serverkit/resources/base.rb', line 25

def recipe
  @recipe
end

Class Method Details

.attribute(name, options = {}) ⇒ Object

Note:

DSL method to define attribute with its validations



12
13
14
15
16
17
18
# File 'lib/serverkit/resources/base.rb', line 12

def attribute(name, options = {})
  default = options.delete(:default)
  define_method(name) do
    @attributes[name.to_s] || default
  end
  validates name, options unless options.empty?
end

Instance Method Details

#all_errorsArray<Serverkit::Errors::Base>

Note:

For override

Returns:



40
41
42
# File 'lib/serverkit/resources/base.rb', line 40

def all_errors
  attribute_validation_errors
end

#handlersArray<Serverkit::Resource>

Returns:

  • (Array<Serverkit::Resource>)


45
46
47
48
49
50
51
# File 'lib/serverkit/resources/base.rb', line 45

def handlers
  @handlers ||= Array(notify).map do |id|
    recipe.handlers.find do |handler|
      handler.id == id
    end
  end.compact
end

#idString

Note:

For logging and notifying

Returns:

  • (String)


55
56
57
# File 'lib/serverkit/resources/base.rb', line 55

def id
  @attributes["id"] || default_id
end

#inspect_apply_resultString

Returns:

  • (String)


60
61
62
63
64
65
66
67
68
69
# File 'lib/serverkit/resources/base.rb', line 60

def inspect_apply_result
  case @recheck_result
  when nil
    "[SKIP] #{result_inspection_suffix}"
  when false
    "[FAIL] #{result_inspection_suffix}"
  else
    "[DONE] #{result_inspection_suffix}"
  end
end

#inspect_check_resultString

Returns:

  • (String)


72
73
74
75
76
77
78
# File 'lib/serverkit/resources/base.rb', line 72

def inspect_check_result
  if @check_result
    "[ OK ] #{result_inspection_suffix}"
  else
    "[ NG ] #{result_inspection_suffix}"
  end
end

#notifiable?true, false

Returns True if this resource should call any handler.

Returns:

  • (true, false)

    True if this resource should call any handler



81
82
83
# File 'lib/serverkit/resources/base.rb', line 81

def notifiable?
  @recheck_result == true && !handlers.nil?
end

#run_applyObject

Note:

#check and #apply wrapper



86
87
88
89
90
91
# File 'lib/serverkit/resources/base.rb', line 86

def run_apply
  unless run_check
    apply
    @recheck_result = recheck
  end
end

#run_checktrue, false

Note:

#check wrapper

Returns:

  • (true, false)


95
96
97
# File 'lib/serverkit/resources/base.rb', line 95

def run_check
  @check_result = check
end

#to_aArray<Serverkit::Resources::Base>

Note:

recipe resource will override to replace itself with multiple resources

Returns:



101
102
103
# File 'lib/serverkit/resources/base.rb', line 101

def to_a
  [self]
end