File: basic.rb

package info (click to toggle)
ruby-shoulda-matchers 7.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: ruby: 34,046; sh: 280; makefile: 9
file content (102 lines) | stat: -rw-r--r-- 2,571 bytes parent folder | download
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require 'forwardable'

module UnitTests
  module ModelCreators
    class Basic
      def self.call(args)
        new(args).call
      end

      extend Forwardable

      def_delegators :arguments, :attribute_name, :model_name
      def_delegators :model_creator, :customize_model

      def initialize(arguments)
        @arguments = arguments
        @model_creator = build_model_creator
      end

      def call
        model_creator.call
      end

      protected

      attr_reader :arguments, :model_creator

      private

      def_delegators(
        :arguments,
        :additional_model_creation_strategy_args,
        :all_attribute_overrides,
        :columns,
        :custom_validation?,
        :model_creation_strategy,
        :validation_name,
        :validation_options,
        :column_type,
      )

      def build_model_creator
        model_creator = model_creation_strategy.new(
          model_name,
          columns,
          arguments,
        )

        model_creator.customize_model do |model|
          add_validation_to(model)
          possibly_override_attribute_writer_method_for(model)
        end

        model_creator
      end

      def add_validation_to(model)
        if custom_validation?
          _attribute_name = attribute_name

          model.send(:define_method, :custom_validation) do
            custom_validation.call(self, _attribute_name)
          end

          model.validate(:custom_validation)
        else
          model.public_send(validation_name, attribute_name, validation_options)
        end
      end

      def possibly_override_attribute_writer_method_for(model)
        all_attribute_overrides.each do |attribute_name, overrides|
          if overrides.key?(:changing_values_with)
            _change_value = method(:change_value)

            model.send(:define_method, "#{attribute_name}=") do |value|
              new_value = _change_value.call(
                value,
                overrides[:changing_values_with],
              )

              if
                respond_to?(:write_attribute) && (
                  !self.class.respond_to?(:reflect_on_association) ||
                  !self.class.reflect_on_association(attribute_name)
                )

                write_attribute(attribute_name, new_value)
              else
                super(new_value)
              end
            end
          end
        end
      end

      def change_value(value, value_changer)
        UnitTests::ChangeValue.call(column_type, value, value_changer)
      end
    end
  end
end