Class: Uinit::Type::Check

Inherits:
Base
  • Object
show all
Defined in:
lib/uinit/type/check.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, [], #is!, #to_s, #trace!, #type_error!

Methods included from Operators

#&, #|

Constructor Details

#initialize(check) ⇒ Check

Returns a new instance of Check.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
# File 'lib/uinit/type/check.rb', line 14

def initialize(check)
  super()

  raise ArgumentError, 'check must be a Proc' unless self.class.from?(check)

  @check = check
end

Instance Attribute Details

#checkObject (readonly)

Returns the value of attribute check.



22
23
24
# File 'lib/uinit/type/check.rb', line 22

def check
  @check
end

Class Method Details

.from(check) ⇒ Object



10
11
12
# File 'lib/uinit/type/check.rb', line 10

def self.from(check)
  new(check) if from?(check)
end

.from?(check) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/uinit/type/check.rb', line 6

def self.from?(check)
  check.is_a?(Proc)
end

Instance Method Details

#check!(value, depth) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/uinit/type/check.rb', line 30

def check!(value, depth)
  res = check[value]

  return value if res == true || res.nil?

  file, line = check.source_location

  type_error!(
    "#{value.inspect} does not respect rule specified in #{file}:#{line}",
    depth
  )
end

#inspectObject



43
44
45
46
47
# File 'lib/uinit/type/check.rb', line 43

def inspect
  file, line = check.source_location

  "#{super}[#{file}:#{line}]"
end

#is?(value) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/uinit/type/check.rb', line 24

def is?(value)
  res = check[value]

  res == true || res.nil?
end