Class: Interview::Control

Inherits:
Object
  • Object
show all
Includes:
Draper::ViewHelpers
Defined in:
lib/interview/control.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Control

Returns a new instance of Control.



7
8
9
10
# File 'lib/interview/control.rb', line 7

def initialize(params={})
  set_attributes(params)
  set_defaults
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



5
6
7
# File 'lib/interview/control.rb', line 5

def parent
  @parent
end

Instance Method Details

#ancestorsObject



29
30
31
32
33
34
35
# File 'lib/interview/control.rb', line 29

def ancestors
  if @parent.nil?
    return []
  else
    return [@parent] + @parent.ancestors
  end
end

#build(b) ⇒ Object



61
62
63
# File 'lib/interview/control.rb', line 61

def build(b)
  yield if block_given?
end

#build_child(b, control, &block) ⇒ Object



70
71
72
# File 'lib/interview/control.rb', line 70

def build_child(b, control, &block)
  b.add! control, &block
end

#build_with_params(b, params = {}) ⇒ Object



65
66
67
68
# File 'lib/interview/control.rb', line 65

def build_with_params(b, params={})
  set_attributes(params)
  build(b)
end

#find_attribute(attribute) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/interview/control.rb', line 37

def find_attribute(attribute)
  if respond_to? attribute
    value = send attribute
    return value if value
  end
  if @parent
    return @parent.find_attribute(attribute)
  else
    return nil
  end
  # return nil if @parent.nil?
  # if @parent.respond_to? attribute
    # return @parent.send attribute
  # else
    # return @parent.find_attribute attribute
  # end
end

#find_attribute!(attr_name) ⇒ Object



55
56
57
58
59
# File 'lib/interview/control.rb', line 55

def find_attribute!(attr_name)
  attribute = find_attribute(attr_name)
  throw "Attribute #{attr_name} was not found." if attribute.nil?
  return attribute
end

#set_attributes(params = {}) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/interview/control.rb', line 12

def set_attributes(params={})
  params.each do |key, value|
    key = "#{key}=".to_sym
    if respond_to?(key) # and not value.nil?
      send(key, value)
    end
  end
end

#set_defaultsObject

todo: löschen?



21
22
23
24
25
26
27
# File 'lib/interview/control.rb', line 21

def set_defaults # todo: löschen?
  if self.class.const_defined? 'DEFAULTS'
    self.class::DEFAULTS.each do |key, value|
      self.send("#{key}=".to_sym, value)
    end
  end
end