File: config.rb

package info (click to toggle)
ruby-representable 3.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 896 kB
  • sloc: ruby: 6,432; makefile: 3
file content (44 lines) | stat: -rw-r--r-- 1,142 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module Representable
  # 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 = ::Declarative::Option(value, instance_exec: true, callable: Uber::Callable)
    end

    # Computes the wrap string or returns false.
    def wrap_for(represented, *args, &block)
      return unless @wrap

      value = @wrap.(represented, *args)

      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