Class: Isomorphic::Node::Guard
- Inherits:
-
AbstractNode
- Object
- AbstractNode
- Isomorphic::Node::Guard
- Defined in:
- lib/isomorphic/node.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#lens ⇒ Object
readonly
Returns the value of attribute lens.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
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, lens, *values, **options, &block) ⇒ Guard
constructor
A new instance of Guard.
- #to_isomorphism(scope, record, instance = nil) ⇒ Object
Methods inherited from AbstractNode
Methods included from Internal::DeepEquals
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, **, &block) super(parent, **, &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) && !.key?(:default) raise Isomorphic::DefaultOptionNotFound.new(nil) elsif .key?(:default) && !values.include?([:default]) raise Isomorphic::DefaultOptionNotIncluded.new(nil) end @lens = lens @values = values end |
Instance Attribute Details
#lens ⇒ Object (readonly)
Returns the value of attribute lens.
864 865 866 |
# File 'lib/isomorphic/node.rb', line 864 def lens @lens end |
#values ⇒ Object (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 [: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 || [:allow_blank] instance else nil end else nil end } end |