Class: Iamsure::IamSure

Inherits:
Object
  • Object
show all
Defined in:
lib/iamsure.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ IamSure

Returns a new instance of IamSure.



9
10
11
# File 'lib/iamsure.rb', line 9

def initialize(obj)
  @obj = obj
end

Class Method Details

.of(obj) ⇒ Object



13
14
15
# File 'lib/iamsure.rb', line 13

def self.of(obj)
  new(obj)
end

Instance Method Details

#exist(as = :file) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/iamsure.rb', line 27

def exist(as=:file)
  raise ArgumentError.new 'Argument cannot be nil' if as.nil?
  raise ArgumentError.new "Argument should be Hash as 'as: :file' or 'as: :dir'" if not as.is_a?(Hash)

  if as.has_key?(:as) && as[:as] == :file
    raise ArgumentError.new "File (#{ @obj }) does not exist!" if not File.file?(@obj)
  elsif as.has_key?(:as) && as[:as] == :dir
    raise ArgumentError.new "Dir (#{ @obj }) does not exist!" if not File.directory?(@obj)
  else
    raise ArgumentError.new "Argument (#{ as }) is not valid!"
  end
  self
end

#get(initiator = nil) ⇒ Object



50
51
52
53
54
# File 'lib/iamsure.rb', line 50

def get(initiator=nil)
  return @obj if initiator.nil?
  return Initiator.new(@obj) if initiator.is_a?(Initiator)
  raise ArgumentError.new "initiator should be empty or inherited from Initiator"
end

#not_empty(msg = '') ⇒ Object



22
23
24
25
# File 'lib/iamsure.rb', line 22

def not_empty(msg='')
  raise ArgumentError.new msg if @obj.nil? || @obj.empty?
  self
end

#not_nil(msg = '') ⇒ Object



17
18
19
20
# File 'lib/iamsure.rb', line 17

def not_nil(msg='')
  raise ArgumentError.new msg if @obj.nil?
  self
end

#unpack(unpacker) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/iamsure.rb', line 41

def unpack(unpacker)
  raise ArgumentError.new "Argument cannot be nil!" if unpacker.nil?
  raise ArgumentError.new "Argument should be Unpacker!" unless unpacker.is_a?(Unpacker)

  @obj = unpacker.unpack(@obj)

  self
end