Class: Serverkit::Resources::Base

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

Class Attribute Summary collapse

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:



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

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

Class Attribute Details

.abstract_class=(value) ⇒ Object (writeonly)

Sets the attribute abstract_class

Parameters:

  • value

    the value to set the attribute abstract_class to.



17
18
19
# File 'lib/serverkit/resources/base.rb', line 17

def abstract_class=(value)
  @abstract_class = value
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



37
38
39
# File 'lib/serverkit/resources/base.rb', line 37

def attributes
  @attributes
end

#backendObject

Returns the value of attribute backend.



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

def backend
  @backend
end

#check_resultObject (readonly)

Returns the value of attribute check_result.



37
38
39
# File 'lib/serverkit/resources/base.rb', line 37

def check_result
  @check_result
end

#recheck_resultObject (readonly)

Returns the value of attribute recheck_result.



37
38
39
# File 'lib/serverkit/resources/base.rb', line 37

def recheck_result
  @recheck_result
end

#recipeObject (readonly)

Returns the value of attribute recipe.



37
38
39
# File 'lib/serverkit/resources/base.rb', line 37

def recipe
  @recipe
end

Class Method Details

.abstract_class?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/serverkit/resources/base.rb', line 19

def abstract_class?
  !!@abstract_class
end

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

Note:

DSL method to define attribute with its validations



24
25
26
27
28
29
30
# File 'lib/serverkit/resources/base.rb', line 24

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

Instance Method Details

#all_errorsArray<Serverkit::Errors::Base>

Note:

For override

Returns:



63
64
65
# File 'lib/serverkit/resources/base.rb', line 63

def all_errors
  attribute_validation_errors
end

#check_command(*args) ⇒ true, false

Returns:

  • (true, false)


68
69
70
# File 'lib/serverkit/resources/base.rb', line 68

def check_command(*args)
  run_command(*args).success?
end

#check_command_from_identifier(*args) ⇒ true, false

Returns:

  • (true, false)


73
74
75
# File 'lib/serverkit/resources/base.rb', line 73

def check_command_from_identifier(*args)
  run_command_from_identifier(*args).success?
end

#get_command_from_identifier(*args) ⇒ String

Returns:

  • (String)


78
79
80
# File 'lib/serverkit/resources/base.rb', line 78

def get_command_from_identifier(*args)
  backend.command.get(*args)
end

#handlersArray<Serverkit::Resource>

Returns:

  • (Array<Serverkit::Resource>)


83
84
85
86
87
88
89
# File 'lib/serverkit/resources/base.rb', line 83

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)


93
94
95
# File 'lib/serverkit/resources/base.rb', line 93

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

#notifiable?true, false

Returns True if this resource should call any handler.

Returns:

  • (true, false)

    True if this resource should call any handler



98
99
100
# File 'lib/serverkit/resources/base.rb', line 98

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

#run_applyObject

Note:

#check and #apply wrapper



103
104
105
106
107
108
# File 'lib/serverkit/resources/base.rb', line 103

def run_apply
  unless run_check
    apply
    @recheck_result = !!recheck_with_script
  end
end

#run_checktrue, false

Note:

#check wrapper

Returns:

  • (true, false)


112
113
114
# File 'lib/serverkit/resources/base.rb', line 112

def run_check
  @check_result = !!check_with_script
end

#successful?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/serverkit/resources/base.rb', line 116

def successful?
  successful_on_check? || successful_on_recheck?
end

#successful_on_check?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/serverkit/resources/base.rb', line 120

def successful_on_check?
  @check_result == true
end

#successful_on_recheck?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/serverkit/resources/base.rb', line 124

def successful_on_recheck?
  @recheck_result == true
end

#to_aArray<Serverkit::Resources::Base>

Note:

recipe resource will override to replace itself with multiple resources

Returns:



130
131
132
# File 'lib/serverkit/resources/base.rb', line 130

def to_a
  [self]
end