pretentious 0.0.4 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0fb899b0d269ad993d7e7d5f9b7fe61cfd6b226c
4
- data.tar.gz: 6c3d3400c4eb0ce26f1e7dad7937673b9ee90275
3
+ metadata.gz: 87f10781f0769264b1c39fa2c34463332546ba6a
4
+ data.tar.gz: ffac481a78ad14c7f5bd62533f135b17aab304d9
5
5
  SHA512:
6
- metadata.gz: 5033ed7b8019458b6068043963580818e313f56b083194b22092c2f7754a9754bd5d0a1bfd89aa3c10e92025dac58b42855a62e48a62be76bd9270d26d732411
7
- data.tar.gz: d1e12ecc2d3b839919bc1e8a0294b9d53a76e94f1ab4c7916e12944a9621bfe5634b993696efcd255189380d09b5f986a5b9c49ed96b3a4f93a4e1efa5b1097e
6
+ metadata.gz: 54a6a2fdc2985b0920b61535f58dbfb4288315cc7c54f7d808dd68a587824a12b5f12aa0400c5fff508a23f537d259efc9e47b835e422856ac0c93f46b377ac4
7
+ data.tar.gz: 0fc245c408de5c2f667e2c7d17d859f01aec59d54d12fa173e88a7d57b049fbdd7792843838e5a5ae65ea026d031aed92c579dbdb11209e76a4fbfa5b55224fd
data/README.md CHANGED
@@ -9,7 +9,8 @@ you've written in a straightfoward manner!
9
9
 
10
10
  On a serious note, this gem allows you to generate tests template much better than those generated by default
11
11
  for various frameworks. It is also useful for "recording" current behavior of existing components in order
12
- to prepare for refactoring.
12
+ to prepare for refactoring. As a bonus it also exposes an Object Deconstructor which allows you, given
13
+ any object, to obtain a ruby code on how it was created.
13
14
 
14
15
  ## Installation
15
16
 
@@ -306,6 +307,124 @@ should generate the following in rspec
306
307
  # TestClass1#something_is_wrong when passed should return StandardError
307
308
  expect { @fixture.something_is_wrong }.to raise_error
