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 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
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.pryrc ADDED
@@ -0,0 +1,2 @@
1
+ load File.dirname(__FILE__) + '/test/test_helper.rb'
2
+ load File.dirname(__FILE__) + '/test/starship.rb'
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mollusk.gemspec
4
+ gemspec
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 [![Build Status](https://travis-ci.org/matthull/mollusk.svg?branch=master)](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
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "test/*_test.rb"
6
+ end
7
+
8
+ desc "Run all tests"
9
+ task default: :test
data/lib/mollusk.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "mollusk/collection"
2
+ require "mollusk/filterable"
3
+ require "mollusk/version"
@@ -0,0 +1,3 @@
1
+ module Mollusk
2
+ module Collection; end
3
+ end
@@ -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
@@ -0,0 +1,3 @@
1
+ module Mollusk
2
+ VERSION = "0.1"
3
+ 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
@@ -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
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mollusk'
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