File: from_symbol.rb

package info (click to toggle)
ruby-virtus 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 660 kB
  • sloc: ruby: 4,378; makefile: 2
file content (35 lines) | stat: -rw-r--r-- 876 bytes parent folder | download | duplicates (4)
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
module Virtus
  class Attribute
    class DefaultValue

      # Represents default value evaluated via a symbol
      #
      # @api private
      class FromSymbol < DefaultValue

        # Return if the class can handle the value
        #
        # @param [Object] value
        #
        # @return [Boolean]
        #
        # @api private
        def self.handle?(value)
          value.is_a?(Symbol)
        end

        # Evaluates the value via instance#public_send(value)
        #
        # Symbol value is returned if the instance doesn't respond to value
        #
        # @return [Object] evaluated value
        #
        # @api private
        def call(instance, _)
          instance.respond_to?(@value, true) ? instance.send(@value) : @value
        end

      end # class FromSymbol
    end # class DefaultValue
  end # class Attribute
end # module Virtus