activeset 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.rubocop.yml +25 -0
- data/.travis.yml +5 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +8 -0
- data/activeset.gemspec +32 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/active_set.rb +44 -0
- data/lib/active_set/base_adapter.rb +8 -0
- data/lib/active_set/base_processor.rb +8 -0
- data/lib/active_set/filter/enumerable_adapter.rb +14 -0
- data/lib/active_set/filter/processor.rb +23 -0
- data/lib/active_set/paginate/enumerable_adapter.rb +24 -0
- data/lib/active_set/paginate/processor.rb +25 -0
- data/lib/active_set/sort/enumerable_adapter.rb +15 -0
- data/lib/active_set/sort/processor.rb +23 -0
- data/lib/active_set/version.rb +5 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 643196795ab69a5f654e5dd52afc5356b51dee42
|
4
|
+
data.tar.gz: 015ca510f83e179a815839609a3687192f23b51d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7e8244ed3ea19a1fa7c43fbb974603e803b02abd0c74a193a2b49f5206b61b139c2c30041b1b92d2c1efc5e973d4298d791ac43c3f086aadcdd9967d1bab9e30
|
7
|
+
data.tar.gz: 684f8413a2f8fe98c3df7e6a0de599d9547a562592a23652ce46ad93383885127ae605cd5625e10d8e94394d639ac5fb607918f412ccabcd72671debdf493959
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
Include:
|
4
|
+
- Rakefile
|
5
|
+
Exclude:
|
6
|
+
- bin/*
|
7
|
+
- db/**/*
|
8
|
+
- spec/*_helper.rb
|
9
|
+
Metrics/LineLength:
|
10
|
+
Max: 120
|
11
|
+
# To make it possible to copy or click on URIs in the code, we allow lines
|
12
|
+
# contaning a URI to be longer than Max.
|
13
|
+
AllowURI: true
|
14
|
+
URISchemes:
|
15
|
+
- http
|
16
|
+
- https
|
17
|
+
Style/Documentation:
|
18
|
+
Enabled: false
|
19
|
+
Style/SignalException:
|
20
|
+
EnforcedStyle: semantic
|
21
|
+
Metrics/BlockLength:
|
22
|
+
Exclude:
|
23
|
+
- 'activeset.gemspec'
|
24
|
+
- 'spec/**/*_spec.rb'
|
25
|
+
- '**/schema.rb'
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Stephen Margheim
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Activeset
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/activeset`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'activeset'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install activeset
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/activeset.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/activeset.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
lib = File.expand_path('../lib', __FILE__)
|
5
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
6
|
+
require 'active_set/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.platform = Gem::Platform::RUBY
|
10
|
+
spec.name = 'activeset'
|
11
|
+
spec.version = ActiveSet::VERSION
|
12
|
+
spec.authors = ['Stephen Margheim']
|
13
|
+
spec.email = ['stephen.margheim@gmail.com']
|
14
|
+
|
15
|
+
spec.summary = 'A toolkit for working with enumerable sets.'
|
16
|
+
spec.description = 'Easily filter, sort, and paginate enumerable sets.'
|
17
|
+
spec.homepage = 'https://github.com/fractaledmind/activeset'
|
18
|
+
spec.license = 'MIT'
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
21
|
+
f.match(%r{^(test|spec|features)/})
|
22
|
+
end
|
23
|
+
spec.bindir = 'exe'
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ['lib']
|
26
|
+
|
27
|
+
spec.add_dependency 'activesupport'
|
28
|
+
|
29
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
30
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
31
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
32
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'activeset'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/active_set.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_set/version'
|
4
|
+
require 'active_support/core_ext/object/blank'
|
5
|
+
require 'active_support/core_ext/hash/slice'
|
6
|
+
require 'patches/core_ext/hash/flatten_keys'
|
7
|
+
|
8
|
+
require 'active_set/filter/processor'
|
9
|
+
require 'active_set/sort/processor'
|
10
|
+
require 'active_set/paginate/processor'
|
11
|
+
|
12
|
+
class ActiveSet
|
13
|
+
include Enumerable
|
14
|
+
|
15
|
+
attr_reader :set
|
16
|
+
|
17
|
+
def initialize(set)
|
18
|
+
@set = set
|
19
|
+
end
|
20
|
+
|
21
|
+
def each(&block)
|
22
|
+
@set.each(&block)
|
23
|
+
end
|
24
|
+
|
25
|
+
def ==(other)
|
26
|
+
return @set == other unless other.is_a?(ActiveSet)
|
27
|
+
@set == other.set
|
28
|
+
end
|
29
|
+
|
30
|
+
def filter(structure)
|
31
|
+
filterer = Filter::Processor.new(@set, structure)
|
32
|
+
self.class.new(filterer.process)
|
33
|
+
end
|
34
|
+
|
35
|
+
def sort(structure)
|
36
|
+
sorter = Sort::Processor.new(@set, structure)
|
37
|
+
self.class.new(sorter.process)
|
38
|
+
end
|
39
|
+
|
40
|
+
def paginate(structure)
|
41
|
+
paginater = Paginate::Processor.new(@set, structure)
|
42
|
+
self.class.new(paginater.process)
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../base_adapter'
|
4
|
+
|
5
|
+
class ActiveSet
|
6
|
+
module Filter
|
7
|
+
class EnumerableAdapter < BaseAdapter
|
8
|
+
def process(set)
|
9
|
+
@set = set
|
10
|
+
@set.select { |item| item.send(@key) == @value }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../base_processor'
|
4
|
+
require_relative './enumerable_adapter'
|
5
|
+
|
6
|
+
class ActiveSet
|
7
|
+
module Filter
|
8
|
+
class Processor < BaseProcessor
|
9
|
+
def process
|
10
|
+
@structure.reject { |_, v| v.blank? }
|
11
|
+
.reduce(@set) do |set, (key, value)|
|
12
|
+
adapter.new(key, value).process(set)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def adapter
|
19
|
+
EnumerableAdapter
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../base_adapter'
|
4
|
+
|
5
|
+
class ActiveSet
|
6
|
+
module Paginate
|
7
|
+
class EnumerableAdapter < BaseAdapter
|
8
|
+
def process(set)
|
9
|
+
@set = set
|
10
|
+
@set.each_slice(pagesize).take(page_number).last
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def pagesize
|
16
|
+
@value
|
17
|
+
end
|
18
|
+
|
19
|
+
def page_number
|
20
|
+
@key
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../base_processor'
|
4
|
+
require_relative './enumerable_adapter'
|
5
|
+
|
6
|
+
class ActiveSet
|
7
|
+
module Paginate
|
8
|
+
class Processor < BaseProcessor
|
9
|
+
def process
|
10
|
+
return @set if @set.count < pagesize
|
11
|
+
adapter.new(@structure[:page], pagesize).process(@set)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def adapter
|
17
|
+
EnumerableAdapter
|
18
|
+
end
|
19
|
+
|
20
|
+
def pagesize
|
21
|
+
@structure[:size] || 25
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../base_adapter'
|
4
|
+
|
5
|
+
class ActiveSet
|
6
|
+
module Sort
|
7
|
+
class EnumerableAdapter < BaseAdapter
|
8
|
+
def process(set)
|
9
|
+
@set = set
|
10
|
+
@set.sort_by { |item| item.send(@key) }
|
11
|
+
.tap { |c| c.reverse! if @value.to_s == 'desc' }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../base_processor'
|
4
|
+
require_relative './enumerable_adapter'
|
5
|
+
|
6
|
+
class ActiveSet
|
7
|
+
module Sort
|
8
|
+
class Processor < BaseProcessor
|
9
|
+
def process
|
10
|
+
@structure.reject { |_, value| value.blank? }
|
11
|
+
.reduce(@set) do |set, (key, value)|
|
12
|
+
adapter.new(key, value).process(set)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def adapter
|
19
|
+
EnumerableAdapter
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activeset
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stephen Margheim
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.15'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.15'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: Easily filter, sort, and paginate enumerable sets.
|
70
|
+
email:
|
71
|
+
- stephen.margheim@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- activeset.gemspec
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/active_set.rb
|
88
|
+
- lib/active_set/base_adapter.rb
|
89
|
+
- lib/active_set/base_processor.rb
|
90
|
+
- lib/active_set/filter/enumerable_adapter.rb
|
91
|
+
- lib/active_set/filter/processor.rb
|
92
|
+
- lib/active_set/paginate/enumerable_adapter.rb
|
93
|
+
- lib/active_set/paginate/processor.rb
|
94
|
+
- lib/active_set/sort/enumerable_adapter.rb
|
95
|
+
- lib/active_set/sort/processor.rb
|
96
|
+
- lib/active_set/version.rb
|
97
|
+
homepage: https://github.com/fractaledmind/activeset
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.5.1
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: A toolkit for working with enumerable sets.
|
121
|
+
test_files: []
|