Class: Isomorphic::Node::Attribute
- Inherits:
-
AbstractNode
- Object
- AbstractNode
- Isomorphic::Node::Attribute
- Includes:
- Internal::InstanceMethodsForMember
- Defined in:
- lib/isomorphic/node.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#xmlattr_class ⇒ Object
readonly
Returns the value of attribute xmlattr_class.
Attributes inherited from AbstractNode
#factory, #inflector, #options, #parent
Instance Method Summary collapse
- #from_isomorphism(scope, instance, record = nil, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new) ⇒ Object
-
#initialize(parent, method_name, xmlattr_class = nil, **options, &block) ⇒ Attribute
constructor
A new instance of Attribute.
- #to_isomorphism(scope, record, instance = nil) ⇒ Object
Methods included from Internal::InstanceMethodsForMember
#attribute, #attribute_for, #collection, #from, #guard_attribute_for, #member, #namespace, #namespace_attribute_for, #to
Methods inherited from AbstractNode
Methods included from Internal::DeepEquals
Constructor Details
#initialize(parent, method_name, xmlattr_class = nil, **options, &block) ⇒ Attribute
Returns a new instance of Attribute.
647 648 649 650 651 652 653 654 655 656 657 |
# File 'lib/isomorphic/node.rb', line 647 def initialize(parent, method_name, xmlattr_class = nil, **, &block) super(parent, **, &block) unless xmlattr_class.nil? || (xmlattr_class.is_a?(::Class) && (xmlattr_class.parents[-2] == parent.inflector.base)) raise Isomorphic::InvalidNodeClass.new(nil, xmlattr_class) end @method_name = method_name @xmlattr_class = xmlattr_class end |
Instance Attribute Details
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
643 644 645 |
# File 'lib/isomorphic/node.rb', line 643 def method_name @method_name end |
#xmlattr_class ⇒ Object (readonly)
Returns the value of attribute xmlattr_class.
645 646 647 |
# File 'lib/isomorphic/node.rb', line 645 def xmlattr_class @xmlattr_class end |
Instance Method Details
#from_isomorphism(scope, instance, record = nil, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new) ⇒ Object
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 |
# File 'lib/isomorphic/node.rb', line 659 def from_isomorphism(scope, instance, record = nil, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new) is_present = false instance_for_node = instance.send(method_name) is_valid = (xmlattr_class.nil? || instance_for_node.instance_of?(xmlattr_class)) && all_attributes_eql?(inflector.base, instance_for_node, [:attributes].try(:each_pair).try(:inject, ::ActiveSupport::HashWithIndifferentAccess.new) { |acc, pair| xmlattr_name, value = *pair acc[:"xmlattr_#{xmlattr_name}"] = value acc }) if is_valid && (!instance_for_node.nil? || [:allow_nil]) _set(scope, record, instance_for_node, xmlattr_acc).try { |record_for_node| is_present ||= true @__child_nodes.inject(false) { |acc, child_node| child_node.from_isomorphism(scope, instance_for_node, record_for_node, xmlattr_acc).nil? ? acc : true } } end if is_present record else nil end end |
#to_isomorphism(scope, record, instance = nil) ⇒ Object
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 |
# File 'lib/isomorphic/node.rb', line 686 def to_isomorphism(scope, record, instance = nil) instance_for_node = _get(scope, record).try { |retval_or_hash| @__cache_for_to_isomorphism_for[record] ||= retval_or_hash if retval_or_hash.is_a?(::Hash) retval_or_hash.values else retval_or_hash end }.try { |retval| if retval.class.parents[-2] == inflector.base @__child_nodes.inject(false) { |acc, child_node| child_node.to_isomorphism(scope, record, retval).nil? ? acc : true } retval else retval.try(:to_s).try { |s| if xmlattr_class.nil? s else instance_for_node = xmlattr_class.new(s) [:attributes].try(:each_pair).try(:inject, {}) { |acc, pair| xmlattr_name, value = *pair acc[:"xmlattr_#{xmlattr_name}"] = value acc }.try { |attributes| factory.update_attributes(instance_for_node, attributes) } @__child_nodes.inject(false) { |acc, child_node| child_node.to_isomorphism(scope, record, instance_for_node).nil? ? acc : true } instance_for_node end } end } if !instance_for_node.nil? || [:allow_nil] instance.send(:"#{method_name}=", instance_for_node) else nil end end |