Sha256: 6d3710ac2f8e3c5f6ca60f16d4bf1970a9b2fa31af465fcf80086aa4800b1590

Contents?: true

Size: 1.54 KB

Versions: 7

Compression:

Stored size: 1.54 KB

Contents

module Representable
  # NOTE: the API of Config is subject to change so don't rely too much on this private object.
  class Config < Hash
    # DISCUSS: experimental. this will soon be moved to a separate gem
    module InheritableArray
      def inheritable_array(name)
        inheritable_arrays[name] ||= []
      end
      def inheritable_arrays
        @inheritable_arrays ||= {}
      end

      def inherit(parent)
        super

        parent.inheritable_arrays.keys.each do |k|
          inheritable_array(k).push *parent.inheritable_array(k).clone
        end
      end
    end

    def <<(definition)
      self[definition.name] = definition
    end

    def [](name)
      fetch(name.to_s, nil)
    end

    def each(*args, &block)
      values.each(*args, &block)
    end

    attr_accessor :wrap

    # Computes the wrap string or returns false.
    def wrap_for(name)
      return unless wrap
      return infer_name_for(name) if wrap === true
      wrap
    end

    # Write representer configuration into this hash.
    def options
      @options ||= {}
    end

    module InheritMethods
      def cloned
        collect { |d| d.clone }
      end

      def inherit(parent)
        push(parent.cloned)
      end
    end
    include InheritMethods
    include InheritableArray # overrides #inherit.

  private
    def push(defs)
      defs.each { |d| self << d }
    end

    def infer_name_for(name)
      name.to_s.split('::').last.
       gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
       gsub(/([a-z\d])([A-Z])/,'\1_\2').
       downcase
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
representable-1.7.7 lib/representable/config.rb
representable-1.7.6 lib/representable/config.rb
representable-1.7.5 lib/representable/config.rb
representable-1.7.4 lib/representable/config.rb
representable-1.7.3 lib/representable/config.rb
representable-1.7.2 lib/representable/config.rb
representable-1.7.1 lib/representable/config.rb