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 the one ActiveModel uses so hopefully it will also look pretty during a pry session when the variables become too many.
-
#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 the one ActiveModel uses so hopefully it will also look pretty during a pry session when the variables become too many.
7 8 9 10 11 12 |
# File 'lib/aye_commander/inspectable.rb', line 7 def inspect inspection = to_hash.map do |name, value| "#{name}: #{value}" end.compact.join(', ') "#<#{self.class} #{inspection}>" end |
#to_hash(limit = instance_variables) ⇒ Object
Returns a hash of the specified instance_variables Defaults to returning all the currently existing instance variables
16 17 18 19 20 21 |
# File 'lib/aye_commander/inspectable.rb', line 16 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
28 29 30 31 32 33 34 |
# File 'lib/aye_commander/inspectable.rb', line 28 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 |