Class: Modular::Components::Container
- Defined in:
- lib/modular/components/container.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
Attributes inherited from Base
Instance Method Summary collapse
- #add(type = nil, args = {}, &block) ⇒ Object
- #all_errors ⇒ Object
- #find_by_uniqid(id) ⇒ Object
-
#initialize(attributes = {}) ⇒ Container
constructor
A new instance of Container.
- #valid_with_children?(context = nil) ⇒ Boolean (also: #valid?)
Methods inherited from Base
attr_accessor, attr_reader, attr_writer, #persisted?, #to_hash, #to_json, #type, type, use_mustached_template!
Methods included from Rendering
#action_name, #execute, #render
Constructor Details
#initialize(attributes = {}) ⇒ Container
Returns a new instance of Container.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/modular/components/container.rb', line 6 def initialize(attributes = {}) @attributes = ActiveSupport::HashWithIndifferentAccess.new(:children => []) attributes = attributes.with_indifferent_access attributes["children"] ||= [] attributes["children"].each do |value| @attributes[:children].push value if value.is_a? Modular::Components::Base end super(attributes.except("children")) end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
4 5 6 |
# File 'lib/modular/components/container.rb', line 4 def children @children end |
Instance Method Details
#add(type = nil, args = {}, &block) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/modular/components/container.rb', line 18 def add(type = nil, args = {}, &block) unless type.nil? cont = Modular.create(type, args) cont.instance_eval &block if block_given? children.push cont end end |
#all_errors ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/modular/components/container.rb', line 37 def all_errors if valid_with_no_children? errs = {} else errs = super end children.each do |child| e = child.all_errors e.each do |key, value| unless value.empty? errs[key] ||= [] errs[key] += value end end end errs end |
#find_by_uniqid(id) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/modular/components/container.rb', line 56 def find_by_uniqid(id) id = id.to_s result = super if result.nil? children.each do |child| result = child.find_by_uniqid(id) return result unless result.nil? end end result end |
#valid_with_children?(context = nil) ⇒ Boolean Also known as: valid?
26 27 28 29 30 31 32 |
# File 'lib/modular/components/container.rb', line 26 def valid_with_children?(context = nil) children.each do |child| return false if child.invalid? end valid_with_no_children? end |