Class: Serverkit::Resources::Base
- Inherits:
-
Object
- Object
- Serverkit::Resources::Base
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/serverkit/resources/base.rb
Direct Known Subclasses
Command, File, Git, HomebrewCask, Missing, Nothing, Package, Recipe, Service, Symlink, Unknown
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#backend ⇒ Object
Returns the value of attribute backend.
-
#recipe ⇒ Object
readonly
Returns the value of attribute recipe.
Class Method Summary collapse
Instance Method Summary collapse
- #all_errors ⇒ Array<Serverkit::Errors::Base>
- #handlers ⇒ Array<Serverkit::Resource>
- #id ⇒ String
-
#initialize(recipe, attributes) ⇒ Base
constructor
A new instance of Base.
- #inspect_apply_result ⇒ String
- #inspect_check_result ⇒ String
-
#notifiable? ⇒ true, false
True if this resource should call any handler.
- #run_apply ⇒ Object
- #run_check ⇒ true, false
- #to_a ⇒ Array<Serverkit::Resources::Base>
Constructor Details
#initialize(recipe, attributes) ⇒ Base
Returns a new instance of Base.
33 34 35 36 |
# File 'lib/serverkit/resources/base.rb', line 33 def initialize(recipe, attributes) @attributes = attributes @recipe = recipe end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
25 26 27 |
# File 'lib/serverkit/resources/base.rb', line 25 def attributes @attributes end |
#backend ⇒ Object
Returns the value of attribute backend.
23 24 25 |
# File 'lib/serverkit/resources/base.rb', line 23 def backend @backend end |
#recipe ⇒ Object (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, = {}) default = .delete(:default) define_method(name) do @attributes[name.to_s] || default end validates name, unless .empty? end |
Instance Method Details
#all_errors ⇒ Array<Serverkit::Errors::Base>
Note:
For override
40 41 42 |
# File 'lib/serverkit/resources/base.rb', line 40 def all_errors attribute_validation_errors end |
#handlers ⇒ 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 |
#id ⇒ String
Note:
For logging and notifying
55 56 57 |
# File 'lib/serverkit/resources/base.rb', line 55 def id @attributes["id"] || default_id end |
#inspect_apply_result ⇒ 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_result ⇒ 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.
81 82 83 |
# File 'lib/serverkit/resources/base.rb', line 81 def notifiable? @recheck_result == true && !handlers.nil? end |
#run_apply ⇒ Object
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_check ⇒ true, false
Note:
#check wrapper
95 96 97 |
# File 'lib/serverkit/resources/base.rb', line 95 def run_check @check_result = check end |
#to_a ⇒ Array<Serverkit::Resources::Base>
Note:
recipe resource will override to replace itself with multiple resources
101 102 103 |
# File 'lib/serverkit/resources/base.rb', line 101 def to_a [self] end |