Module: Lutaml::Model::Serialize

Includes:
ComparableModel, Liquefiable, Validation
Included in:
Serializable
Defined in:
lib/lutaml/model/serialize.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Liquefiable

#to_liquid

Methods included from Validation

#validate, #validate!, #validate_helper

Methods included from ComparableModel

#already_compared?, #attributes_hash, #calculate_hash, #comparison_key, #eql?, #hash, #same_class?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



520
521
522
523
524
525
526
527
528
529
# File 'lib/lutaml/model/serialize.rb', line 520

def method_missing(method_name, *args)
  if method_name.to_s.end_with?("=") && attribute_exist?(method_name)
    define_singleton_method(method_name) do |value|
      instance_variable_set(:"@#{method_name.to_s.chomp('=')}", value)
    end
    send(method_name, *args)
  else
    super
  end
end

Instance Attribute Details

#element_orderObject

Returns the value of attribute element_order.



462
463
464
# File 'lib/lutaml/model/serialize.rb', line 462

def element_order
  @element_order
end

#encodingObject

Returns the value of attribute encoding.



462
463
464
# File 'lib/lutaml/model/serialize.rb', line 462

def encoding
  @encoding
end

#mixed=(value) ⇒ Object (writeonly)

Sets the attribute mixed

Parameters:

  • value

    the value to set the attribute mixed to.



463
464
465
# File 'lib/lutaml/model/serialize.rb', line 463

def mixed=(value)
  @mixed = value
end

#ordered=(value) ⇒ Object (writeonly)

Sets the attribute ordered

Parameters:

  • value

    the value to set the attribute ordered to.



463
464
465
# File 'lib/lutaml/model/serialize.rb', line 463

def ordered=(value)
  @ordered = value
end

#schema_locationObject

Returns the value of attribute schema_location.



462
463
464
# File 'lib/lutaml/model/serialize.rb', line 462

def schema_location
  @schema_location
end

Class Method Details

.included(base) ⇒ Object



24
25
26
27
# File 'lib/lutaml/model/serialize.rb', line 24

def self.included(base)
  base.extend(ClassMethods)
  base.initialize_attrs(base)
end

.register_format_mapping_method(format) ⇒ Object



427
428
429
430
431
432
433
# File 'lib/lutaml/model/serialize.rb', line 427

def self.register_format_mapping_method(format)
  method_name = format == :hash ? :hsh : format

  ::Lutaml::Model::Serialize::ClassMethods.define_method(method_name) do |&block|
    process_mapping(format, &block)
  end
end

.register_from_format_method(format) ⇒ Object



435
436
437
438
439
440
441
442
443
# File 'lib/lutaml/model/serialize.rb', line 435

def self.register_from_format_method(format)
  ClassMethods.define_method(:"from_#{format}") do |data, options = {}|
    from(format, data, options)
  end

  ClassMethods.define_method(:"of_#{format}") do |doc, options = {}|
    of(format, doc, options)
  end
end

.register_to_format_method(format) ⇒ Object



445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/lutaml/model/serialize.rb', line 445

def self.register_to_format_method(format)
  ClassMethods.define_method(:"to_#{format}") do |instance, options = {}|
    to(format, instance, options)
  end

  ClassMethods.define_method(:"as_#{format}") do |instance, options = {}|
    as(format, instance, options)
  end

  define_method(:"to_#{format}") do |options = {}|
    raise Lutaml::Model::NoRootMappingError.new(self.class) if format == :xml && !self.class.root?

    options[:parse_encoding] = encoding if encoding
    self.class.to(format, self, options)
  end
end

Instance Method Details

#attr_value(attrs, name, attr_rule) ⇒ Object



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/lutaml/model/serialize.rb', line 482

def attr_value(attrs, name, attr_rule)
  value = if attrs.key?(name.to_sym)
            attrs[name.to_sym]
          elsif attrs.key?(name.to_s)
            attrs[name.to_s]
          else
            attr_rule.default
          end

  if attr_rule.collection? || value.is_a?(Array)
    value&.map do |v|
      if v.is_a?(Hash)
        attr_rule.type.new(v)
      else
        # TODO: This code is problematic because Type.cast does not know
        # about all the types.
        Lutaml::Model::Type.cast(v, attr_rule.type)
      end
    end
  else
    # TODO: This code is problematic because Type.cast does not know
    # about all the types.
    Lutaml::Model::Type.cast(value, attr_rule.type)
  end
end

#attribute_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


536
537
538
539
540
# File 'lib/lutaml/model/serialize.rb', line 536

def attribute_exist?(name)
  name = name.to_s.chomp("=").to_sym if name.end_with?("=")

  self.class.attributes.key?(name)
end

#initialize(attrs = {}, options = {}) ⇒ Object



465
466
467
468
469
470
471
472
# File 'lib/lutaml/model/serialize.rb', line 465

def initialize(attrs = {}, options = {})
  @using_default = {}
  return unless self.class.attributes

  set_ordering(attrs)
  set_schema_location(attrs)
  initialize_attributes(attrs, options)
end

#key_exist?(hash, key) ⇒ Boolean

Returns:

  • (Boolean)


556
557
558
# File 'lib/lutaml/model/serialize.rb', line 556

def key_exist?(hash, key)
  hash.key?(key.to_sym) || hash.key?(key.to_s)
end

#key_value(hash, key) ⇒ Object



560
561
562
# File 'lib/lutaml/model/serialize.rb', line 560

def key_value(hash, key)
  hash[key.to_sym] || hash[key.to_s]
end

#mixed?Boolean

Returns:

  • (Boolean)


552
553
554
# File 'lib/lutaml/model/serialize.rb', line 552

def mixed?
  !!@mixed
end

#ordered?Boolean

Returns:

  • (Boolean)


548
549
550
# File 'lib/lutaml/model/serialize.rb', line 548

def ordered?
  !!@ordered
end

#pretty_print_instance_variablesObject



564
565
566
# File 'lib/lutaml/model/serialize.rb', line 564

def pretty_print_instance_variables
  (instance_variables - i[@using_default]).sort
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


531
532
533
534
# File 'lib/lutaml/model/serialize.rb', line 531

def respond_to_missing?(method_name, include_private = false)
  (method_name.to_s.end_with?("=") && attribute_exist?(method_name)) ||
    super
end

#to_yaml_hashObject



568
569
570
# File 'lib/lutaml/model/serialize.rb', line 568

def to_yaml_hash
  self.class.as_yaml(self)
end

#using_default?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


516
517
518
# File 'lib/lutaml/model/serialize.rb', line 516

def using_default?(attribute_name)
  @using_default[attribute_name]
end

#using_default_for(attribute_name) ⇒ Object



508
509
510
# File 'lib/lutaml/model/serialize.rb', line 508

def using_default_for(attribute_name)
  @using_default[attribute_name] = true
end

#validate_attribute!(attr_name) ⇒ Object



542
543
544
545
546
# File 'lib/lutaml/model/serialize.rb', line 542

def validate_attribute!(attr_name)
  attr = self.class.attributes[attr_name]
  value = instance_variable_get(:"@#{attr_name}")
  attr.validate_value!(value)
end

#value_map(options) ⇒ Object



474
475
476
477
478
479
480
# File 'lib/lutaml/model/serialize.rb', line 474

def value_map(options)
  {
    omitted: options[:omitted] || :nil,
    nil: options[:nil] || :nil,
    empty: options[:empty] || :empty,
  }
end

#value_set_for(attribute_name) ⇒ Object



512
513
514
# File 'lib/lutaml/model/serialize.rb', line 512

def value_set_for(attribute_name)
  @using_default[attribute_name] = false
end