Module: Lutaml::Model::Serialize

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



629
630
631
632
633
634
635
636
637
638
# File 'lib/lutaml/model/serialize.rb', line 629

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

#__registerObject

Returns the value of attribute __register.



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

def __register
  @__register
end

#element_orderObject

Returns the value of attribute element_order.



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

def element_order
  @element_order
end

#encodingObject

Returns the value of attribute encoding.



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

def encoding
  @encoding
end

#mixed=(value) ⇒ Object (writeonly)

Sets the attribute mixed

Parameters:

  • value

    the value to set the attribute mixed to.



587
588
589
# File 'lib/lutaml/model/serialize.rb', line 587

def mixed=(value)
  @mixed = value
end

#ordered=(value) ⇒ Object (writeonly)

Sets the attribute ordered

Parameters:

  • value

    the value to set the attribute ordered to.



587
588
589
# File 'lib/lutaml/model/serialize.rb', line 587

def ordered=(value)
  @ordered = value
end

#schema_locationObject

Returns the value of attribute schema_location.



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

def schema_location
  @schema_location
end

Class Method Details

.included(base) ⇒ Object



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

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

.register_format_mapping_method(format) ⇒ Object



554
555
556
557
558
559
560
# File 'lib/lutaml/model/serialize.rb', line 554

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



562
563
564
565
566
567
568
569
570
# File 'lib/lutaml/model/serialize.rb', line 562

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



572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/lutaml/model/serialize.rb', line 572

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 = {}|
    to_format(format, options)
  end
end

Instance Method Details

#attr_value(attrs, name, attribute) ⇒ Object



612
613
614
615
# File 'lib/lutaml/model/serialize.rb', line 612

def attr_value(attrs, name, attribute)
  value = Utils.fetch_str_or_sym(attrs, name, attribute.default(__register))
  attribute.cast_value(value, __register)
end

#attribute_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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

#extract_register_id(attrs, options) ⇒ Object



599
600
601
602
# File 'lib/lutaml/model/serialize.rb', line 599

def extract_register_id(attrs, options)
  register = attrs&.dig(:__register) || options&.dig(:register)
  self.class.extract_register_id(register)
end

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



589
590
591
592
593
594
595
596
597
# File 'lib/lutaml/model/serialize.rb', line 589

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

  @__register = extract_register_id(attrs, options)
  set_ordering(attrs)
  set_schema_location(attrs)
  initialize_attributes(attrs, options)
end

#key_exist?(hash, key) ⇒ Boolean

Returns:

  • (Boolean)


665
666
667
# File 'lib/lutaml/model/serialize.rb', line 665

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

#key_value(hash, key) ⇒ Object



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

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

#mixed?Boolean

Returns:

  • (Boolean)


661
662
663
# File 'lib/lutaml/model/serialize.rb', line 661

def mixed?
  !!@mixed
end

#ordered?Boolean

Returns:

  • (Boolean)


657
658
659
# File 'lib/lutaml/model/serialize.rb', line 657

def ordered?
  !!@ordered
end

#pretty_print_instance_variablesObject



673
674
675
# File 'lib/lutaml/model/serialize.rb', line 673

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

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

Returns:

  • (Boolean)


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

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

#to_format(format, options = {}) ⇒ Object



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

def to_format(format, options = {})
  validate_root_mapping!(format, options)

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

#to_yaml_hashObject



677
678
679
# File 'lib/lutaml/model/serialize.rb', line 677

def to_yaml_hash
  self.class.as_yaml(self)
end

#using_default?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#using_default_for(attribute_name) ⇒ Object



617
618
619
# File 'lib/lutaml/model/serialize.rb', line 617

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

#validate_attribute!(attr_name) ⇒ Object



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

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



604
605
606
607
608
609
610
# File 'lib/lutaml/model/serialize.rb', line 604

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

#value_set_for(attribute_name) ⇒ Object



621
622
623
# File 'lib/lutaml/model/serialize.rb', line 621

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