Class: Nemo::Components::MultipleAttributeContainer

Inherits:
Wee::Component
  • Object
show all
Defined in:
lib/nemo/components/multiattr.rb

Overview

Multiple Attribute container

Interface to Add/Edit/Delete/View the related data of the given attribute.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a) ⇒ MultipleAttributeContainer

Returns a new instance of MultipleAttributeContainer.



9
10
11
12
13
14
15
# File 'lib/nemo/components/multiattr.rb', line 9

def initialize(a)
  super()
  @attribute = a
  @components = Array.new
  a.cache.each { |obj| @components << viewer_for(obj.metaobject) }
  @components << create_empty_editor
end

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



7
8
9
# File 'lib/nemo/components/multiattr.rb', line 7

def attribute
  @attribute
end

#componentsObject

Returns the value of attribute components.



7
8
9
# File 'lib/nemo/components/multiattr.rb', line 7

def components
  @components
end

Instance Method Details

#backtrack_state(snapshot) ⇒ Object



21
22
23
24
# File 'lib/nemo/components/multiattr.rb', line 21

def backtrack_state(snapshot)
  super
  snapshot.add(self.children)
end

#childrenObject



17
18
19
# File 'lib/nemo/components/multiattr.rb', line 17

def children
  @components
end

#create_empty_editorObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/nemo/components/multiattr.rb', line 39

def create_empty_editor
  metaobject = @attribute.base_class.new.metaobject
  if @components.size < @attribute.item_limit
    editor = editor_for(metaobject)
    answer = Wee::AnswerDecoration.new
    answer.on_answer = method(:on_edit)
    editor.add_decoration(answer)
    editor
  else
    Nemo::Visitors::Visitor.new(metaobject) # placeholder
  end
end

#delete(component) ⇒ Object



26
27
28
29
30
31
# File 'lib/nemo/components/multiattr.rb', line 26

def delete(component)
  @attribute.remove_from_cache(component.metaobject.baseobject)
  @components.delete(component)
  @components.pop
  @components << create_empty_editor
end

#editor_for(metaobject) ⇒ Object



64
65
66
67
68
# File 'lib/nemo/components/multiattr.rb', line 64

def editor_for(metaobject)
  obj = Nemo::Visitors::Editor.new(metaobject)
  obj.subform = true
  obj
end

#on_edit(obj) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nemo/components/multiattr.rb', line 52

def on_edit(obj)
  if obj.nil?
    @components.pop
    @components << create_empty_editor
  else
    @attribute.add_to_cache(obj)
    @components.pop
    @components << viewer_for(obj.metaobject)
    @components << create_empty_editor
  end
end

#renderObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/nemo/components/multiattr.rb', line 74

def render
  @components.each do |component|
    if component.viewer?
      r.render(component)
      r.submit_button.value('edit').callback{ switch_to_edit_mode(component) }
      if @attribute.item_delete?
        r.space
        r.submit_button.value('delete').callback { delete(component) }
      end
      if @attribute.item_limit > 1
        r.break
        r.break
      end
    else
      r.render(component)
    end
  end
end

#switch_to_edit_mode(viewer) ⇒ Object



33
34
35
36
37
# File 'lib/nemo/components/multiattr.rb', line 33

def switch_to_edit_mode(viewer)
  viewer.viewer = false
  viewer.call( editor_for(viewer.metaobject) )
  viewer.viewer = true
end

#viewer_for(metaobject) ⇒ Object



70
71
72
# File 'lib/nemo/components/multiattr.rb', line 70

def viewer_for(metaobject)
  Nemo::Visitors::Viewer.new(metaobject)
end