Module: ReeDto::EntityDSL::ClassMethods

Includes:
Ree::Contracts::ArgContracts, Ree::Contracts::Core
Defined in:
lib/ree_lib/packages/ree_dto/package/ree_dto/entity_dsl.rb

Instance Method Summary collapse

Instance Method Details

#properties(**args) ⇒ Object



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
103
104
105
106
107
108
109
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/entity_dsl.rb', line 46

def properties(**args)
  args.each do |name, contract_class|
    contract None => contract_class
    class_eval %Q(
      def #{name}
        @#{name}
      end
    )
  end

  contract Kwargs[**args] => Any
  class_eval %Q(
    def initialize(#{args.keys.map {|k| "#{k}: nil"}.join(',')})
      #{
        args.map {|k, v|
          "@#{k} = #{k}"
        }.join("\n")
      }
    end

    contract Any => Bool
    def ==(val)
      return false unless val.is_a?(self.class)

      #{
        args.map {|k, _|
          "@#{k} == val.#{k}"
        }.join(" && ")
      }
    end

    def to_h
      {
        #{args.map {|k, _| "#{k}: #{k}" }.join(", ")}
      }
    end

    def values_for(*args)
      args.map do |arg|
        variable = ("@" + arg.to_s).to_sym

        if !instance_variables.include?(variable)
          raise ArgumentError.new("variable :" + arg.to_s + " not found in dto")
        end

        instance_variable_get(variable)
      end
    end

    class << self
      def import_from(other_dto)
        self.new(
          #{
            args.map {|k, v|
              "#{k}: other_dto.#{k}"
            }.join(",")
          }
        )
      end
    end
  )

  nil
end