Class: KafoWizards::Entries::AbstractEntry
- Inherits:
-
Object
- Object
- KafoWizards::Entries::AbstractEntry
- Defined in:
- lib/kafo_wizards/entries/abstract.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#default_value ⇒ Object
readonly
Returns the value of attribute default_value.
-
#description ⇒ Object
Returns the value of attribute description.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#post_hook ⇒ Object
Returns the value of attribute post_hook.
-
#pre_hook ⇒ Object
Returns the value of attribute pre_hook.
-
#validators ⇒ Object
Returns the value of attribute validators.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #call_post_hook ⇒ Object
- #call_pre_hook ⇒ Object
- #display_type(with_ancestry = false) ⇒ Object
-
#initialize(name, options = {}) ⇒ AbstractEntry
constructor
A new instance of AbstractEntry.
- #required? ⇒ Boolean
- #update(new_value) ⇒ Object
- #validate(new_value) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ AbstractEntry
Returns a new instance of AbstractEntry.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 6 def initialize(name, ={}) @name = name @value = @default_value = .fetch(:default_value, nil) @label = .fetch(:label, @name.to_s.gsub('_', ' ').capitalize) @description = .fetch(:description, '') @validators = .fetch(:validators, []) @required = !!.fetch(:required, false) @parent = .fetch(:parent, nil) @post_hook = .fetch(:post_hook, nil) @pre_hook = .fetch(:pre_hook, nil) end |
Instance Attribute Details
#default_value ⇒ Object (readonly)
Returns the value of attribute default_value.
3 4 5 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 3 def default_value @default_value end |
#description ⇒ Object
Returns the value of attribute description.
4 5 6 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 4 def description @description end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
3 4 5 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 3 def label @label end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 3 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
4 5 6 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 4 def parent @parent end |
#post_hook ⇒ Object
Returns the value of attribute post_hook.
4 5 6 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 4 def post_hook @post_hook end |
#pre_hook ⇒ Object
Returns the value of attribute pre_hook.
4 5 6 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 4 def pre_hook @pre_hook end |
#validators ⇒ Object
Returns the value of attribute validators.
4 5 6 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 4 def validators @validators end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
3 4 5 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 3 def value @value end |
Class Method Details
.descendants ⇒ Object
49 50 51 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 49 def self.descendants @descendants ||= [] end |
.entry_type ⇒ Object
58 59 60 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 58 def self.entry_type :abstract end |
.inherited(child) ⇒ Object
53 54 55 56 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 53 def self.inherited(child) superclass.inherited(child) if self < KafoWizards::Entries::AbstractEntry descendants << child end |
Instance Method Details
#call_post_hook ⇒ Object
31 32 33 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 31 def call_post_hook post_hook.call(self) if post_hook end |
#call_pre_hook ⇒ Object
35 36 37 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 35 def call_pre_hook pre_hook.call(self) if pre_hook end |
#display_type(with_ancestry = false) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 18 def display_type(with_ancestry=false) classes = self.class.ancestors.select { |cls| cls.name =~ /Entry\Z/ } if with_ancestry result = classes.map { |cls| class_to_underscore(cls.name) } else result = class_to_underscore(classes.first.name) end end |
#required? ⇒ Boolean
27 28 29 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 27 def required? @required end |
#update(new_value) ⇒ Object
39 40 41 42 43 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 39 def update(new_value) value = validate(new_value) @value = @validators.inject(value) { |result, lam| lam.call(result) } @value end |
#validate(new_value) ⇒ Object
45 46 47 |
# File 'lib/kafo_wizards/entries/abstract.rb', line 45 def validate(new_value) new_value end |