Class: Datory::Attributes::Attribute
- Inherits:
-
Object
- Object
- Datory::Attributes::Attribute
- Defined in:
- lib/datory/attributes/attribute.rb
Instance Attribute Summary collapse
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Instance Method Summary collapse
-
#initialize(name, **options) ⇒ Attribute
constructor
rubocop:disable Metrics/MethodLength, Metrics/AbcSize.
-
#input_deserialization_options ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
-
#input_serialization_options ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
-
#output_deserialization_options ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#output_serialization_options ⇒ Object
rubocop:disable Metrics/AbcSize.
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, **) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize @from = Options::From.new( name:, type: .fetch(:from), consists_of: .fetch(:consists_of, false), min: .fetch(:min, nil), max: .fetch(:max, nil), format: .fetch(:format, nil) ) @to = Options::To.new( name: .fetch(:to, name), type: .fetch(:as, @from.type), # TODO: It is necessary to implement NilClass support for optional required: .fetch(:required, true), default: .fetch(:default, nil), consists_of: @from.consists_of, min: @from.min, max: @from.max, format: .fetch(:format, nil), include_class: .fetch(:include, nil) ) end |
Instance Attribute Details
#from ⇒ Object (readonly)
Returns the value of attribute from.
6 7 8 |
# File 'lib/datory/attributes/attribute.rb', line 6 def from @from end |
#to ⇒ Object (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_options ⇒ Object
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 # 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_options ⇒ Object
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 # 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_options ⇒ Object
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 # 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_options ⇒ Object
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 # 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 |