Class: Shale::Type::Complex Private
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Build complex object. Don’t use it directly. It serves as a base type class for @see Shale::Mapper
Direct Known Subclasses
Class Method Summary collapse
-
.as_xml(instance, node_name = nil, doc = nil, _cdata = nil, only: nil, except: nil, context: nil, version: nil) ⇒ ::REXML::Document, ...
Convert Object to XML document.
-
.from_csv(csv, only: nil, except: nil, context: nil, headers: false, **csv_options) ⇒ model instance
Convert CSV to Object.
-
.from_json(json, only: nil, except: nil, context: nil, **json_options) ⇒ model instance
Convert JSON to Object.
-
.from_toml(toml, only: nil, except: nil, context: nil, **toml_options) ⇒ model instance
Convert TOML to Object.
-
.from_xml(xml, only: nil, except: nil, context: nil) ⇒ model instance
Convert XML to Object.
-
.from_yaml(yaml, only: nil, except: nil, context: nil, **yaml_options) ⇒ model instance
Convert YAML to Object.
-
.of_xml(element, only: nil, except: nil, context: nil) ⇒ model instance
Convert XML document to Object.
-
.to_csv(instance, only: nil, except: nil, context: nil, headers: false, **csv_options) ⇒ String
Convert Object to CSV.
-
.to_json(instance, only: nil, except: nil, context: nil, pretty: false, **json_options) ⇒ String
Convert Object to JSON.
-
.to_toml(instance, only: nil, except: nil, context: nil, **toml_options) ⇒ String
Convert Object to TOML.
-
.to_xml(instance, only: nil, except: nil, context: nil, pretty: false, declaration: false, encoding: false) ⇒ String
Convert Object to XML.
-
.to_yaml(instance, only: nil, except: nil, context: nil, **yaml_options) ⇒ String
Convert Object to YAML.
Instance Method Summary collapse
-
#to_csv(only: nil, except: nil, context: nil, headers: false, **csv_options) ⇒ String
Convert Object to CSV.
-
#to_hash(only: nil, except: nil, context: nil) ⇒ Hash
Convert Object to Hash.
-
#to_json(only: nil, except: nil, context: nil, pretty: false, **json_options) ⇒ String
Convert Object to JSON.
-
#to_toml(only: nil, except: nil, context: nil, **toml_options) ⇒ String
Convert Object to TOML.
-
#to_xml(only: nil, except: nil, context: nil, pretty: false, declaration: false, encoding: false) ⇒ String
Convert Object to XML.
-
#to_yaml(only: nil, except: nil, context: nil, **yaml_options) ⇒ String
Convert Object to YAML.
Methods inherited from Value
as_csv, as_hash, as_json, as_toml, as_xml_value, as_yaml, cast, of_csv, of_hash, of_json, of_toml, of_yaml
Class Method Details
.as_xml(instance, node_name = nil, doc = nil, _cdata = nil, only: nil, except: nil, context: nil, version: nil) ⇒ ::REXML::Document, ...
Convert Object to XML document
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 |
# File 'lib/shale/type/complex.rb', line 646 def as_xml( instance, node_name = nil, doc = nil, _cdata = nil, only: nil, except: nil, context: nil, version: nil ) unless instance.is_a?(model) msg = "argument is a '#{instance.class}' but should be a '#{model}'" raise IncorrectModelError, msg end unless doc doc = Shale.xml_adapter.create_document(version) element = as_xml( instance, xml_mapping.prefixed_root, doc, only: only, except: except, context: context ) doc.add_element(doc.doc, element) return doc.doc end element = doc.create_element(node_name) doc.add_namespace( xml_mapping.default_namespace.prefix, xml_mapping.default_namespace.name ) grouping = Shale::Mapping::Group::XmlGrouping.new only = to_partial_render_attributes(only) except = to_partial_render_attributes(except) xml_mapping.attributes.each_value do |mapping| if mapping.group grouping.add(mapping, :attribute, nil) elsif mapping.method_to mapper = new if mapper.method(mapping.method_to).arity == 4 mapper.send(mapping.method_to, instance, element, doc, context) else mapper.send(mapping.method_to, instance, element, doc) end else if mapping.receiver receiver = instance.send(mapping.receiver) else receiver = instance end receiver_attributes = get_receiver_attributes(mapping) attribute = receiver_attributes[mapping.attribute] next unless attribute next if only && !only.key?(attribute.name) next if except&.key?(attribute.name) value = receiver.send(attribute.name) if receiver if mapping.render_nil? || !value.nil? doc.add_namespace(mapping.namespace.prefix, mapping.namespace.name) doc.add_attribute(element, mapping.prefixed_name, value) end end end content_mapping = xml_mapping.content if content_mapping if content_mapping.group grouping.add(content_mapping, :content, nil) elsif content_mapping.method_to mapper = new if mapper.method(content_mapping.method_to).arity == 4 mapper.send(content_mapping.method_to, instance, element, doc, context) else mapper.send(content_mapping.method_to, instance, element, doc) end else if content_mapping.receiver receiver = instance.send(content_mapping.receiver) else receiver = instance end receiver_attributes = get_receiver_attributes(content_mapping) attribute = receiver_attributes[content_mapping.attribute] if attribute skip = false # rubocop:disable Metrics/BlockNesting skip = true if only && !only.key?(attribute.name) skip = true if except&.key?(attribute.name) unless skip value = receiver.send(attribute.name) if receiver if content_mapping.cdata doc.create_cdata(value.to_s, element) else doc.add_text(element, value.to_s) end end # rubocop:enable Metrics/BlockNesting end end end xml_mapping.elements.each_value do |mapping| if mapping.group grouping.add(mapping, :element, nil) elsif mapping.method_to mapper = new if mapper.method(mapping.method_to).arity == 4 mapper.send(mapping.method_to, instance, element, doc, context) else mapper.send(mapping.method_to, instance, element, doc) end else if mapping.receiver receiver = instance.send(mapping.receiver) else receiver = instance end receiver_attributes = get_receiver_attributes(mapping) attribute = receiver_attributes[mapping.attribute] next unless attribute if only attribute_only = only[attribute.name] next unless only.key?(attribute.name) end if except attribute_except = except[attribute.name] next if except.key?(attribute.name) && attribute_except.nil? end value = receiver.send(attribute.name) if receiver if mapping.render_nil? || !value.nil? doc.add_namespace(mapping.namespace.prefix, mapping.namespace.name) end if value.nil? if mapping.render_nil? child = doc.create_element(mapping.prefixed_name) doc.add_element(element, child) end elsif attribute.collection? [*value].each do |v| next if v.nil? child = attribute.type.as_xml( v, mapping.prefixed_name, doc, mapping.cdata, only: attribute_only, except: attribute_except, context: context ) doc.add_element(element, child) end else child = attribute.type.as_xml( value, mapping.prefixed_name, doc, mapping.cdata, only: attribute_only, except: attribute_except, context: context ) doc.add_element(element, child) end end end grouping.each do |group| mapper = new if mapper.method(group.method_to).arity == 4 mapper.send(group.method_to, instance, element, doc, context) else mapper.send(group.method_to, instance, element, doc) end end element end |
.from_csv(csv, only: nil, except: nil, context: nil, headers: false, **csv_options) ⇒ model instance
Convert CSV to Object
383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/shale/type/complex.rb', line 383 def from_csv(csv, only: nil, except: nil, context: nil, headers: false, **) validate_csv_adapter data = Shale.csv_adapter.load(csv, **.merge(headers: csv_mapping.keys.keys)) data.shift if headers of_csv( data, only: only, except: except, context: context ) end |
.from_json(json, only: nil, except: nil, context: nil, **json_options) ⇒ model instance
Convert JSON to Object
265 266 267 268 269 270 271 272 |
# File 'lib/shale/type/complex.rb', line 265 def from_json(json, only: nil, except: nil, context: nil, **) of_json( Shale.json_adapter.load(json, **), only: only, except: except, context: context ) end |
.from_toml(toml, only: nil, except: nil, context: nil, **toml_options) ⇒ model instance
Convert TOML to Object
342 343 344 345 346 347 348 349 350 |
# File 'lib/shale/type/complex.rb', line 342 def from_toml(toml, only: nil, except: nil, context: nil, **) validate_toml_adapter of_toml( Shale.toml_adapter.load(toml, **), only: only, except: except, context: context ) end |
.from_xml(xml, only: nil, except: nil, context: nil) ⇒ model instance
Convert XML to Object
622 623 624 625 626 627 628 629 630 |
# File 'lib/shale/type/complex.rb', line 622 def from_xml(xml, only: nil, except: nil, context: nil) validate_xml_adapter of_xml( Shale.xml_adapter.load(xml), only: only, except: except, context: context ) end |
.from_yaml(yaml, only: nil, except: nil, context: nil, **yaml_options) ⇒ model instance
Convert YAML to Object
304 305 306 307 308 309 310 311 |
# File 'lib/shale/type/complex.rb', line 304 def from_yaml(yaml, only: nil, except: nil, context: nil, **) of_yaml( Shale.yaml_adapter.load(yaml, **), only: only, except: except, context: context ) end |
.of_xml(element, only: nil, except: nil, context: nil) ⇒ model instance
Convert XML document to Object
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 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 575 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 601 602 603 604 605 606 607 608 |
# File 'lib/shale/type/complex.rb', line 442 def of_xml(element, only: nil, except: nil, context: nil) instance = model.new attributes .values .select { |attr| attr.default } .each { |attr| instance.send(attr.setter, attr.default.call) } grouping = Shale::Mapping::Group::XmlGrouping.new delegates = Shale::Mapping::Delegates.new only = to_partial_render_attributes(only) except = to_partial_render_attributes(except) element.attributes.each do |key, value| mapping = xml_mapping.attributes[key.to_s] next unless mapping if mapping.group grouping.add(mapping, :attribute, value) elsif mapping.method_from mapper = new if mapper.method(mapping.method_from).arity == 3 mapper.send(mapping.method_from, instance, value, context) else mapper.send(mapping.method_from, instance, value) end else receiver_attributes = get_receiver_attributes(mapping) attribute = receiver_attributes[mapping.attribute] next unless attribute next if only && !only.key?(attribute.name) next if except&.key?(attribute.name) casted_value = attribute.type.cast(value) if attribute.collection? if mapping.receiver delegates.add_collection( attributes[mapping.receiver], attribute.setter, casted_value ) else instance.send(attribute.name) << casted_value end elsif mapping.receiver delegates.add(attributes[mapping.receiver], attribute.setter, casted_value) else instance.send(attribute.setter, casted_value) end end end content_mapping = xml_mapping.content if content_mapping if content_mapping.group grouping.add(content_mapping, :content, element) elsif content_mapping.method_from mapper = new if mapper.method(content_mapping.method_from).arity == 3 mapper.send(content_mapping.method_from, instance, element, context) else mapper.send(content_mapping.method_from, instance, element) end else receiver_attributes = get_receiver_attributes(content_mapping) attribute = receiver_attributes[content_mapping.attribute] if attribute skip = false # rubocop:disable Metrics/BlockNesting skip = true if only && !only.key?(attribute.name) skip = true if except&.key?(attribute.name) unless skip value = attribute.type.cast(attribute.type.of_xml(element)) if content_mapping.receiver delegates.add(attributes[content_mapping.receiver], attribute.setter, value) else instance.send(attribute.setter, value) end end # rubocop:enable Metrics/BlockNesting end end end element.children.each do |node| mapping = xml_mapping.elements[node.name] next unless mapping if mapping.group grouping.add(mapping, :element, node) elsif mapping.method_from mapper = new if mapper.method(mapping.method_from).arity == 3 mapper.send(mapping.method_from, instance, node, context) else mapper.send(mapping.method_from, instance, node) end else receiver_attributes = get_receiver_attributes(mapping) attribute = receiver_attributes[mapping.attribute] next unless attribute if only attribute_only = only[attribute.name] next unless only.key?(attribute.name) end if except attribute_except = except[attribute.name] next if except.key?(attribute.name) && attribute_except.nil? end value = attribute.type.of_xml( node, only: attribute_only, except: attribute_except, context: context ) casted_value = attribute.type.cast(value) if attribute.collection? if mapping.receiver delegates.add_collection( attributes[mapping.receiver], attribute.setter, casted_value ) else instance.send(attribute.name) << casted_value end elsif mapping.receiver delegates.add(attributes[mapping.receiver], attribute.setter, casted_value) else instance.send(attribute.setter, casted_value) end end end delegates.each do |delegate| receiver = get_receiver(instance, delegate.receiver_attribute) receiver.send(delegate.setter, delegate.value) end grouping.each do |group| mapper = new if mapper.method(group.method_from).arity == 3 mapper.send(group.method_from, instance, group.dict, context) else mapper.send(group.method_from, instance, group.dict) end end instance end |
.to_csv(instance, only: nil, except: nil, context: nil, headers: false, **csv_options) ⇒ String
Convert Object to CSV
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
# File 'lib/shale/type/complex.rb', line 409 def to_csv(instance, only: nil, except: nil, context: nil, headers: false, **) validate_csv_adapter data = as_csv([*instance], only: only, except: except, context: context) cols = csv_mapping.keys.values if only cols = cols.select { |e| only.include?(e.attribute) } end if except cols = cols.reject { |e| except.include?(e.attribute) } end cols = cols.map(&:name) if headers data.prepend(cols.to_h { |e| [e, e] }) end Shale.csv_adapter.dump(data, **.merge(headers: cols)) end |
.to_json(instance, only: nil, except: nil, context: nil, pretty: false, **json_options) ⇒ String
Convert Object to JSON
286 287 288 289 290 291 |
# File 'lib/shale/type/complex.rb', line 286 def to_json(instance, only: nil, except: nil, context: nil, pretty: false, **) Shale.json_adapter.dump( as_json(instance, only: only, except: except, context: context), **.merge(pretty: pretty) ) end |
.to_toml(instance, only: nil, except: nil, context: nil, **toml_options) ⇒ String
Convert Object to TOML
363 364 365 366 367 368 369 |
# File 'lib/shale/type/complex.rb', line 363 def to_toml(instance, only: nil, except: nil, context: nil, **) validate_toml_adapter Shale.toml_adapter.dump( as_toml(instance, only: only, except: except, context: context), ** ) end |
.to_xml(instance, only: nil, except: nil, context: nil, pretty: false, declaration: false, encoding: false) ⇒ String
Convert Object to XML
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 |
# File 'lib/shale/type/complex.rb', line 867 def to_xml( instance, only: nil, except: nil, context: nil, pretty: false, declaration: false, encoding: false ) validate_xml_adapter Shale.xml_adapter.dump( as_xml(instance, only: only, except: except, context: context, version: declaration), pretty: pretty, declaration: declaration, encoding: encoding ) end |
.to_yaml(instance, only: nil, except: nil, context: nil, **yaml_options) ⇒ String
Convert Object to YAML
324 325 326 327 328 329 |
# File 'lib/shale/type/complex.rb', line 324 def to_yaml(instance, only: nil, except: nil, context: nil, **) Shale.yaml_adapter.dump( as_yaml(instance, only: only, except: except, context: context), ** ) end |
Instance Method Details
#to_csv(only: nil, except: nil, context: nil, headers: false, **csv_options) ⇒ String
Convert Object to CSV
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 |
# File 'lib/shale/type/complex.rb', line 1066 def to_csv(only: nil, except: nil, context: nil, headers: false, **) self.class.to_csv( self, only: only, except: except, context: context, headers: headers, ** ) end |
#to_hash(only: nil, except: nil, context: nil) ⇒ Hash
Convert Object to Hash
1001 1002 1003 |
# File 'lib/shale/type/complex.rb', line 1001 def to_hash(only: nil, except: nil, context: nil) self.class.to_hash(self, only: only, except: except, context: context) end |
#to_json(only: nil, except: nil, context: nil, pretty: false, **json_options) ⇒ String
Convert Object to JSON
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 |
# File 'lib/shale/type/complex.rb', line 1016 def to_json(only: nil, except: nil, context: nil, pretty: false, **) self.class.to_json( self, only: only, except: except, context: context, pretty: pretty, ** ) end |
#to_toml(only: nil, except: nil, context: nil, **toml_options) ⇒ String
Convert Object to TOML
1051 1052 1053 |
# File 'lib/shale/type/complex.rb', line 1051 def to_toml(only: nil, except: nil, context: nil, **) self.class.to_toml(self, only: only, except: except, context: context, **) end |
#to_xml(only: nil, except: nil, context: nil, pretty: false, declaration: false, encoding: false) ⇒ String
Convert Object to XML
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 |
# File 'lib/shale/type/complex.rb', line 1089 def to_xml( only: nil, except: nil, context: nil, pretty: false, declaration: false, encoding: false ) self.class.to_xml( self, only: only, except: except, context: context, pretty: pretty, declaration: declaration, encoding: encoding ) end |
#to_yaml(only: nil, except: nil, context: nil, **yaml_options) ⇒ String
Convert Object to YAML
1037 1038 1039 |
# File 'lib/shale/type/complex.rb', line 1037 def to_yaml(only: nil, except: nil, context: nil, **) self.class.to_yaml(self, only: only, except: except, context: context, **) end |