Sha256: 47c3fa2179b049906ff49b82e55d4ca0a11c3b7eab920a076cbfc21ba12d8129

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

require 'declarative/definitions'

module Representable
  autoload :Option, 'representable/option'

  # Stores Definitions from ::property. It preserves the adding order (1.9+).
  # Same-named properties get overridden, just like in a Hash.
  #
  # Overwrite definition_class if you need a custom Definition object (helpful when using
  # representable in other gems).
  class Config < ::Declarative::Definitions
    def initialize(*)
      super
      @wrap = nil
    end

    def remove(name)
      delete(name.to_s)
    end

    def options # FIXME: this is not inherited.
      @options ||= {}
    end

    def wrap=(value)
      value = value.to_s if value.is_a?(Symbol)
      @wrap = ::Representable::Option(value)
    end

    # Computes the wrap string or returns false.
    def wrap_for(represented, options = {}, &block)
      return unless @wrap

      value = @wrap.(exec_context: represented, keyword_arguments: options.to_hash)

      return value if value != true

      infer_name_for(represented.class.name)
    end

  private
    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

5 entries across 5 versions & 3 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/representable-3.2.0/lib/representable/config.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/representable-3.2.0/lib/representable/config.rb
representable-3.2.0 lib/representable/config.rb
representable-3.1.1 lib/representable/config.rb
representable-3.1.0 lib/representable/config.rb