Module: AyeCommander::Inspectable
- Included in:
- Command, Resultable::Result
- Defined in:
- lib/aye_commander/inspectable.rb
Overview
This module handles methods that help a command instance represent its contents in different ways.
Instance Method Summary collapse
-
#inspect ⇒ Object
This inspect mimics ActiveModel for a better inspection.
-
#pretty_print(pp) ⇒ Object
This method mimics ActiveModel pretty_print for a better console output.
-
#sorted_instance_variables ⇒ Object
Sorts the instance variables in alphabetical order, but keeps @status at the beginning for easier inspection.
-
#to_hash(limit = instance_variables) ⇒ Object
Returns a hash of the specified instance_variables Defaults to returning all the currently existing instance variables.
-
#to_result_hash ⇒ Object
Returns a hash of only the instance variables that were specified by the .returns method.
Instance Method Details
#inspect ⇒ Object
This inspect mimics ActiveModel for a better inspection.
6 7 8 9 10 11 |
# File 'lib/aye_commander/inspectable.rb', line 6 def inspect inspection = to_hash.map do |name, value| "#{name}: #{value}" end.compact.join(', ') "#<#{self.class} #{inspection}>" end |
#pretty_print(pp) ⇒ Object
This method mimics ActiveModel pretty_print for a better console output.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/aye_commander/inspectable.rb', line 14 def pretty_print(pp) pp.object_address_group(self) do ivs = sorted_instance_variables.map(&:to_s) pp.seplist(ivs, proc { pp.text ',' }) do |iv| pp.breakable ' ' pp.group(1) do pp.text iv pp.text ':' pp.breakable pp.pp instance_variable_get(iv) end end end end |
#sorted_instance_variables ⇒ Object
Sorts the instance variables in alphabetical order, but keeps @status at the beginning for easier inspection
53 54 55 |
# File 'lib/aye_commander/inspectable.rb', line 53 def sorted_instance_variables [:@status] | instance_variables.sort end |
#to_hash(limit = instance_variables) ⇒ Object
Returns a hash of the specified instance_variables Defaults to returning all the currently existing instance variables
31 32 33 34 35 36 |
# File 'lib/aye_commander/inspectable.rb', line 31 def to_hash(limit = instance_variables) limit.each_with_object({}) do |iv, hash| ivn = to_ivar(iv) hash[ivn] = instance_variable_get(ivn) end end |
#to_result_hash ⇒ Object
Returns a hash of only the instance variables that were specified by the .returns method.
If no variables were specified then it becomes functionally identical to #to_hash
43 44 45 46 47 48 49 |
# File 'lib/aye_commander/inspectable.rb', line 43 def to_result_hash if self.class.respond_to?(:returns) && self.class.returns.any? to_hash([:status] | self.class.returns) else to_hash end end |