lazy_global_record 1.0.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 80f068e29c8dba787bf3ee262e954db73b1f65d4
4
- data.tar.gz: ade0cf9586c796625a5e4b274f6e88b2f68fe1d2
3
+ metadata.gz: 3312908d16be8dafcdacccc5d18284e7c182250a
4
+ data.tar.gz: cb5a6fbd1da7cd51495457272fc34463a1f0da1e
5
5
  SHA512:
6
- metadata.gz: 98a7ae2b0ee15141ef5e40e63be863c953f56e47dd303ef71ebc8d430eb90ea1a924dcec479463f2c47b2f6b887e4ee4db00b98246af9fb692f0f75715e38a62
7
- data.tar.gz: 800d262e97fc32cb9c7cd91b7b2db0394deb1e997c734a3b0f246211c404bc814bd77726709a2439a88697a9cbd8ef698043d32eed15e157b61c812fa8c64afb
6
+ metadata.gz: 6ad2b93a79e1190343a7e774d14ae51a09fb98a2cb3f0a37f0cbf4517b5661e2441b0f149c8e2107c5444f1dd02a091046f66ddc4195cb99b82335818e8c2b15
7
+ data.tar.gz: 7e5f0fe1a9d2a5421a10874277a1d9b7d2a0d3eb8066ec6927f138fc727a5fbebbeddac0312e8e2e6a2b4d0c1a4024bd7eb6bd36ad8263c0100234ca00585f01
data/README.md CHANGED
@@ -83,6 +83,12 @@ another in a `before(:each)` in your `spec_helper.rb`. Put
83
83
  a `LazyGlobalRecord.reset_all` after each and any `DatabaseCleaner.clean`
84
84
  or `clean_with` calls, to reset cached values when the db is cleaned out.
85
85
 
86
+ You can also use `LazyGlobalRecord.reload_all`, which rests and then
87
+ forces immediate re-fetch. Defeats the purpose of lazy loading to make
88
+ tests faster by only loading what's needed for the test -- but I was
89
+ getting odd segfaults in some cases that this is a potential workaround
90
+ for.
91
+
86
92
  ### Custom transformations
87
93
 
88
94
  What if you need more than just the `id`? You can supply a custom
@@ -10,6 +10,9 @@ class LazyGlobalRecord
10
10
  def reset_all
11
11
  @all_instances.each { |instance| instance.reset if instance.resettable? }
12
12
  end
13
+ def reload_all
14
+ @all_instances.each { |instance| instance.reload if instance.resettable? }
15
+ end
13
16
  end
14
17
 
