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



658
659
660
661
662
663
664
665
666
667
# File 'lib/lutaml/model/serialize.rb', line 658

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.



584
585
586
# File 'lib/lutaml/model/serialize.rb', line 584

def element_order
  @element_order
end

#encodingObject

Returns the value of attribute encoding.



584
585
586
# File 'lib/lutaml/model/serialize.rb', line 584

def encoding
  @encoding
end

#mixed=(value) ⇒ Object (writeonly)

Sets the attribute mixed

Parameters:

  • value

    the value to set the attribute mixed to.



585
586
587
# File 'lib/lutaml/model/serialize.rb', line 585

def mixed=(value)
  @mixed = value
end

#ordered=(value) ⇒ Object (writeonly)

Sets the attribute ordered

Parameters:

  • value

    the value to set the attribute ordered to.



585
586
587
# File 'lib/lutaml/model/serialize.rb', line 585

def ordered=(value)
  @ordered = value
end

#schema_locationObject

Returns the value of attribute schema_location.



584
585
586
# File 'lib/lutaml/model/serialize.rb', line 584

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



620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
# File 'lib/lutaml/model/serialize.rb', line 620

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



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# File 'lib/lutaml/model/serialize.rb', line 587

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

Returns:

  • (Boolean)


683
684
685
# File 'lib/lutaml/model/serialize.rb', line 683

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

#key_value(hash, key) ⇒ Object



687
688
689
# File 'lib/lutaml/model/serialize.rb', line 687

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

#mixed?Boolean

Returns:

  • (Boolean)


679
680
681
# File 'lib/lutaml/model/serialize.rb', line 679

def mixed?
  !!@mixed
end

#ordered?Boolean

Returns:

  • (Boolean)


675
676
677
# File 'lib/lutaml/model/serialize.rb', line 675

def ordered?
  !!@ordered
end

#pretty_print_instance_variablesObject



691
692
693
# File 'lib/lutaml/model/serialize.rb', line 691

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

#to_yaml_hashObject



695
696
697
# File 'lib/lutaml/model/serialize.rb', line 695

def to_yaml_hash
  self.class.as_yaml(self)
end

#using_default?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


654
655
656
# File 'lib/lutaml/model/serialize.rb', line 654

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

#using_default_for(attribute_name) ⇒ Object



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

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

#validate_attribute!(attr_name) ⇒ Object



669
670
671
672
673
# File 'lib/lutaml/model/serialize.rb', line 669

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



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

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