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



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

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/para/components_configuration.rb', line 59

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
      return component if component.identifier.to_s == identifier.to_s

      component.child_components.each do |child_component|
        # If one of the component children has the searched identifier return it
        return child_component if child_component.identifier.to_s == identifier.to_s
      end
    end
  end

  # Return nil if the identifier was not found
  nil
end

#component_for(identifier) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/para/components_configuration.rb', line 47

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

#components_ids_hashObject



80
81
82
# File 'lib/para/components_configuration.rb', line 80

def components_ids_hash
  @components_ids_hash ||= {}.with_indifferent_access
end

#draw(&block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# 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



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

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

#section_for(identifier) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/para/components_configuration.rb', line 35

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

#sectionsObject



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

def sections
  @sections ||= []
end

#sections_ids_hashObject



76
77
78
# File 'lib/para/components_configuration.rb', line 76

def sections_ids_hash
  @sections_ids_hash ||= {}.with_indifferent_access
end