Module: Schema::Associations::HasOne::ClassMethods

Defined in:
lib/schema/associations/has_one.rb

Overview

no-doc

Instance Method Summary collapse

Instance Method Details

#has_one(name, options = {}, &block) ⇒ Object

rubocop:disable Naming/PredicateName



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/schema/associations/has_one.rb', line 14

def has_one(name, options = {}, &block)
  options = ::Schema::Utils.add_association_class(self, name, :has_one, options)

  class_eval(
    <<~STR, __FILE__, __LINE__ + 1
      def #{options[:getter]}
        #{options[:instance_variable]}
      end

      def #{name}_schema_creator
        @#{name}_schema_creator ||= ::Schema::Associations::SchemaCreator.new(self, #{name.inspect})
      end

      def #{options[:setter]}(v)
        #{options[:instance_variable]} = #{name}_schema_creator.create_schema(self, v)
      end
  STR
  )

  kls = const_get(options[:class_name])
  kls.class_eval(&block) if block
  kls
end