Class: KafoWizards::Entries::AbstractEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/kafo_wizards/entries/abstract.rb

Direct Known Subclasses

BooleanEntry, ButtonEntry, StringEntry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options={})
  @name = name
  @value = @default_value = options.fetch(:default_value, nil)
  @label = options.fetch(:label, @name.to_s.gsub('_', ' ').capitalize)
  @description = options.fetch(:description, '')
  @validators = options.fetch(:validators, [])
  @required = !!options.fetch(:required, false)
  @parent = options.fetch(:parent, nil)
  @post_hook = options.fetch(:post_hook, nil)
  @pre_hook = options.fetch(:pre_hook, nil)
end

Instance Attribute Details

#default_valueObject (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

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/kafo_wizards/entries/abstract.rb', line 4

def description
  @description
end

#labelObject (readonly)

Returns the value of attribute label.



3
4
5
# File 'lib/kafo_wizards/entries/abstract.rb', line 3

def label
  @label
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/kafo_wizards/entries/abstract.rb', line 3

def name
  @name
end

#parentObject

Returns the value of attribute parent.



4
5
6
# File 'lib/kafo_wizards/entries/abstract.rb', line 4

def parent
  @parent
end

#post_hookObject

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_hookObject

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

#validatorsObject

Returns the value of attribute validators.



4
5
6
# File 'lib/kafo_wizards/entries/abstract.rb', line 4

def validators
  @validators
end

#valueObject (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

.descendantsObject



49
50
51
# File 'lib/kafo_wizards/entries/abstract.rb', line 49

def self.descendants
  @descendants ||= []
end

.entry_typeObject



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_hookObject



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_hookObject



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

Returns:

  • (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