Class: Kanrisuru::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/kanrisuru/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, &block) ⇒ Result

Returns a new instance of Result.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kanrisuru/result.rb', line 9

def initialize(command, &block)
  @command = command
  @data = nil

  @data = block.call(@command) if @command.success? && block_given?
  @error = @command.to_a if @command.failure?

  ## Define getter methods on result that maps to
  ## the same methods of a data struct.
  return unless @command.success? && Kanrisuru::Util.present?(@data) && @data.class.ancestors.include?(Struct)

  method_names = @data.members
  self.class.class_eval do
    method_names.each do |method_name|
      define_method method_name do
        @data[method_name]
      end
    end
  end
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



7
8
9
# File 'lib/kanrisuru/result.rb', line 7

def command
  @command
end

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/kanrisuru/result.rb', line 7

def data
  @data
end

#errorObject (readonly)

Returns the value of attribute error.



7
8
9
# File 'lib/kanrisuru/result.rb', line 7

def error
  @error
end

Instance Method Details

#[](prop) ⇒ Object



30
31
32
# File 'lib/kanrisuru/result.rb', line 30

def [](prop)
  @data[prop]
end

#each(&block) ⇒ Object



98
99
100
# File 'lib/kanrisuru/result.rb', line 98

def each(&block)
  @data.each { |item| block.call(item) } if @data.instance_of?(Array)
end

#failure?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/kanrisuru/result.rb', line 86

def failure?
  @command.failure?
end

#inspectObject



76
77
78
79
80
81
82
83
84
# File 'lib/kanrisuru/result.rb', line 76

def inspect
  if success?
    format('#<Kanrisuru::Result:0x%<object_id>s @status=%<status>s @data=%<data>s @command=%<command>s>',
           object_id: object_id, status: status, data: @data.inspect, command: command.prepared_command)
  else
    format('#<Kanrisuru::Result:0x%<object_id>s @status=%<status>s @error=%<error>s @command=%<command>s>',
           object_id: object_id, status: status, error: @error.inspect, command: command.prepared_command)
  end
end

#statusObject



94
95
96
# File 'lib/kanrisuru/result.rb', line 94

def status
  @command.exit_status
end

#success?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/kanrisuru/result.rb', line 90

def success?
  @command.success?
end

#to_aObject



42
43
44
# File 'lib/kanrisuru/result.rb', line 42

def to_a
  @data.instance_of?(Array) ? @data : [@data]
end

#to_fObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kanrisuru/result.rb', line 46

def to_f
  case @data
  when Numeric
    @data
  when String
    @data.to_f
  when Array
    @data.map(&:to_f)
  when NilClass
    nil
  else
    raise NoMethodError, "(undefined method `to_f' for Kanrisuru::Result)"
  end
end

#to_hObject



38
39
40
# File 'lib/kanrisuru/result.rb', line 38

def to_h
  @data.to_h
end

#to_iObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kanrisuru/result.rb', line 61

def to_i
  case @data
  when Integer
    @data
  when String
    @data.to_i
  when Array
    @data.map(&:to_i)
  when NilClass
    nil
  else
    raise NoMethodError, "(undefined method `to_i' for Kanrisuru::Result)"
  end
end

#to_sObject



34
35
36
# File 'lib/kanrisuru/result.rb', line 34

def to_s
  @data.to_s
end