polist 0.4.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 73c8e323de7cb71b6b39441dd95abf7fbccb8de9cb391790faf2fde7f7f4a20a
4
- data.tar.gz: a281f80553b3db467785bd1a2c04701a428c8dac372658369b8ddae8b5cc22b3
3
+ metadata.gz: 4a3ccf0f8d1e783625a5f9b9c7f812e199fd965daba514a26e24dd17579d1324
4
+ data.tar.gz: 00c5fd25949bcbadcd391f526ed744428e82d657454c173a3598c196ffa50df5
5
5
  SHA512:
6
- metadata.gz: 6af97512d4e3f8b135369ce37dedc444758c3d060af1df1839b80a296d2305469b4399138312f31b437d993681b726e01d54054b61d753da99ce4130a085fbef
7
- data.tar.gz: 99420ff893d32b0eb1b02b498e0f51dede79054e24ef4f6282bdbb029f329e7fdebf19c6263e03c0298c44f42a8613d87b564c99e71dc88270fb07d40baaf4ec
6
+ metadata.gz: d037d246cbb437c7c33e58cc54f682ceaeb0a041d03e8d70f81cb8d994725d5295069dd53507af0f3e01f0d4e39356e911c578c3fd9217a53e6c127db60ac6b9
7
+ data.tar.gz: 657941a65cf0e2d806967663d7fd32949c96e41d35da152a9e2a95218ec4ecf374605db9df7b3584a810a897bcb7d4db84eb755bd834d81c2a81c845a8284bb2
data/CHANGELOG.md ADDED
@@ -0,0 +1,21 @@
1
+ # Changelog
2
+
3
+ ## [Unreleased]
4
+
5
+ ## [1.0.0] - 2019-02-03
6
+
7
+ ### Added
8
+
9
+ - Add `Polist::Struct` and `Polist::Builder` ([@tycooon]) [#3]
10
+
11
+ ## [0.4.0] - 2017-10-31
12
+
13
+ - Initial public version.
14
+
15
+ [0.4.0]: https://github.com/umbrellio/polist/tree/v0.4.0
16
+ [1.0.0]: https://github.com/umbrellio/polist/compare/v0.4.0...v1.0.0
17
+ [Unreleased]: https://github.com/umbrellio/polist/compare/v1.0.0...HEAD
18
+
19
+ [@tycooon]: https://github.com/tycooon
20
+
21
+ [#3]: https://github.com/umbrellio/polist/pull/3
data/Gemfile CHANGED
@@ -4,3 +4,5 @@ source "https://rubygems.org"
4
4
 
5
5
  # Specify your gem's dependencies in polist.gemspec
6
6
  gemspec
7
+
8
+ gem "rubocop-config-umbrellio", github: "umbrellio/code-style"
data/README.md CHANGED
@@ -1,11 +1,17 @@
1
1
  # Polist   [![Gem Version](https://badge.fury.io/rb/polist.svg)](https://badge.fury.io/rb/polist) [![Build Status](https://travis-ci.org/umbrellio/polist.svg?branch=master)](https://travis-ci.org/umbrellio/polist) [![Coverage Status](https://coveralls.io/repos/github/umbrellio/polist/badge.svg?branch=master)](https://coveralls.io/github/umbrellio/polist?branch=master)
2
2
 
3
- `Polist::Service` is a simple class designed for creating service classes.
3
+ Polist is a set of simple tools for creating business logic layer of your applications:
4
4
 
5
- ### Installation
6
- Juts add `gem "polist"` to your Gemfile.
5
+ - `Polist::Service` is a simple class designed for creating service classes.
6
+ - `Polist::Builder` is a builder system based on `Uber::Builder`.
7
+ - `Polist::Struct` is a small utility that helps generating simple `Struct`-like object initializers.
8
+
9
+ ## Installation
10
+
11
+ Simply add `gem "polist"` to your Gemfile.
12
+
13
+ ## Using Polist::Service
7
14
 
8
- ### Basic usage example
9
15
  ```ruby
10
16
  class MyService < Polist::Service
11
17
  def call
@@ -51,6 +57,7 @@ end
51
57
  Note that `.run` and `.call` are just shortcuts for `MyService.new(...).run` and `MyService.new(...).call` with the only difference that they always return the service instance instead of the result of `#run` or `#call`. Unlike `#call` though, `#run` is not intended to be owerwritten in subclasses.
52
58
 
53
59
  ### Using Form objects
60
+
54
61
  Sometimes you want to use some kind of params parsing and/or validation, and you can do that with the help of `Polist::Service::Form` class. It uses [tainbox](https://github.com/enthrops/tainbox) gem under the hood.
55
62
 
56
63
  ```ruby
@@ -83,12 +90,95 @@ end
83
90
 
84
91
  MyService.call(param1: "1", param2: "2") # prints false and then ["1", 2, "smth"]
85
92
  ```
93
+
86
94
  The `#form` method is there just for convinience and by default it uses what `#form_attributes` returns as the attributes for the default form class which is the services' `Form` class. You are free to use as many different form classes as you want in your service.
87
95
 
96
+ ## Using Polist::Builder
97
+
98
+ The build logic is based on [Uber::Builder](https://github.com/apotonick/uber#builder) but it allows recursive builders. See the example:
99
+
100
+ Can be used with `Polist::Service` or any other Ruby class.
101
+
102
+ ```ruby
103
+ class User
104
+ include Polist::Builder
105
+
106
+ builds do |role|
107
+ case role
108
+ when /admin/
109
+ Admin
110
+ end
111
+ end
112
+
113
+ attr_accessor :role
114
+
115
+ def initialize(role)
116
+ self.role = role
117
+ end
118
+ end
119
+
120
+ class Admin < User
121
+ builds do |role|
122
+ SuperAdmin if role == "super_admin"
123
+ end
124
+
125
+ class SuperAdmin < Admin
126
+ def super?
127
+ true
128
+ end
129
+ end
130
+
131
+ def super?
132
+ false
133
+ end
134
+ end
135
+
136
+ User.build("user") # => #<User:... @role="user">
137
+
138
+ User.build("admin") # => #<Admin:... @role="admin">
139
+ User.build("admin").super? # => false
140
+
141
+ User.build("super_admin") # => #<Admin::SuperAdmin:... @role="super_admin">
142
+ User.build("super_admin").super? # => true
143
+
144
+ Admin.build("smth") # => #<Admin:... @role="admin">
145
+ SuperAdmin.build("smth") # => #<Admin::SuperAdmin:... @role="admin">
146
+ ```
147
+
148
+ ## Using Polist::Struct
149
+
150
+ Works pretty much the same like Ruby `Struct` class, but you don't have to subclass it.
151
+
152
+ Can be used with `Polist::Service` or any other class that don't have initializer specified.
153
+
154
+ ```ruby
155
+ class Point
156
+ include Polist::Struct
157
+
158
+ struct :x, :y
159
+ end
160
+
161
+ a = Point.new(15, 25)
162
+ a.x # => 15
163
+ a.y # => 25
164
+
165
+ b = Point.new(15, 25, 35) # raises ArgumentError: struct size differs
166
+
167
+ c = Point.new(15)
168
+ c.x # => 15
169
+ c.y # => nil
170
+ ```
171
+
172
+ ## Contributing
173
+
174
+ Bug reports and pull requests are welcome on GitHub at https://github.com/umbrellio/polist.
175
+
88
176
  ## License
177
+
89
178
  Released under MIT License.
90
179
 
91
180
  ## Authors
181
+
92
182
  Created by Yuri Smirnov.
93
183
 
94
184
  <a href="https://github.com/umbrellio/">
data/lib/polist.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "polist/builder"
3
4
  require "polist/service"
5
+ require "polist/struct"
4
6
  require "polist/version"
5
-
6
- module Polist
7
- end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uber/builder"
4
+
5
+ module Polist
6
+ module Builder
7
+ def self.included(base)
8
+ base.include(Uber::Builder)
9
+ base.extend(ClassMethods)
10
+ end
11
+
12
+ module ClassMethods
13
+ # Recursively runs class builders on class until no builders on that class found
14
+ # or some builder returns the class itself
15
+ def build_klass(*args)
16
+ klass = self
17
+
18
+ loop do
19
+ new_klass = klass.build!(klass, *args)
20
+ break if new_klass == klass
21
+ klass = new_klass
22
+ end
23
+
24
+ klass
25
+ end
26
+
27
+ def build(*args)
28
+ build_klass(*args).new(*args)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polist
4
+ module Struct
5
+ module ClassMethods
6
+ def struct(*attrs)
7
+ attr_accessor(*attrs)
8
+
9
+ define_method(:initialize) do |*args|
10
+ raise ArgumentError, "struct size differs" if args.length > attrs.length
11
+ attrs.zip(args).each { |attr, val| public_send(:"#{attr}=", val) }
12
+ end
13
+ end
14
+ end
15
+
16
+ def self.included(base)
17
+ base.extend(ClassMethods)
18
+ end
19
+ end
20
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Polist
4
- VERSION = "0.4.0"
4
+ VERSION = "1.0.0"
5
5
  end
data/polist.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- lib = File.expand_path("../lib", __FILE__)
3
+ lib = File.expand_path("lib", __dir__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  require "polist/version"
6
6
 
@@ -10,8 +10,8 @@ Gem::Specification.new do |spec|
10
10
  spec.authors = ["Yuri Smirnov"]
11
11
  spec.email = ["tycooon@yandex.ru"]
12
12
 
13
- spec.summary = "A gem for creating simple service classes."
14
- spec.description = "Polist is a gem for creating simple service classes."
13
+ spec.summary = "A gem for creating simple service classes and more."
14
+ spec.description = "Polist is a gem for creating simple service classes and more."
15
15
  spec.homepage = "https://github.com/umbrellio/polist"
16
16
  spec.license = "MIT"
17
17
 
@@ -21,12 +21,12 @@ Gem::Specification.new do |spec|
21
21
  spec.add_runtime_dependency "activemodel", ">= 3.0"
22
22
  spec.add_runtime_dependency "plissken", ">= 0.3"
23
23
  spec.add_runtime_dependency "tainbox"
24
+ spec.add_runtime_dependency "uber"
24
25
 
25
26
  spec.add_development_dependency "bundler"
26
27
  spec.add_development_dependency "coveralls"
27
28
  spec.add_development_dependency "pry"
28
29
  spec.add_development_dependency "rake"
29
30
  spec.add_development_dependency "rspec"
30
- spec.add_development_dependency "rubocop-config-umbrellio"
31
31
  spec.add_development_dependency "simplecov"
32
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuri Smirnov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-31 00:00:00.000000000 Z
11
+ date: 2019-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -53,13 +53,13 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: bundler
56
+ name: uber
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
- type: :development
62
+ type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: coveralls
70
+ name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: pry
84
+ name: coveralls
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -95,7 +95,7 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: rake
98
+ name: pry
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
@@ -109,7 +109,7 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
- name: rspec
112
+ name: rake
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
@@ -123,7 +123,7 @@ dependencies:
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  - !ruby/object:Gem::Dependency
126
- name: rubocop-config-umbrellio
126
+ name: rspec
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - ">="
@@ -150,7 +150,7 @@ dependencies:
150
150
  - - ">="
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
- description: Polist is a gem for creating simple service classes.
153
+ description: Polist is a gem for creating simple service classes and more.
154
154
  email:
155
155
  - tycooon@yandex.ru
156
156
  executables: []
@@ -161,12 +161,15 @@ files:
161
161
  - ".rspec"
162
162
  - ".rubocop.yml"
163
163
  - ".travis.yml"
164
+ - CHANGELOG.md
164
165
  - Gemfile
165
166
  - LICENSE.txt
166
167
  - README.md
167
168
  - Rakefile
168
169
  - lib/polist.rb
170
+ - lib/polist/builder.rb
169
171
  - lib/polist/service.rb
172
+ - lib/polist/struct.rb
170
173
  - lib/polist/version.rb
171
174
  - polist.gemspec
172
175
  homepage: https://github.com/umbrellio/polist
@@ -188,9 +191,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
188
191
  - !ruby/object:Gem::Version
189
192
  version: '0'
190
193
  requirements: []
191
- rubyforge_project:
192
- rubygems_version: 2.7.6
194
+ rubygems_version: 3.0.1
193
195
  signing_key:
194
196
  specification_version: 4
195
- summary: A gem for creating simple service classes.
197
+ summary: A gem for creating simple service classes and more.
196
198
  test_files: []