class KafoWizards::Entries::AbstractEntry

Attributes

default_value[R]
description[RW]
label[R]
name[R]
parent[RW]
post_hook[RW]
pre_hook[RW]
validators[RW]
value[R]

Public Class Methods

descendants() click to toggle source
# File lib/kafo_wizards/entries/abstract.rb, line 49
def self.descendants
  @descendants ||= []
end
entry_type() click to toggle source
# File lib/kafo_wizards/entries/abstract.rb, line 58
def self.entry_type
  :abstract
end
inherited(child) click to toggle source
# File lib/kafo_wizards/entries/abstract.rb, line 53
def self.inherited(child)
  superclass.inherited(child) if self < KafoWizards::Entries::AbstractEntry
  descendants << child
end
new(name, options={}) click to toggle source
# 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

Public Instance Methods

call_post_hook() click to toggle source
# File lib/kafo_wizards/entries/abstract.rb, line 31
def call_post_hook
  post_hook.call(self) if post_hook
end
call_pre_hook() click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/kafo_wizards/entries/abstract.rb, line 27
def required?
  @required
end
update(new_value) click to toggle source
# 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) click to toggle source
# File lib/kafo_wizards/entries/abstract.rb, line 45
def validate(new_value)
  new_value
end

Protected Instance Methods

class_to_underscore(class_name) click to toggle source
# File lib/kafo_wizards/entries/abstract.rb, line 64
def class_to_underscore(class_name)
  class_name.split('::').last.gsub(/Entry/, '').
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  downcase.to_sym
end