Class: Isomorphic::Node::AbstractNode Abstract

Inherits:
Object
  • Object
show all
Includes:
Internal::DeepEquals
Defined in:
lib/isomorphic/node.rb

Overview

This class is abstract.

Subclass and override #from_isomorphism and #to_isomorphism to implement a custom class.

Generic base class for Isomorphic nodes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Internal::DeepEquals

#all_attributes_eql?

Constructor Details

#initialize(parent, *args) {|| ... } ⇒ AbstractNode

Default constructor.

Parameters:

Yield Parameters:

  • (self)

Yield Returns:

  • (void)


506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/isomorphic/node.rb', line 506

def initialize(parent, *args, &block)
  super()

  @__cache_for_to_isomorphism_for = {}
  @__child_nodes = []

  @parent = parent

  @options = args.extract_options!

  unless block.nil?
    self.instance_exec(*args, &block)
  end
end

Instance Attribute Details

#factoryIsomorphic::Factory::AbstractFactory (readonly)

Returns the Isomorphic factory.

Returns:



525
526
527
528
529
530
531
532
533
# File 'lib/isomorphic/node.rb', line 525

%i(factory inflector).each do |method_name|
  define_method(method_name) do
    if parent.is_a?(Isomorphic::Node::AbstractNode)
      parent.send(method_name)
    else
      instance_variable_get(:"@#{method_name}")
    end
  end
end

#inflectorIsomorphic::Inflector::AbstractInflector (readonly)

Returns the Isomorphic inflector.

Returns:



525
526
527
528
529
530
531
532
533
# File 'lib/isomorphic/node.rb', line 525

%i(factory inflector).each do |method_name|
  define_method(method_name) do
    if parent.is_a?(Isomorphic::Node::AbstractNode)
      parent.send(method_name)
    else
      instance_variable_get(:"@#{method_name}")
    end
  end
end

#optionsObject (readonly)

Returns the value of attribute options.



498
499
500
# File 'lib/isomorphic/node.rb', line 498

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



494
495
496
# File 'lib/isomorphic/node.rb', line 494

def parent
  @parent
end

Instance Method Details

#from_isomorphism(scope, isomorphism_instance, record = nil, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new) ⇒ ActiveRecord::Base

Converts the given instance to an Active Record record.

Parameters:

  • scope (Isomorphic::Node::Internal::Scope)

    the scope

  • isomorphism_instance (Object)

    the instance

  • record (ActiveRecord::Base) (defaults to: nil)

    the record (optional; when not provided, a new instance of the Active Record class is constructed)

  • xmlattr_acc (ActiveSupport::HashWithIndifferentAccess) (defaults to: ::ActiveSupport::HashWithIndifferentAccess.new)

    the accumulator for XML attributes

Returns:

  • (ActiveRecord::Base)

    the record

Raises:

  • (::NotImplementedError)


542
543
544
# File 'lib/isomorphic/node.rb', line 542

def from_isomorphism(scope, isomorphism_instance, record = nil, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new)
  raise ::NotImplementedError
end

#to_isomorphism(scope, record, isomorphism_instance = nil) ⇒ Object

Converts the given Active Record record to an instance.

Parameters:

  • scope (Isomorphic::Node::Internal::Scope)

    the scope

  • record (ActiveRecord::Base)

    the record

  • isomorphism_instance (Object) (defaults to: nil)

    the instance (optional; when not provided, a new instance is constructed)

Returns:

  • (Object)

    the instance

Raises:

  • (::NotImplementedError)


552
553
554
# File 'lib/isomorphic/node.rb', line 552

def to_isomorphism(scope, record, isomorphism_instance = nil)
  raise ::NotImplementedError
end

#to_isomorphism_for(record, *args) ⇒ Object

Returns the cached instance for the given Active Record record.

Parameters:

  • record (ActiveRecord::Base)

    the record

  • args (Array<#to_s>)

    the method names

Returns:

  • (Object)

    the cached instance



561
562
563
564
565
# File 'lib/isomorphic/node.rb', line 561

def to_isomorphism_for(record, *args)
  args.inject(@__cache_for_to_isomorphism_for[record]) { |acc, arg|
    acc.try(:[], arg)
  }
end