Sha256: e0f5b092734ee74c24851186f8cf6f42fbc3fb7dee0e7ea25dfed928d6dc3929

Contents?: true

Size: 1.34 KB

Versions: 10

Compression:

Stored size: 1.34 KB

Contents

module Blocks
  class HashWithRenderStrategy < HashWithCaller
    attr_accessor :render_strategy

    RENDER_WITH_PROXY = :with
    RENDER_WITH_BLOCK = :block
    RENDER_WITH_PARTIAL = :partial

    RENDERING_STRATEGIES = [RENDER_WITH_PROXY, RENDER_WITH_BLOCK, RENDER_WITH_PARTIAL]

    def initialize_copy(original)
      super
      self.callers = original.callers.clone
      self.render_strategy = nil
      RENDERING_STRATEGIES.each do |rs|
        self.delete(rs)
      end
    end

    alias_method :dup, :clone

    def reverse_merge(options)
      self.clone.tap {|cloned| cloned.add_options(options) }
    end

    # TODO: need to implement either merge or update to update
    #  both the hash and the callers hash

    def add_options(*args, &block)
      options = args.extract_options!
      if !options.is_a?(HashWithIndifferentAccess)
        options = options.with_indifferent_access
      end
      options[:block] = block if block
      if render_strategy.nil?
        self.render_strategy = if options.is_a?(HashWithRenderStrategy)
          options.render_strategy
        else
          RENDERING_STRATEGIES.detect {|render_strategy| options[render_strategy].present? }
        end
      end

      super(*args, options)
    end

    def render_strategy_and_item
      [render_strategy, self[render_strategy]] if render_strategy
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
blocks-3.0.4 lib/blocks/utilities/hash_with_render_strategy.rb
blocks-3.0.3 lib/blocks/utilities/hash_with_render_strategy.rb
blocks-3.0.2 lib/blocks/utilities/hash_with_render_strategy.rb
blocks-3.0.1 lib/blocks/utilities/hash_with_render_strategy.rb
blocks-3.0.0 lib/blocks/utilities/hash_with_render_strategy.rb
blocks-3.0.0.rc9 lib/blocks/utilities/hash_with_render_strategy.rb
blocks-3.0.0.rc8 lib/blocks/utilities/hash_with_render_strategy.rb
blocks-3.0.0.rc7 lib/blocks/utilities/hash_with_render_strategy.rb
blocks-3.0.0.rc6 lib/blocks/utilities/hash_with_render_strategy.rb
blocks-3.0.0.rc5 lib/blocks/utilities/hash_with_render_strategy.rb