mollusk 0.1
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 +7 -0
- data/.gitignore +17 -0
- data/.pryrc +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +45 -0
- data/Rakefile +9 -0
- data/lib/mollusk.rb +3 -0
- data/lib/mollusk/collection.rb +3 -0
- data/lib/mollusk/filterable.rb +37 -0
- data/lib/mollusk/version.rb +3 -0
- data/mollusk.gemspec +23 -0
- data/test/mollusk_test.rb +15 -0
- data/test/starship.rb +15 -0
- data/test/starships.yml +19 -0
- data/test/test_helper.rb +2 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a7f0a6fcc3607702bfaca834120481059b1bda37
|
4
|
+
data.tar.gz: 318fbe545932cd35925981793c9305a57198557b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 50b5840c69ffa49ed54ea97aefa900bda4b62ccb03b7fb5db58675ca454694d917ea1f292781c4876b44aaaac532036734ee86ddf84a8e29e37f91155106f02e
|
7
|
+
data.tar.gz: 92428119c0331f7706a318ed78104c9621db80b908d287747ad1e134734a23b7b4216367364850d6b3c8d9bd21edb5d579a8279c2a4f0cba5fb411cf63b94e98
|
data/.gitignore
ADDED
data/.pryrc
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Matt Hull
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Mollusk [](https://travis-ci.org/matthull/mollusk)
|
2
|
+
|
3
|
+
Mollusk provides chainable filters for arrays and other collections, ActiveRecord-style.
|
4
|
+
|
5
|
+
Sometimes you need to wrap a class around data from an array, yaml file,
|
6
|
+
or another source like [Money::Currency.table](https://github.com/RubyMoney/money).
|
7
|
+
With Mollusk you can add filters to these wrapper classes using syntax similar to
|
8
|
+
ActiveRecord named scopes.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'mollusk'
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install mollusk
|
23
|
+
|
24
|
+
## Usage Example
|
25
|
+
````
|
26
|
+
class Widget
|
27
|
+
extend Mollusk::Filterable
|
28
|
+
|
29
|
+
def self.all
|
30
|
+
%w( foo bar bashaz )
|
31
|
+
end
|
32
|
+
|
33
|
+
filter :starts_with_b, -> { select { |w| w.chars.first == 'b' } }
|
34
|
+
filter :long, -> { select { |w| w.length > 3 } }
|
35
|
+
end
|
36
|
+
|
37
|
+
Widget.starts_with_b #=> ['bar', 'baz']
|
38
|
+
```
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
1. Fork it ( http://github.com/matthull/mollusk/fork )
|
42
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
43
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
44
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
45
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/mollusk.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module Mollusk
|
2
|
+
# Extend a class with this module to enable filters
|
3
|
+
#
|
4
|
+
# Example:
|
5
|
+
#
|
6
|
+
# class Widget
|
7
|
+
# extend Mollusk::Filterable
|
8
|
+
#
|
9
|
+
# def self.all
|
10
|
+
# %w( foo bar baz )
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# filter :starts_with_b, -> { select { |w| w.chars.first == 'b' } }
|
14
|
+
# filter :long, -> { select { |w| w.length > 3 } }
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# Widget.starts_with_b #=> ['bar', 'baz']
|
18
|
+
module Filterable
|
19
|
+
def self.extended(base)
|
20
|
+
define_method(:_mollusk_base_class) { base }
|
21
|
+
collection_module =
|
22
|
+
Mollusk::Collection.const_set base.name.to_sym, Module.new
|
23
|
+
define_method(:_mollusk_collection_module) { collection_module }
|
24
|
+
end
|
25
|
+
|
26
|
+
def filter(name, callback)
|
27
|
+
_mollusk_collection_module.send :define_method, name, &callback
|
28
|
+
define_singleton_method(name) do
|
29
|
+
collection = self == _mollusk_base_class ? all : self
|
30
|
+
results = collection.instance_exec(&callback)
|
31
|
+
mod = _mollusk_collection_module
|
32
|
+
(class << results; self end).send :include, mod
|
33
|
+
results
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/mollusk.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mollusk/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mollusk"
|
8
|
+
spec.version = Mollusk::VERSION
|
9
|
+
spec.authors = ["Matt Hull"]
|
10
|
+
spec.email = ["matt@matthull.io"]
|
11
|
+
spec.summary = %q{Chainable filters for arrays, ActiveRecord-style}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "minitest", "5.4.0"
|
23
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
require_relative 'starship'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
|
5
|
+
class TestMollusk < MiniTest::Unit::TestCase
|
6
|
+
def test_single_filter
|
7
|
+
star_trek_ships = Starship.star_trek.map { |s| s[:class] }
|
8
|
+
assert_equal ['Constitution'], star_trek_ships
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_chained_filter
|
12
|
+
star_wars_fighters = Starship.star_wars.fighters.map { |s| s[:class] }.sort
|
13
|
+
assert_equal ['Tie Fighter', 'X-Wing'], star_wars_fighters
|
14
|
+
end
|
15
|
+
end
|
data/test/starship.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
# Collection of starships from different fictional settings
|
4
|
+
class Starship
|
5
|
+
extend Mollusk::Filterable
|
6
|
+
|
7
|
+
def self.all
|
8
|
+
yaml_path = File.dirname(__FILE__) + '/starships.yml'
|
9
|
+
YAML.load_file(yaml_path)[:starships]
|
10
|
+
end
|
11
|
+
|
12
|
+
filter :star_wars, -> { select { |s| s[:universe] == 'Star Wars' } }
|
13
|
+
filter :star_trek, -> { select { |s| s[:universe] == 'Star Trek' } }
|
14
|
+
filter :fighters, -> { select { |s| s[:type] == 'Fighter' } }
|
15
|
+
end
|
data/test/starships.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# Data for Starship test class
|
2
|
+
---
|
3
|
+
:starships:
|
4
|
+
-
|
5
|
+
:class: X-Wing
|
6
|
+
:universe: Star Wars
|
7
|
+
:type: Fighter
|
8
|
+
-
|
9
|
+
:class: Tie Fighter
|
10
|
+
:universe: Star Wars
|
11
|
+
:type: Fighter
|
12
|
+
-
|
13
|
+
:class: Star Destroyer
|
14
|
+
:universe: Star Wars
|
15
|
+
:type: Capital Ship
|
16
|
+
-
|
17
|
+
:class: Constitution
|
18
|
+
:universe: Star Trek
|
19
|
+
:type: Capital Ship
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mollusk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matt Hull
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-25 00:00:00.000000000 Z
|
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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 5.4.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 5.4.0
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- matt@matthull.io
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .pryrc
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/mollusk.rb
|
69
|
+
- lib/mollusk/collection.rb
|
70
|
+
- lib/mollusk/filterable.rb
|
71
|
+
- lib/mollusk/version.rb
|
72
|
+
- mollusk.gemspec
|
73
|
+
- test/mollusk_test.rb
|
74
|
+
- test/starship.rb
|
75
|
+
- test/starships.yml
|
76
|
+
- test/test_helper.rb
|
77
|
+
homepage: ''
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.2.2
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Chainable filters for arrays, ActiveRecord-style
|
101
|
+
test_files:
|
102
|
+
- test/mollusk_test.rb
|
103
|
+
- test/starship.rb
|
104
|
+
- test/starships.yml
|
105
|
+
- test/test_helper.rb
|