active_record_lite 0.1.0 → 0.2.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
  SHA256:
3
- metadata.gz: 7b2e70191758bf10f9226b2498e7bf8b6fa751493e66a82d47913f7e63a39016
4
- data.tar.gz: c11cff4b26e8adad2482837e1015c5b6bf32a8cd7e3022668cb3db942266b1a2
3
+ metadata.gz: 384c59db0a31d49da5defdb12a12b39de14add88018d619711a73509dad4b6ff
4
+ data.tar.gz: 8f09691dd1a6116306c1e07c028435a0eee0a83af42eb3b55f18391198c66c66
5
5
  SHA512:
6
- metadata.gz: f02bba0706f6250bcc55e6cac39242e9522ab114e6909a76899760f303be7c61b6df4d4e96b55f445649df800c17f1f3ff995f7b1101fbcc21606509a5518bcf
7
- data.tar.gz: e1d7e8add388fc91d894b652d4b695ca0bac3a64296d58348d090279cdb960e28d8330a8e8a4c2b1611e10a2f5650563a764b913c2a1500c14b27416aadc9889
6
+ metadata.gz: ba9563d613333c24567fe7ec6247ea2dffc0b6071c5843470e6f19009edea0ee3f111cf783f2afbcc00262a706275c689241405bec73ef4c16d9b267b2c59967
7
+ data.tar.gz: 8b0d582125de0ec0f84e7f340a591032e0a418506daa0cb342ed5375369623e11b2eded62c525b0ade6f7389196e2dd735c1f76b1679506154b1ef06bbe4961d
@@ -0,0 +1,58 @@
1
+ # Ruby CircleCI 2.0 configuration file
2
+ #
3
+ # Check https://circleci.com/docs/2.0/language-ruby/ for more details
4
+ #
5
+ version: 2
6
+ jobs:
7
+ build:
8
+ docker:
9
+ # specify the version you desire here
10
+ - image: circleci/ruby:2.4.1-node-browsers
11
+
12
+ # Specify service dependencies here if necessary
13
+ # CircleCI maintains a library of pre-built images
14
+ # documented at https://circleci.com/docs/2.0/circleci-images/
15
+ # - image: circleci/postgres:9.4
16
+
17
+ working_directory: ~/repo
18
+
19
+ steps:
20
+ - checkout
21
+
22
+ # Download and cache dependencies
23
+ - restore_cache:
24
+ keys:
25
+ - v1-dependencies-{{ checksum "Gemfile.lock" }}
26
+ # fallback to using the latest cache if no exact match is found
27
+ - v1-dependencies-
28
+
29
+ - run:
30
+ name: install dependencies
31
+ command: |
32
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
33
+
34
+ - save_cache:
35
+ paths:
36
+ - ./vendor/bundle
37
+ key: v1-dependencies-{{ checksum "Gemfile.lock" }}
38
+
39
+ # run tests!
40
+ - run:
41
+ name: run tests
42
+ command: |
43
+ mkdir /tmp/test-results
44
+ TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
45
+ circleci tests split --split-by=timings)"
46
+
47
+ bundle exec rspec \
48
+ --format progress \
49
+ --out /tmp/test-results/rspec.xml \
50
+ --format progress \
51
+ $TEST_FILES
52
+
53
+ # collect reports
54
+ - store_test_results:
55
+ path: /tmp/test-results
56
+ - store_artifacts:
57
+ path: /tmp/test-results
58
+ destination: test-results
data/.gitignore CHANGED
@@ -6,4 +6,6 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
- Gemfile.lock
9
+ Gemfile.lock
10
+ .vscode
11
+ .byebug_history
data/README.md CHANGED
@@ -1,4 +1,7 @@
1
1
  # ActiveRecordLite
