aye_commander 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/aye_commander/inspectable.rb +23 -2
- data/lib/aye_commander/ivar.rb +15 -4
- data/lib/aye_commander/version.rb +1 -1
- data/spec/aye_commander/inspectable_spec.rb +14 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 41be7b9a74745fe7d3a69b24b334b52900022feb
|
4
|
+
data.tar.gz: 65b0b4de86fa2fb3d75fbc19002e7f8914c0ed5c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2d9e76e953e642d10fb0e0dba1e043d67c5d6d0df87f1c4291fed06bcc7a03d78eb8806580e363dbcffd45213b27487b2e37d009887d14b2e5409f644bb13c7
|
7
|
+
data.tar.gz: 32460bc54a9b5abfb10038c6315cfac27551dc48ed1548b65c25f4e6e518dc4e838f52d12b11be82011b81bd4d44183591015fdbad2c5150b5855c07c3921692
|
@@ -2,8 +2,7 @@ module AyeCommander
|
|
2
2
|
# This module handles methods that help a command instance represent its
|
3
3
|
# contents in different ways.
|
4
4
|
module Inspectable
|
5
|
-
# This inspect mimics
|
6
|
-
# look pretty during a pry session when the variables become too many.
|
5
|
+
# This inspect mimics ActiveModel for a better inspection.
|
7
6
|
def inspect
|
8
7
|
inspection = to_hash.map do |name, value|
|
9
8
|
"#{name}: #{value}"
|
@@ -11,6 +10,22 @@ module AyeCommander
|
|
11
10
|
"#<#{self.class} #{inspection}>"
|
12
11
|
end
|
13
12
|
|
13
|
+
# This method mimics ActiveModel pretty_print for a better console output.
|
14
|
+
def pretty_print(pp)
|
15
|
+
pp.object_address_group(self) do
|
16
|
+
ivs = sorted_instance_variables.map(&:to_s)
|
17
|
+
pp.seplist(ivs, proc { pp.text ',' }) do |iv|
|
18
|
+
pp.breakable ' '
|
19
|
+
pp.group(1) do
|
20
|
+
pp.text iv
|
21
|
+
pp.text ':'
|
22
|
+
pp.breakable
|
23
|
+
pp.pp instance_variable_get(iv)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
14
29
|
# Returns a hash of the specified instance_variables
|
15
30
|
# Defaults to returning all the currently existing instance variables
|
16
31
|
def to_hash(limit = instance_variables)
|
@@ -32,5 +47,11 @@ module AyeCommander
|
|
32
47
|
to_hash
|
33
48
|
end
|
34
49
|
end
|
50
|
+
|
51
|
+
# Sorts the instance variables in alphabetical order, but keeps @status at
|
52
|
+
# the beginning for easier inspection
|
53
|
+
def sorted_instance_variables
|
54
|
+
[:@status] | instance_variables.sort
|
55
|
+
end
|
35
56
|
end
|
36
57
|
end
|
data/lib/aye_commander/ivar.rb
CHANGED
@@ -2,6 +2,9 @@ module AyeCommander
|
|
2
2
|
# This module contains mostly methods related to method missing and instance
|
3
3
|
# variables
|
4
4
|
module Ivar
|
5
|
+
AT = '@'.freeze
|
6
|
+
EQ = '='.freeze
|
7
|
+
|
5
8
|
# Instance variable related class methods
|
6
9
|
module ClassMethods
|
7
10
|
# Adds the received reader to the class.
|
@@ -14,13 +17,21 @@ module AyeCommander
|
|
14
17
|
# Transforms the received name to instance variable form
|
15
18
|
# Eg: command -> @command
|
16
19
|
def to_ivar(name)
|
17
|
-
name[0] ==
|
20
|
+
name[0] == at ? name.to_sym : "@#{name}".to_sym
|
18
21
|
end
|
19
22
|
|
20
23
|
# Transforms the received name to normal variable form
|
21
24
|
# Eg: @command -> command
|
22
25
|
def to_nvar(name)
|
23
|
-
name[0] ==
|
26
|
+
name[0] == at ? name[1..-1].to_sym : name.to_sym
|
27
|
+
end
|
28
|
+
|
29
|
+
def at
|
30
|
+
::AyeCommander::Ivar::AT
|
31
|
+
end
|
32
|
+
|
33
|
+
def eq
|
34
|
+
::AyeCommander::Ivar::EQ
|
24
35
|
end
|
25
36
|
end
|
26
37
|
|
@@ -73,7 +84,7 @@ module AyeCommander
|
|
73
84
|
# Any method that ends with an equal sign will be able to be handled by
|
74
85
|
# this method missing.
|
75
86
|
def method_missing(name, *args)
|
76
|
-
if name[-1] ==
|
87
|
+
if name[-1] == self.class.eq
|
77
88
|
var_name = to_ivar(name[0...-1])
|
78
89
|
instance_variable_set var_name, args.first
|
79
90
|
self.class.uses name[0...-1]
|
@@ -87,7 +98,7 @@ module AyeCommander
|
|
87
98
|
private
|
88
99
|
|
89
100
|
def respond_to_missing?(name, *args)
|
90
|
-
name[-1] ==
|
101
|
+
name[-1] == self.class.eq || super
|
91
102
|
end
|
92
103
|
end
|
93
104
|
end
|
@@ -12,6 +12,13 @@ describe AyeCommander::Inspectable do
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
+
context '#pretty_print' do
|
16
|
+
it 'gives a pretty print representation of the innards of the class' do
|
17
|
+
pretty_print = PP.pp(instance, ''.dup)
|
18
|
+
expect(pretty_print).to match(/#<#<Class:\dx\w+>:\dx\w+\n @status: :success,\n @other: :potato,\n @variable: :something>/)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
15
22
|
context '#to_hash' do
|
16
23
|
it 'gives a hash representation of the innards of the class' do
|
17
24
|
result = { :@status => :success, :@variable => :something, :@other => :potato }
|
@@ -42,4 +49,11 @@ describe AyeCommander::Inspectable do
|
|
42
49
|
expect(instance.to_result_hash).to eq result
|
43
50
|
end
|
44
51
|
end
|
52
|
+
|
53
|
+
context '#sorted_instance_variables' do
|
54
|
+
it 'sorts the instance variables alphabetically but keeps @status at the beginning' do
|
55
|
+
sorted = %i[@status @other @variable]
|
56
|
+
expect(instance.sorted_instance_variables).to eq sorted
|
57
|
+
end
|
58
|
+
end
|
45
59
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aye_commander
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pyzlnar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A gem that helps to write commands in ruby.
|
14
14
|
email: pyzlnar@gmail.com
|
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
68
|
version: '0'
|
69
69
|
requirements: []
|
70
70
|
rubyforge_project:
|
71
|
-
rubygems_version: 2.
|
71
|
+
rubygems_version: 2.6.13
|
72
72
|
signing_key:
|
73
73
|
specification_version: 4
|
74
74
|
summary: A simple command pattern gem
|