Class: Datory::Attributes::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/datory/attributes/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, **options) ⇒ Attribute

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/datory/attributes/attribute.rb', line 8

def initialize(name, **options) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  @from = Options::From.new(
    name:,
    type: options.fetch(:from),
    consists_of: options.fetch(:consists_of, false),
    min: options.fetch(:min, nil),
    max: options.fetch(:max, nil),
    format: options.fetch(:format, nil)
  )

  @to = Options::To.new(
    name: options.fetch(:to, name),
    type: options.fetch(:as, @from.type),
    # TODO: It is necessary to implement NilClass support for optional
    required: options.fetch(:required, true),
    default: options.fetch(:default, nil),
    consists_of: @from.consists_of,
    min: @from.min,
    max: @from.max,
    format: options.fetch(:format, nil),
    include_class: options.fetch(:include, nil)
  )
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



6
7
8
# File 'lib/datory/attributes/attribute.rb', line 6

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



6
7
8
# File 'lib/datory/attributes/attribute.rb', line 6

def to
  @to
end

Instance Method Details

#input_deserialization_optionsObject

rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



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
# File 'lib/datory/attributes/attribute.rb', line 69

def input_deserialization_options # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  hash = {
    as: to.name,
    type: from.type,
    required: to.required,
    default: to.default,
    consists_of: from.consists_of,
    prepare: (lambda do |value:|
      return value unless to.include_class.present?

      if [Set, Array].include?(from.type)
        value.map { |item| to.include_class.deserialize(**item) }
      else
        return nil if value.nil? # NOTE: When `one` is optional and not passed

        to.include_class.deserialize(**value)
      end
    end)
  }

  hash[:min] = from.min if from.min.present?

  hash[:max] = from.max if from.max.present?

  hash[:format] = from.format if from.format.present?

  hash
end

#input_serialization_optionsObject

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/datory/attributes/attribute.rb', line 34

def input_serialization_options # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  hash = {
    as: to.name,
    type: to.type,
    required: to.required,
    default: to.default,
    consists_of: to.consists_of
  }

  hash[:min] = to.min if to.min.present?

  hash[:max] = to.max if to.max.present?

  hash[:format] = to.format if to.format.present?

  hash
end

#output_deserialization_optionsObject

rubocop:disable Metrics/AbcSize



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/datory/attributes/attribute.rb', line 98

def output_deserialization_options # rubocop:disable Metrics/AbcSize
  hash = {
    consists_of: to.consists_of,
    type: to.type
  }

  hash[:min] = to.min if to.min.present?

  hash[:max] = to.max if to.max.present?

  hash[:format] = to.format if to.format.present?

  hash
end

#output_serialization_optionsObject

rubocop:disable Metrics/AbcSize



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/datory/attributes/attribute.rb', line 52

def output_serialization_options # rubocop:disable Metrics/AbcSize
  hash = {
    consists_of: from.consists_of,
    type: from.type
  }

  hash[:min] = from.min if from.min.present?

  hash[:max] = from.max if from.max.present?

  hash[:format] = from.format if from.format.present?

  hash
end