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



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

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.



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

#registerObject

Returns the value of attribute register.



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

def register
  @register
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



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

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)


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

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

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

#extract_register_id(register) ⇒ Object



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

def extract_register_id(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(options[:register])
  set_ordering(attrs)
  set_schema_location(attrs)
  initialize_attributes(attrs, options)
end

#key_exist?(hash, key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#key_value(hash, key) ⇒ Object



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

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

#mixed?Boolean

Returns:

  • (Boolean)


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

def mixed?
  !!@mixed
end

#ordered?Boolean

Returns:

  • (Boolean)


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

def ordered?
  !!@ordered
end

#pretty_print_instance_variablesObject



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

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

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

Returns:

  • (Boolean)


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

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



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

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



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

def to_yaml_hash
  self.class.as_yaml(self)
end

#using_default?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#using_default_for(attribute_name) ⇒ Object



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

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

#validate_attribute!(attr_name) ⇒ Object



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

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



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

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

#value_set_for(attribute_name) ⇒ Object



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

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