2
+ [![CircleCI](https://circleci.com/gh/ritikesh/active_record_lite.svg?style=svg)](https://circleci.com/gh/ritikesh/active_record_lite)
3
+ [![Gem Version](https://badge.fury.io/rb/active_record_lite.svg)](https://badge.fury.io/rb/active_record_lite)
4
+ ![Downloads](https://ruby-gem-downloads-badge.herokuapp.com/active_record_lite?type=total)
2
5
 
3
6
  Lightweight ActiveRecord extension for basic reads and caching.
4
7
 
@@ -51,34 +54,14 @@ By default the lite class will include all columns defined in the parent model c
51
54
  ```
52
55
 
53
56
  #### NOTE:
54
- 1. To keep the gem lightweight, `serialization` is the implementer's job. Eg.
55
- ```ruby
56
- class User < ActiveRecord::Base
57
- has_lite, columns: %w(id name preferences)
57
+ 1. active_record_lite internally uses active_record, hence model level default scopes will apply when querying for the record from db.
58
58
 
59
- serialize :preferences, Hash
60
- end
61
-
62
- # to serialize preferences in lite model
63
- class User::ActiveRecordLite < ActiveRecordLite::Base
64
- def preferences
65
- YAML.load(read_attribute('preferences'.freeze))
66
- end
67
- end
68
- ```
69
- 2. active_record_lite internally uses active_record, hence model level default scopes will apply when querying for the record from db.
59
+ 2. `to_lite` should be called after all `serialize` calls in the model. `to_lite` relies on `serialised_attributes` which will be correctly initialised only after all the `serialize` calls.
60
+
61
+ 3. Dynamic selects are not possible. `to_lite` will run a `unscope(:select)` on the current chained scope.
70
62
 
71
- 3. `Boolean` columns in rails are very smaller integers - literally, `TINYINT`. Internally, `ActiveRecord` magic typecasts them to behave like bools and adds the `column_name?` methods to the model. This is also left to the implementer.
72
- ```ruby
73
- class Company::ActiveRecordLite < ActiveRecordLite::Base
74
- def active
75
- read_attribute('active'.freeze) == 1
76
- end
77
- alias :active? :active
78
- end
79
- ```
80
63
  #### TODO:
81
- 1. Dynamic selects are not possible currently.
64
+ 1. Add support to eager load lite objects via AR relation.currently.
82
65
 
83
66
  ## Development
84
67
 
@@ -38,7 +38,6 @@ Gem::Specification.new do |spec|
38
38
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
39
39
  spec.require_paths = ["lib"]
40
40
 
41
- spec.add_development_dependency "bundler", "~> 1.16"
42
41
  spec.add_development_dependency "rake", "~> 10.0"
43
42
  spec.add_development_dependency "rspec"
44
43
 
@@ -24,13 +24,18 @@ module ActiveRecordLite
24
24
  attr_accessor :attributes
25
25
 
26
26
  def self.fetch_from_db(sql)
27
- base_class.connection.execute(sql)
27
+ base_class.connection.select_rows(sql)
28
28
  end
29
29
 
30
30
  def read_attribute(attr_name)
31
31
  attributes[self.class.attr_lookup(attr_name)]
32
32
  end
33
33
 
34
+ def memoize_results(key)
35
+ return instance_variable_get(key) if instance_variable_defined?(key)
36
+ instance_variable_set(key, yield)
37
+ end
38
+
34
39
  def self.attr_lookup(attr_name)
35
40
  @attr_lookup[attr_name]
36
41
  end
@@ -4,10 +4,9 @@ module ActiveRecordLite
4
4
  if options.key?(:columns)
5
5
  @_arlcolumns = options.delete(:columns)
6
6
  end
7
-
8
7
  class_eval <<-EOV
9
8
  def self.to_lite
10
- scoper = select(@_arlcolumns)
9
+ scoper = unscope(:select).select(@_arlcolumns)
11
10
  ActiveRecordLite.perform(scoper.to_sql)
12
11
  end
13
12
 
@@ -15,8 +14,29 @@ module ActiveRecordLite
15
14
  @base_class = parent
16
15
  @attr_lookup = {}
17
16
  columns.each do |column_name|
18
- define_method(column_name) do
19
- read_attribute(column_name)
17
+ type = parent.type_for_attribute(column_name).type
18
+ if type.eql?(:datetime)
19
+ define_method(column_name) do
20
+ memoize_results(:"@\#{column_name}") {
21
+ read_attribute(column_name).in_time_zone
22
+ }
23
+ end
24
+ elsif type.eql?(:boolean)
25
+ define_method(column_name) do
26
+ read_attribute(column_name) == 1
27
+ end
28
+ alias_method "\#{column_name}?", column_name
29
+ elsif type.eql?(:text) && parent.serialized_attributes.key?(column_name)
30
+ klass = parent.serialized_attributes[column_name]
31
+ define_method(column_name) do
32
+ memoize_results(:"@\#{column_name}") {
33
+ klass.load(read_attribute(column_name))
34
+ }
35
+ end
36
+ else
37
+ define_method(column_name) do
38
+ read_attribute(column_name)
39
+ end
20
40
  end
21
41
  end
22
42
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordLite
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_lite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ritikesh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-12 00:00:00.000000000 Z
11
+ date: 2019-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.16'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.16'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rake
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -74,6 +60,7 @@ executables: []
74
60
  extensions: []
75
61
  extra_rdoc_files: []
76
62
  files:
63
+ - ".circleci/config.yml"
77
64
  - ".gitignore"
78
65
  - ".travis.yml"
79
66
  - CODE_OF_CONDUCT.md