Class: Isomorphic::Node::Guard

Inherits:
AbstractNode show all
Defined in:
lib/isomorphic/node.rb

Direct Known Subclasses

GuardCollection, GuardMember

Instance Attribute Summary collapse

Attributes inherited from AbstractNode

#factory, #inflector, #options, #parent

Instance Method Summary collapse

Methods inherited from AbstractNode

#to_isomorphism_for

Methods included from Internal::DeepEquals

#all_attributes_eql?

Constructor Details

#initialize(parent, lens, *values, **options, &block) ⇒ Guard

Returns a new instance of Guard.



868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
# File 'lib/isomorphic/node.rb', line 868

def initialize(parent, lens, *values, **options, &block)
  super(parent, **options, &block)

  unless (lens.is_a?(Isomorphic::Lens::Association) && lens.last_lens.is_a?(Isomorphic::Lens::Attribute)) || lens.is_a?(Isomorphic::Lens::Attribute)
    raise Isomorphic::LensInvalid.new(nil, lens)
  end

  if (values.size > 1) && !options.key?(:default)
    raise Isomorphic::DefaultOptionNotFound.new(nil)
  elsif options.key?(:default) && !values.include?(options[:default])
    raise Isomorphic::DefaultOptionNotIncluded.new(nil)
  end

  @lens = lens
  @values = values
end

Instance Attribute Details

#lensObject (readonly)

Returns the value of attribute lens.



864
865
866
# File 'lib/isomorphic/node.rb', line 864

def lens
  @lens
end

#valuesObject (readonly)

Returns the value of attribute values.



866
867
868
# File 'lib/isomorphic/node.rb', line 866

def values
  @values
end

Instance Method Details

#from_isomorphism(scope, instance, record = nil, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new) ⇒ Object



885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
# File 'lib/isomorphic/node.rb', line 885

def from_isomorphism(scope, instance, record = nil, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new)
  is_present = @__child_nodes.inject(false) { |acc, child_node| child_node.from_isomorphism(scope, instance, record, xmlattr_acc).nil? ? acc : true }

  if is_present
    value = \
      case values.size
        when 1 then values[0]
        else options[:default]
      end

    lens.set(record, value, xmlattr_acc)

    record
  else
    nil
  end
end

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



903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
# File 'lib/isomorphic/node.rb', line 903

def to_isomorphism(scope, record, instance = nil)
  lens.get(record).try { |value_or_hash|
    is_valid = \
      if value_or_hash.is_a?(::Hash)
        value_or_hash.values.all? { |value| values.include?(value) }
      else
        values.include?(value_or_hash)
      end

    if is_valid
      is_present = @__child_nodes.inject(false) { |acc, child_node| child_node.to_isomorphism(scope, record, instance).nil? ? acc : true }

      if is_present || options[:allow_blank]
        instance
      else
        nil
      end
    else
      nil
    end
  }
end