Class: Object

Inherits:
BasicObject
Defined in:
lib/aastdlib/object.rb

Instance Method Summary collapse

Instance Method Details

#declare_and_define_ivar(name:, value:, convenience: true) ⇒ Object

Declare and define an instance variable for a given object. Name is the instance variable name and value is the item assigned to that instance variable. Convenience determines whether getter/setter methods are defined as well.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/aastdlib/object.rb', line 7

def declare_and_define_ivar(name:, value:, convenience: true)

	if name =~ /^@/

		iv_handle = name
		name = name.to_s.gsub('@','')

	else

		iv_handle = ('@'+name)

	end

	iv_handle = iv_handle.to_sym if iv_handle.respond_to?(:to_sym)

    self.instance_variable_set(iv_handle, value)

    if convenience

    	# getter
    	self.define_singleton_method(name) do
    		self.instance_variable_get(iv_handle)
    	end

    	# setter
    	self.define_singleton_method(name+'=') do |value|
    		self.instance_variable_set(iv_handle, value)
    	end

    end

end

#define_ivar(name:, value:, convenience: true) ⇒ Object

Convenience method for declare_and_define_ivar()



41
42
43
44
45
# File 'lib/aastdlib/object.rb', line 41

def define_ivar(name:, value:, convenience: true)

	self.declare_and_define_ivar(name: name, value: value)

end

#instance_variable_tableObject

Using terminal-table, format all instance variables in a visually appealing text-based table.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/aastdlib/object.rb', line 49

def instance_variable_table()

	rows = []
	table = Terminal::Table.new()
	table.headings = ['Instance Variable', 'Object']
	table.title = "#{self.class}: Instance Vairables"

	self.instance_variables.each do |iv|

		rows << [iv.to_s,self.instance_variable_get(iv)]
		
	end

	table.rows = rows

	return table

end

#method_table(type = nil) ⇒ Object

Using terminal-table, print a visually appealing table detailing methods offered by the object. Valid arguments to type include:

  • private

  • public

  • protected



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/aastdlib/object.rb', line 113

def method_table(type=nil)

	mlists = {
		methods: [],
		private: [],
		protected: [],
		public: []
	}

	if type

		case type

		when :private

			self.private_methods.sort.each  {|m| mlists[:private] << m}

		when :protected

			self.protected_methods.sort.each {|m| mlists[:protected] << m}

		when :public

			self.public_methods.sort.each  {|m| mlists[:public] << m}

		end

	else

		self.methods.sort.each {|m| mlists[:methods] << m} unless type
		self.private_methods.sort.each  {|m| mlists[:private] << m}
		self.protected_methods.sort.each {|m| mlists[:protected] << m}
		self.public_methods.sort.each  {|m| mlists[:public] << m}

	end

	mlists.delete_if {|k,v| v == []}

	if mlists.count < 1

		if type

			puts "[+] No methods of #{type} type available!"

		else

			puts "[+] No methods available!"

		end

		return

	end

	lengths = []
	headings = ['#']

	mlists.each do |k,v|
		headings << k.to_s.gsub('_',' ').capitalize
		lengths << v.length
	end

	max_len = lengths.uniq.sort.last

	table = Terminal::Table.new()
	table.headings = headings
	table.title = "Methods: #{self.class}"

	rows = []

	max_len.times do |n|

		row = []

		row << n
		mlists.each { |k,v|	row << mlists[k][n] }

		rows << row

	end

	table.rows = rows

	return table

end

#pi(obj) ⇒ Object

Pretty inspect and print it to stdout.



100
101
102
103
104
105
106
# File 'lib/aastdlib/object.rb', line 100

def pi(obj)

	puts
	puts obj.pretty_inspect
	puts

end

#pretty_inspectObject

Inspect the object all pretty like.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/aastdlib/object.rb', line 69

def pretty_inspect()

	tab_count = 0

	out = self.inspect.gsub("@","\n\t@")
		.lines

	expanded = ""

	counter = out.length
	(0..counter-1).each do |n|

		line = out[n]

		expanded += ("\t"*tab_count) + line

		tab_count += 1 if line =~ /\=\#\</

		if line =~ /\>\,/ and tab_count != 0

			tab_count -= 1

		end

	end

	return expanded

end