Sha256: ddec31abb63817031ee751b00d14b6eb474af2e0d335cf2f1d4e1ff0bf454947

Contents?: true

Size: 1007 Bytes

Versions: 1

Compression:

Stored size: 1007 Bytes

Contents

module Prezzo
  module ComponentsDSL
    def self.included(base)
      base.class_eval do
        base.extend(ClassMethods)
      end
    end

    module ClassMethods
      def component(name, klass, sub_context_name = nil)
        components << name

        define_method(name) do
          cached_components[name] ||=
            begin
              c = sub_context_name ? context.fetch(sub_context_name) : context

              instance = klass.new(c)
              instance.sub_context(sub_context_name) if sub_context_name
              instance
            end

          cached_components[name].calculate
        end
      end

      def components
        @components ||= []
      end
    end

    def compile_components
      self.class.components.reduce({}) do |acc, name|
        public_send(name) # force component cache
        acc[name] = cached_components[name].explain
        acc
      end
    end

    private

    def cached_components
      @cached_components ||= {}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prezzo-1.0.0.pre.rc lib/prezzo/components_dsl.rb