skimming 0.0.1 → 0.0.2

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: d38d35c798f029c2f8331d2ff3f890ede872607567b65e2d4ae43dcbdc28d034
4
- data.tar.gz: 6447c4b4c3d103c5b3458852d87216444a8bc9c985b1851ae45727678c375e38
3
+ metadata.gz: 5c31cbb40931809f9bed74b5d90a9c30058734f50a8818d2694e16630a70c493
4
+ data.tar.gz: 272a1af1759927b87add863af0112d8c1ac92ca7353eb040892c58afbfefb4b3
5
5
  SHA512:
6
- metadata.gz: 715fdc5fa8d1cb0519ac9e7b6696e102f3f245ba57bf6d7d8e08e89025e0ec060691bdfdac941de84422df17cd643032e745a91c8ffec7dc83a329de212a6380
7
- data.tar.gz: 1e6e3d3a21855d2917790f56c50bd82579ec798f1392288febea4cf94bfbf704536e9ad2c0577f498bb0a24a88e3cb8afe6f9837f7510d67e5dd809dff1d2d41
6
+ metadata.gz: 28cea3d3f6ae71f06598ddb1e35bbd6aff2a559199460af4893b9484a63ed67a59ffc24a247629a0c99a83e676f24f7cd3eab58847a61ec74c022f09140517ea
7
+ data.tar.gz: 71cd69780026514df89d0193a2060196d06def6f0710f253c378f96ae4c2dcf154c2f133b33b669cc2e0165bb8ccd019c612fb2083b5a9ee4ecfe75a0c7ebff1
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Skimming
2
+ Skimming is a gem for rails application that allows to filter a collection based on certain database-configurable rules. Every model can have its own rules towards every other model.
3
+ For example you can apply the feature to your Role model, so that every Role has individual filter rules for an association. It's useful expecially when you have to respond to an api call with a collection of records and you need it to be filtered differently based on which roles the authenticated user has.
4
+
5
+ ## Installation
6
+ Inside your Gemfile put
7
+
8
+ gem 'skimming'
9
+
10
+ and then run
11
+
12
+ bundle install
13
+
14
+ In each model you want to have the feature, just put the following.
15
+
16
+ include Skimming::Model
17
+
18
+ Create `config/skimming.yml` file in your project and compile it as follows for each of the models you included the code above into (let's say for instance you included Skimming::Model into Role model).
19
+
20
+ associations:
21
+ roles:
22
+ class_name: 'Role'
23
+
24
+ Once this config file is created, you can launch the following command.
25
+
26
+ rails g skimming_migration
27
+
28
+ The command above will generate a migration file in your project. Now you can migrate.
29
+
30
+ rails db:migrate
31
+
32
+ ### Use skimming filters of another model
33
+
34
+ If you want, you can also allow a related model (for example User) to use Role's skimming rules. In that case the User model should have this instead.
35
+
36
+ include Skimming::ParasiteModel
37
+
38
+ And the config file should have one more configuration.
39
+
40
+ associations:
41
+ roles:
42
+ class_name: 'Role'
43
+ options:
44
+ user:
45
+ skim_through:
46
+ - 'roles'
47
+
48
+ ## Usage
49
+
50
+ Create one `Skimming::CollectionFilter` for each class your model needs to skim and assign a rule that has to be satisfied.
51
+
52
+ role.create_collection_filter(object_name: 'order', rule: '@order.complete?')
53
+ role.create_collection_filter(object_name: 'pet', rule: '@pet.nice?')
54
+ role.create_collection_filter(object_name: 'room', rule: '@room.clean?')
55
+
56
+ You could also assign the same `Skimming::CollectionFilter` to multiple roles since they have a HABTM relation.
57
+
58
+ If now you call `role.skim rooms_collection` only clean rooms will be returned.
59
+
60
+ Also user can do that if it has the role assigned. By default the parasite model skims through all its associated models that have included `Skimming::Model` and sums all results, rejecting the records that all of the skimming models have rejected (if any of the skimming models have the record present in the filter, it will be present in the parasite model skimming result). However in some cases you may want to make the parasite model to skim only through one or some of these models. To do so, just pass one of the models or an array of those.
61
+
62
+ user.skim rooms_collection, through: :roles
63
+ user.skim rooms_collection, through: [:roles, :whatever]
64
+
65
+ ### Rules
66
+
67
+ You can store in filters rules whatever conditions you like in plain ruby and the string will be evaulated. Inside the rules, object have to be called as instance variables. These instance variables indeed need to be present and can be declared in a few ways.
68
+
69
+ 1. The model name of the instance you called `skim` from, like the user, will be defined automatically based on model name, so if you call `user.skim rooms_collection` you will have `@user` variable defined.
70
+ 2. Based on the skimmed collection, instance variables are defined. In the case above, `@room` variable will be present for each of the rooms to be evaluated in. The collection name is calculated based on the class of the first object in the collection and will raise an error if is not the same for all collection elements. You can override this calculation specifying the `object_name` of the collection (the skimming will look for collection_filters with that `object_name`)
71
+ 3. You can pass as third argument an hash of objects like this `user.skim rooms_collection, time: Time.zone.now, whatever: @you_want` and you will have `@time` and `@whatever` variables defined for rule evaluation.
72
+
73
+ WARNING: since the strings get evaluated, i recommend not to allow anyone except project developers to create collection_filters, in order to prevent malicious code from being executed.
@@ -17,7 +17,7 @@ module Skimming
17
17
  return collection if filters_rules.empty?
18
18
 
19
19
  skimming_result = collection.select do |collection_object|
20
- instance_variable_set("@#{collection_object.class.name.downcase}", collection_object)
20
+ instance_variable_set("@#{collection_name}", collection_object)
21
21
 
22
22
  filters_rules.all? { |rule| eval rule }
23
23
  end
@@ -29,6 +29,7 @@ module Skimming
29
29
  set_instance_variables
30
30
 
31
31
  skimming_associations = Skimming.configuration.options[subject.class.name.underscore][:skim_through] if skimming_associations.blank?
32
+ skimming_associations = [skimming_associations] unless skimming_associations.respond_to? :each
32
33
  skimming_result = []
33
34
 
34
35
  skimming_associations.each do |association_name|
data/skimming.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'skimming'
3
- s.version = '0.0.1'
3
+ s.version = '0.0.2'
4
4
  s.date = '2020-04-18'
5
5
  s.summary = "Collections skimming"
6
6
  s.description = "Filter your collections with database-configurable rules"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: skimming
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Valerio Bellaveglia
@@ -17,6 +17,7 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - ".gitignore"
20
+ - README.md
20
21
  - lib/generators/skimming_migration_generator.rb
21
22
  - lib/models/skimming/collection_filter.rb
22
23
  - lib/skimming.rb