Class: Para::ComponentsConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/para/components_configuration.rb

Defined Under Namespace

Classes: Component, ComponentTooDeepError, Section, UndefinedComponentTypeError

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/para/components_configuration.rb', line 26

def method_missing(method, *args, &block)
  if (component = component_for(method))
    component.tap(&ActiveDecorator::Decorator.instance.method(:decorate))
  else
    super
  end
end

Instance Method Details

#component_configuration_for(identifier) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/para/components_configuration.rb', line 50

def component_configuration_for(identifier)
  sections.each do |section|
    section.components.each do |component|
      # If one of the section component has the searched identifier return it
      if component.identifier.to_s == identifier.to_s
        return component
      else
        component.child_components.each do |child_component|
          # If one of the component children has the searched identifier return it
          if child_component.identifier.to_s == identifier.to_s
            return child_component
          end
        end
      end
    end
  end

  # Return nil if the identifier was not found
  nil
end

#component_for(identifier) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/para/components_configuration.rb', line 42

def component_for(identifier)
  if (component = components_cache[identifier])
    component
  elsif (component_id = components_ids_hash[identifier])
    components_cache[identifier] = Para::Component::Base.find(component_id)
  end
end

#draw(&block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/para/components_configuration.rb', line 6

def draw(&block)
  return unless components_installed?
  Para::LogConfig.with_log_level(:fatal) do
    log_level = Rails.logger.level
    Rails.logger.level = :fatal

    eager_load_components!
    instance_eval(&block)
    build
  end
end

#section(*args, &block) ⇒ Object



18
19
20
# File 'lib/para/components_configuration.rb', line 18

def section(*args, &block)
  sections << Section.new(*args, &block)
end

#section_for(identifier) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/para/components_configuration.rb', line 34

def section_for(identifier)
  if (section = sections_cache[identifier])
    section
  elsif (section_id = sections_ids_hash[identifier])
    sections_cache[identifier] = Para::ComponentSection.find(section_id)
  end
end

#sectionsObject



22
23
24
# File 'lib/para/components_configuration.rb', line 22

def sections
  @sections ||= []
end