308
309
  ```
310
+ ## Object Deconstruction Utility
311
+
312
+ As Pretentious as the gem is, there are other uses other than generating tests specs. Tools are also available to
313
+ deconstruct objects. Object deconstruction basically means that the components used to create and initialized are
314
+ extracted from an object until only primitive types remain. The pretentious gem will also generate the
315
+ necessary ruby code to create one form scracth. Below is an example:
316
+
317
+ Given an instance of an activerecord base connection for example
318
+
319
+ ```ruby
320
+ ActiveRecord::Base.connection
321
+ ```
322
+
323
+ running deconstruct would generate:
324
+
325
+ ```ruby
326
+ var_70301267513280 = #<File:0x007fe094279f80>
327
+ logger = ActiveSupport::Logger.new(var_70301267513280)
328
+ connection_options = ["localhost", "root", "password", "jclviste_test", nil, nil, 131074]
329
+ config = {adapter: "mysql", encoding: "utf8", reconnect: false, database: "jclviste_test", pool: 5, username: "root", password: "password", host: "localhost"}
330
+ var_70301281665660 = ActiveRecord::ConnectionAdapters::MysqlAdapter.new(connection, logger, connection_options, config)
331
+ ```
332
+ Note that
333
+
334
+ ```ruby
335
+ var_70301267513280 = #<File:0x007fe094279f80>
336
+ ```
337
+
338
+ because the pretentious gem was not able to capture its init arguments.
339
+
340
+ ### How to use
341
+
342
+ Simply call:
343
+
344
+ Pretentious.install_watcher
345
+
346
+ before all your objects are initalized. This will add the following methods to all objects:
347
+
348
+ ```ruby
349
+ object._deconstruct
350
+ object._deconstruct_to_ruby
351
+ ```
352
+ The _deconstruct method generates a raw deconstruction data structure using by the _deconstruct_to_ruby method.
353
+ Of course _deconstruct_to_ruby generates the ruby code necessary to create the object!
354
+
355
+ ### Using the Object deconstruct in rails
356
+
357
+ In your Gemfile, add the pretentious gem.
358
+
359
+ ```ruby
360
+ group :test do
361
+ gem 'pretentious'
362
+ end
363
+ ```
364
+
365
+ The do a bundle
366
+
367
+ $ bundle
368
+
369
+
370
+ Note: It is advisable to add it only in the test or development group!
371
+ The way it logs objects would probably prevent anything from being GC'ed.
372
+
373
+ For rails, including inside config.ru is sufficient to capture most objects:
374
+
375
+ ```ruby
376
+ #config.ru
377
+ require File.expand_path('../boot', __FILE__)
378
+
379
+ require 'rails/all'
380
+ # Require the gems listed in Gemfile, including any gems
381
+ # you've limited to :test, :development, or :production.
382
+ Bundler.require(*Rails.groups)
383
+
384
+ if Rails.env.test?
385
+ puts "watching new instances"
386
+ Pretentious::Generator.watch_new_instances
387
+ end
388
+
389
+ module App
390
+ class Application < Rails::Application
391
+ # ..... stuf ......
392
+
393
+ end
394
+ end
395
+ ```
396
+
397
+ boot up the rails console and blow your mind:
398
+
399
+ ```
400
+ $ rails c -e test
401
+ watching new instances
402
+ Loading test environment (Rails 4.2.4)
403
+ irb: warn: can't alias context from irb_context.
404
+ 2.2.0 :001 > puts ActiveRecord::Base.connection._deconstruct_to_ruby
405
+ connection = #<Mysql:0x007fe095d785c0>
406
+ var_70301267513280 = #<File:0x007fe094279f80>
407
+ logger = ActiveSupport::Logger.new(var_70301267513280)
408
+ connection_options = ["localhost", "root", "password", "app_test", nil, nil, 131074]
409
+ config = {adapter: "mysql", encoding: "utf8", reconnect: false, database: "app_test", pool: 5, username: "root", password: "password", host: "localhost"}
410
+ var_70301281665660 = ActiveRecord::ConnectionAdapters::MysqlAdapter.new(connection, logger, connection_options, config)
411
+ => nil
412
+ 2.2.0 :004 > w = User.where(id: 1)
413
+ User Load (2.8ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = ? [["id", 1]]
414
+ => #<ActiveRecord::Relation []>
415
+ 2.2.0 :005 > w._deconstruct_to_ruby
416
+ => "klass = User\nvar_70301317929300 = \"users\"\ntable = Arel::Table.new(var_70301317929300, klass)\nvar_70301339518120 = User::ActiveRecord_Relation.new(klass, table)\n"
417
+ 2.2.0 :006 > puts w._deconstruct_to_ruby
418
+ klass = User
419
+ var_70301317929300 = "users"
420
+ table = Arel::Table.new(var_70301317929300, klass)
421
+ var_70301339518120 = User::ActiveRecord_Relation.new(klass, table)
422
+ => nil
423
+ 2.2.0 :007 >
424
+ ```
425
+
426
+ Note: There are some objects it will fails to capture and as such those would not deconstruct properly.
427
+
309
428
 
310
429
  ## Things to do after
311
430
 
data/lib/pretentious.rb CHANGED
@@ -18,6 +18,14 @@ module Pretentious
18
18
  @results
19
19
  end
20
20
 
21
+ def self.install_watcher
22
+ Pretentious::Generator.watch_new_instances
23
+ end
24
+
25
+ def self.uninstall_watcher
26
+ Pretentious::Generator.unwatch_new_instances
27
+ end
28
+
21
29
  def self.value_ize(value, let_variables, declared_names)
22
30
  if (value.kind_of? String)
23
31
  "#{value.dump}"
@@ -397,9 +405,9 @@ module Pretentious
397
405
  p = params[index]
398
406
  if p.size > 1
399
407
  @_variable_names[arg.object_id] = p[1].to_s
400
- end
408
+ end unless p.nil?
401
409
  index+=1
402
- end
410
+ end unless args.nil?
403
411
 
404
412
  end
405
413
 
@@ -412,7 +420,7 @@ module Pretentious
412
420
  end
413
421
 
414
422
  def _deconstruct_to_ruby(indentation = 0)
415
- Pretentious::Deconstructor.new().deconstruct_to_ruby(indentation, _variable_map, self)
423
+ Pretentious::Deconstructor.new().deconstruct_to_ruby(indentation, _variable_map, {}, self)
416
424
  end
417
425
 
418
426
  end
@@ -154,7 +154,6 @@ class Pretentious::Deconstructor
154
154
  target_objects.each { |target_object|
155
155
  variable_map.merge!(target_object._variable_map) if target_object.methods.include?(:_variable_map) && !target_object._variable_map.nil?
156
156
  }
157
-
158
157
  declarations, dependencies = deconstruct *target_objects
159
158
  declarations[:declaration].each do |d|
160
159
 
@@ -1,3 +1,3 @@
1
1
  module Pretentious
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.6"
3
3
  end
data/run_test.sh CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  git add .
4
4
  gem build pretentious.gemspec
5
- gem install pretentious-0.0.3.gem
5
+ gem install pretentious-0.0.6.gem
6
6
  ruby test/test_generator.rb
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pretentious
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Emmanuel Dayo