15
18
  def initialize( relation:,
@@ -57,6 +60,11 @@ class LazyGlobalRecord
57
60
  @slot.set( create_delay )
58
61
  end
59
62
 
63
+ def reload
64
+ reset
65
+ value
66
+ end
67
+
60
68
  protected
61
69
 
62
70
  def create_delay
@@ -1,3 +1,3 @@
1
1
  class LazyGlobalRecord
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -1650,3 +1650,121 @@ LazyGlobalRecordTest: test_custom_filter
1650
1650
  Value Load (0.1ms) SELECT "values".* FROM "values" WHERE "values"."value" = ? ORDER BY "values"."id" ASC LIMIT 1 [["value", "seven"]]
1651
1651
   (0.0ms) RELEASE SAVEPOINT active_record_1
1652
1652
   (0.4ms) rollback transaction
1653
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
1654
+  (0.1ms) begin transaction
1655
+ ----------------------------------------
1656
+ LazyGlobalRecordTest: test_custom_filter
1657
+ ----------------------------------------
1658
+  (0.0ms) SAVEPOINT active_record_1
1659
+ SQL (0.4ms) INSERT INTO "values" ("value", "other_value", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["value", "seven"], ["other_value", "treasure"], ["created_at", "2016-03-02 18:08:03.297582"], ["updated_at", "2016-03-02 18:08:03.297582"]]
1660
+  (0.0ms) RELEASE SAVEPOINT active_record_1
1661
+  (0.0ms) SAVEPOINT active_record_1
1662
+ Value Load (0.1ms) SELECT "values".* FROM "values" WHERE "values"."value" = ? ORDER BY "values"."id" ASC LIMIT 1 [["value", "seven"]]
1663
+  (0.0ms) RELEASE SAVEPOINT active_record_1
1664
+  (1.2ms) rollback transaction
1665
+  (0.1ms) begin transaction
1666
+ -------------------------------------
1667
+ LazyGlobalRecordTest: test_it_reloads
1668
+ -------------------------------------
1669
+  (0.0ms) SAVEPOINT active_record_1
1670
+ SQL (0.2ms) INSERT INTO "values" ("value", "other_value", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["value", "five"], ["other_value", "first"], ["created_at", "2016-03-02 18:08:03.305760"], ["updated_at", "2016-03-02 18:08:03.305760"]]
1671
+  (0.0ms) RELEASE SAVEPOINT active_record_1
1672
+  (0.0ms) SAVEPOINT active_record_1
1673
+ Value Load (0.0ms) SELECT "values".* FROM "values" WHERE "values"."value" = ? ORDER BY "values"."id" ASC LIMIT 1 [["value", "five"]]
1674
+  (0.0ms) RELEASE SAVEPOINT active_record_1
1675
+ SQL (0.0ms) DELETE FROM "values"
1676
+  (0.0ms) SAVEPOINT active_record_1
1677
+ SQL (0.4ms) INSERT INTO "values" ("value", "other_value", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["value", "five"], ["other_value", "second"], ["created_at", "2016-03-02 18:08:03.307879"], ["updated_at", "2016-03-02 18:08:03.307879"]]
1678
+  (0.0ms) RELEASE SAVEPOINT active_record_1
1679
+  (0.0ms) SAVEPOINT active_record_1
1680
+ Value Load (0.0ms) SELECT "values".* FROM "values" WHERE "values"."value" = ? ORDER BY "values"."id" ASC LIMIT 1 [["value", "five"]]
1681
+  (0.0ms) RELEASE SAVEPOINT active_record_1
1682
+ Value Load (0.1ms) SELECT "values".* FROM "values" WHERE "values"."id" = ? LIMIT 1 [["id", 2]]
1683
+  (0.7ms) rollback transaction
1684
+  (0.1ms) begin transaction
1685
+ ---------------------------------------------------
1686
+ LazyGlobalRecordTest: test_it_returns_id_by_default
1687
+ ---------------------------------------------------
1688
+  (0.0ms) SAVEPOINT active_record_1
1689
+ SQL (0.3ms) INSERT INTO "values" ("value", "created_at", "updated_at") VALUES (?, ?, ?) [["value", "one"], ["created_at", "2016-03-02 18:08:03.312365"], ["updated_at", "2016-03-02 18:08:03.312365"]]
1690
+  (0.0ms) RELEASE SAVEPOINT active_record_1
1691
+  (0.0ms) SAVEPOINT active_record_1
1692
+ Value Load (0.0ms) SELECT "values".* FROM "values" WHERE "values"."value" = ? ORDER BY "values"."id" ASC LIMIT 1 [["value", "one"]]
1693
+  (0.0ms) RELEASE SAVEPOINT active_record_1
1694
+ Value Load (0.0ms) SELECT "values".* FROM "values" WHERE "values"."value" = ? ORDER BY "values"."id" ASC LIMIT 1 [["value", "one"]]
1695
+  (0.3ms) rollback transaction
1696
+  (0.0ms) begin transaction
1697
+ -----------------------------------------------------------------------
1698
+ LazyGlobalRecordTest: test_raises_an_exception_if_exceptions_are_raised
1699
+ -----------------------------------------------------------------------
1700
+  (0.0ms) SAVEPOINT active_record_1
1701
+  (0.0ms) ROLLBACK TO SAVEPOINT active_record_1
1702
+  (0.0ms) SAVEPOINT active_record_1
1703
+ Value Load (0.1ms) SELECT "values".* FROM "values" WHERE "values"."value" = ? ORDER BY "values"."id" ASC LIMIT 1 [["value", "nonesuch"]]
1704
+  (0.0ms) ROLLBACK TO SAVEPOINT active_record_1
1705
+  (0.0ms) rollback transaction
1706
+  (0.0ms) begin transaction
1707
+ --------------------------------------------------------------------
1708
+ LazyGlobalRecordTest: test_it_creates_a_record_when_creation_allowed
1709
+ --------------------------------------------------------------------
1710
+  (0.0ms) SAVEPOINT active_record_1
1711
+ Value Load (0.1ms) SELECT "values".* FROM "values" WHERE "values"."value" = ? ORDER BY "values"."id" ASC LIMIT 1 [["value", "three"]]
1712
+ SQL (0.2ms) INSERT INTO "values" ("value", "created_at", "updated_at") VALUES (?, ?, ?) [["value", "three"], ["created_at", "2016-03-02 18:08:03.317191"], ["updated_at", "2016-03-02 18:08:03.317191"]]
1713
+  (0.0ms) RELEASE SAVEPOINT active_record_1
1714
+ Value Load (0.0ms) SELECT "values".* FROM "values" WHERE "values"."id" = ? LIMIT 1 [["id", 1]]
1715
+  (0.4ms) rollback transaction
1716
+  (0.0ms) begin transaction
1717
+ -----------------------------------------------------------
1718
+ LazyGlobalRecordTest: test_it_does_allow_reset_when_allowed
1719
+ -----------------------------------------------------------
1720
+  (0.1ms) SAVEPOINT active_record_1
1721
+ SQL (0.3ms) INSERT INTO "values" ("value", "other_value", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["value", "five"], ["other_value", "first"], ["created_at", "2016-03-02 18:08:03.319221"], ["updated_at", "2016-03-02 18:08:03.319221"]]
1722
+  (0.0ms) RELEASE SAVEPOINT active_record_1
1723
+  (0.0ms) SAVEPOINT active_record_1
1724
+ Value Load (0.0ms) SELECT "values".* FROM "values" WHERE "values"."value" = ? ORDER BY "values"."id" ASC LIMIT 1 [["value", "five"]]
1725
+  (0.0ms) RELEASE SAVEPOINT active_record_1
1726
+ SQL (0.0ms) DELETE FROM "values"
1727
+  (0.0ms) SAVEPOINT active_record_1
1728
+ SQL (0.3ms) INSERT INTO "values" ("value", "other_value", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["value", "five"], ["other_value", "second"], ["created_at", "2016-03-02 18:08:03.321187"], ["updated_at", "2016-03-02 18:08:03.321187"]]
1729
+  (0.1ms) RELEASE SAVEPOINT active_record_1
1730
+  (0.1ms) SAVEPOINT active_record_1
1731
+ Value Load (0.1ms) SELECT "values".* FROM "values" WHERE "values"."value" = ? ORDER BY "values"."id" ASC LIMIT 1 [["value", "five"]]
1732
+  (0.0ms) RELEASE SAVEPOINT active_record_1
1733
+ Value Load (0.0ms) SELECT "values".* FROM "values" WHERE "values"."id" = ? LIMIT 1 [["id", 2]]
1734
+  (0.6ms) rollback transaction
1735
+  (0.0ms) begin transaction
1736
+ ---------------------------------------------------------------------------
1737
+ LazyGlobalRecordTest: test_it_raises_on_no_record_when_creation_not_allowed
1738
+ ---------------------------------------------------------------------------
1739
+  (0.0ms) SAVEPOINT active_record_1
1740
+ Value Load (0.1ms) SELECT "values".* FROM "values" WHERE "values"."value" = ? ORDER BY "values"."id" ASC LIMIT 1 [["value", "two"]]
1741
+  (0.0ms) ROLLBACK TO SAVEPOINT active_record_1
1742
+  (0.0ms) rollback transaction
1743
+  (0.0ms) begin transaction
1744
+ -------------------------------------------------------------------
1745
+ LazyGlobalRecordTest: test_it_does_not_allow_reset_when_not_allowed
1746
+ -------------------------------------------------------------------
1747
+  (0.0ms) SAVEPOINT active_record_1
1748
+ Value Load (0.1ms) SELECT "values".* FROM "values" WHERE "values"."value" = ? ORDER BY "values"."id" ASC LIMIT 1 [["value", "four"]]
1749
+ SQL (0.3ms) INSERT INTO "values" ("value", "created_at", "updated_at") VALUES (?, ?, ?) [["value", "four"], ["created_at", "2016-03-02 18:08:03.326868"], ["updated_at", "2016-03-02 18:08:03.326868"]]
1750
+  (0.0ms) RELEASE SAVEPOINT active_record_1
1751
+  (0.5ms) rollback transaction
1752
+  (0.2ms) begin transaction
1753
+ -------------------------------------------------------
1754
+ LazyGlobalRecordTest: test_it_uses_custom_creation_proc
1755
+ -------------------------------------------------------
1756
+  (0.1ms) SAVEPOINT active_record_1
1757
+ Value Load (0.1ms) SELECT "values".* FROM "values" WHERE "values"."value" = ? ORDER BY "values"."id" ASC LIMIT 1 [["value", "six"]]
1758
+ SQL (0.3ms) INSERT INTO "values" ("value", "other_value", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["value", "six"], ["other_value", "manual"], ["created_at", "2016-03-02 18:08:03.330629"], ["updated_at", "2016-03-02 18:08:03.330629"]]
1759
+  (0.1ms) RELEASE SAVEPOINT active_record_1
1760
+ Value Load (0.1ms) SELECT "values".* FROM "values" WHERE "values"."id" = ? LIMIT 1 [["id", 1]]
1761
+  (0.5ms) rollback transaction
1762
+  (0.1ms) begin transaction
1763
+ ---------------------------------------------------------
1764
+ LazyGlobalRecordTest: test_raises_on_exceptions_in_filter
1765
+ ---------------------------------------------------------
1766
+  (0.1ms) SAVEPOINT active_record_1
1767
+ Value Load (0.2ms) SELECT "values".* FROM "values" WHERE "values"."value" = ? ORDER BY "values"."id" ASC LIMIT 1 [["value", "one"]]
1768
+ SQL (0.3ms) INSERT INTO "values" ("value", "created_at", "updated_at") VALUES (?, ?, ?) [["value", "one"], ["created_at", "2016-03-02 18:08:03.336266"], ["updated_at", "2016-03-02 18:08:03.336266"]]
1769
+  (0.1ms) ROLLBACK TO SAVEPOINT active_record_1
1770
+  (0.3ms) rollback transaction
@@ -62,6 +62,24 @@ class LazyGlobalRecordTest < ActiveSupport::TestCase
62
62
  assert_equal "second", Value.find(lazy.value).other_value
63
63
  end
64
64
 
65
+ test "it reloads" do
66
+ Value.create(value: "five", other_value: "first")
67
+
68
+ lazy = LazyGlobalRecord.new(
69
+ relation: -> { Value.where(value: "five") },
70
+ resettable: true
71
+ )
72
+
73
+ lazy.value
74
+
75
+ Value.delete_all
76
+ Value.create(value: "five", other_value: "second")
77
+
78
+ lazy.reload
79
+
80
+ assert_equal "second", Value.find(lazy.value).other_value
81
+ end
82
+
65
83
  test "it uses custom creation proc" do
66
84
  lazy = LazyGlobalRecord.new(
67
85
  relation: -> { Value.where(value: "six") },
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy_global_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Friends of the Web