Class: Camille::PickAndOmit

Inherits:
BasicType show all
Defined in:
lib/camille/pick_and_omit.rb

Overview

shared base class for Pick and Omit

Direct Known Subclasses

Types::Omit, Types::Pick

Defined Under Namespace

Classes: ArgumentError

Instance Attribute Summary

Attributes inherited from BasicType

#fingerprint

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicType

&, #&, #[], directly_instantiable?, inherited, instance, #transform, #|, |

Constructor Details

#initialize(type, keys) ⇒ PickAndOmit

Returns a new instance of PickAndOmit.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/camille/pick_and_omit.rb', line 6

def initialize type, keys
  @type = Camille::Type.instance(type)
  case
  when @type.is_a?(Camille::Types::Object)
    @target_object = @type
  when @type.is_a?(Camille::Type) && @type.underlying.is_a?(Camille::Types::Object)
    @target_object = @type.underlying
  else
    raise ArgumentError.new("Currently onle an object type or an alias of object type is supported in #{klass_name}. Got #{type.inspect}.")
  end

  unless keys.is_a?(::Array) && !keys.empty? && keys.all?{|x| x.is_a? Symbol}
    raise ArgumentError.new("The second argument of #{klass_name} has to be an array of symbols. Got #{keys.inspect}.")
  end
  @keys = keys
  @fingerprint = Digest::MD5.hexdigest "#{self.class.name}#{@type.fingerprint}#{@keys.sort}"
end

Class Method Details

.[](type, keys) ⇒ Object



32
33
34
# File 'lib/camille/pick_and_omit.rb', line 32

def self.[] type, keys
  self.new type, keys
end

Instance Method Details

#check(value) ⇒ Object



24
25
26
# File 'lib/camille/pick_and_omit.rb', line 24

def check value
  processed_object.check(value)
end

#literalObject



28
29
30
# File 'lib/camille/pick_and_omit.rb', line 28

def literal
  "#{klass_name}<#{@type.literal}, #{keys_in_literal}>"
end