Module: Lutaml::Model::Serialize

Includes:
ComparableModel, 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 Validation

#validate, #validate!

Methods included from ComparableModel

#eql?, #hash

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



614
615
616
617
618
619
620
621
622
623
# File 'lib/lutaml/model/serialize.rb', line 614

def method_missing(method_name, *args)
  if method_name.to_s.end_with?("=") && self.class.attributes.key?(method_name.to_s.chomp("=").to_sym)
    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.



540
541
542
# File 'lib/lutaml/model/serialize.rb', line 540

def element_order
  @element_order
end

#encodingObject

Returns the value of attribute encoding.



540
541
542
# File 'lib/lutaml/model/serialize.rb', line 540

def encoding
  @encoding
end

#mixed=(value) ⇒ Object (writeonly)

Sets the attribute mixed



541
542
543
# File 'lib/lutaml/model/serialize.rb', line 541

def mixed=(value)
  @mixed = value
end

#ordered=(value) ⇒ Object (writeonly)

Sets the attribute ordered



541
542
543
# File 'lib/lutaml/model/serialize.rb', line 541

def ordered=(value)
  @ordered = value
end

#schema_locationObject

Returns the value of attribute schema_location.



540
541
542
# File 'lib/lutaml/model/serialize.rb', line 540

def schema_location
  @schema_location
end

Class Method Details

.included(base) ⇒ Object



22
23
24
25
# File 'lib/lutaml/model/serialize.rb', line 22

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

Instance Method Details

#attr_value(attrs, name, attr_rule) ⇒ Object



576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# File 'lib/lutaml/model/serialize.rb', line 576

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

#initialize(attrs = {}) ⇒ Object



543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/lutaml/model/serialize.rb', line 543

def initialize(attrs = {})
  @using_default = {}

  return unless self.class.attributes

  if attrs.is_a?(Lutaml::Model::MappingHash)
    @ordered = attrs.ordered?
    @element_order = attrs.item_order
  end

  if attrs.key?(:schema_location)
    self.schema_location = attrs[:schema_location]
  end

  self.class.attributes.each do |name, attr|
    value = if attrs.key?(name) || attrs.key?(name.to_s)
              attr_value(attrs, name, attr)
            else
              using_default_for(name)
              attr.default
            end

    # Initialize collections with an empty array if no value is provided
    if attr.collection? && value.nil?
      value = []
    end

    default = using_default?(name)
    public_send(:"#{name}=", self.class.ensure_utf8(value))
    using_default_for(name) if default
  end
end

#key_exist?(hash, key) ⇒ Boolean



639
640
641
# File 'lib/lutaml/model/serialize.rb', line 639

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

#key_value(hash, key) ⇒ Object



643
644
645
# File 'lib/lutaml/model/serialize.rb', line 643

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

#mixed?Boolean



635
636
637
# File 'lib/lutaml/model/serialize.rb', line 635

def mixed?
  !!@mixed
end

#ordered?Boolean



631
632
633
# File 'lib/lutaml/model/serialize.rb', line 631

def ordered?
  !!@ordered
end

#pretty_print_instance_variablesObject



647
648
649
# File 'lib/lutaml/model/serialize.rb', line 647

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

#to_yaml_hashObject



651
652
653
# File 'lib/lutaml/model/serialize.rb', line 651

def to_yaml_hash
  self.class.as_yaml(self)
end

#using_default?(attribute_name) ⇒ Boolean



610
611
612
# File 'lib/lutaml/model/serialize.rb', line 610

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

#using_default_for(attribute_name) ⇒ Object



602
603
604
# File 'lib/lutaml/model/serialize.rb', line 602

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

#validate_attribute!(attr_name) ⇒ Object



625
626
627
628
629
# File 'lib/lutaml/model/serialize.rb', line 625

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

#value_set_for(attribute_name) ⇒ Object



606
607
608
# File 'lib/lutaml/model/serialize.rb', line 606

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