Module: Lutaml::Model::Serialize
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#to_liquid
Methods included from Validation
#validate, #validate!, #validate_helper
#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_order ⇒ Object
Returns the value of attribute element_order.
586
587
588
|
# File 'lib/lutaml/model/serialize.rb', line 586
def element_order
@element_order
end
|
#encoding ⇒ Object
Returns the value of attribute encoding.
586
587
588
|
# File 'lib/lutaml/model/serialize.rb', line 586
def encoding
@encoding
end
|
#mixed=(value) ⇒ Object
587
588
589
|
# File 'lib/lutaml/model/serialize.rb', line 587
def mixed=(value)
@mixed = value
end
|
#ordered=(value) ⇒ Object
Sets the attribute ordered
587
588
589
|
# File 'lib/lutaml/model/serialize.rb', line 587
def ordered=(value)
@ordered = value
end
|
#register ⇒ Object
Returns the value of attribute register.
586
587
588
|
# File 'lib/lutaml/model/serialize.rb', line 586
def register
@register
end
|
#schema_location ⇒ Object
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
|
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
|
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
|
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
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
|
599
600
601
|
# File 'lib/lutaml/model/serialize.rb', line 599
def (register)
self.class.(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 = (options[:register])
set_ordering(attrs)
set_schema_location(attrs)
initialize_attributes(attrs, options)
end
|
#key_exist?(hash, key) ⇒ 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
660
661
662
|
# File 'lib/lutaml/model/serialize.rb', line 660
def mixed?
!!@mixed
end
|
#ordered? ⇒ Boolean
656
657
658
|
# File 'lib/lutaml/model/serialize.rb', line 656
def ordered?
!!@ordered
end
|
#pretty_print_instance_variables ⇒ Object
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
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
|
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_hash ⇒ Object
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
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
|