statics 1.0.0

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
+ SHA256:
3
+ metadata.gz: 93c607e75512692565ed790b398a9478ffa30282245154d9f414217b3eb79480
4
+ data.tar.gz: 00d476425ece47fa15b14320caa55ab1702100c3c5680fcf0560b47dba859cbd
5
+ SHA512:
6
+ metadata.gz: b16a0eb5977be24b1f424c1e43257c319f97b22f6e20249626fca83fdec23b07279856b4765cfc1c717f6ffbdcb8f0f6f1d9ba5c3da906d92779a83b58e69b8d
7
+ data.tar.gz: e41db198c8702cfaaa76f6b1caa167e3cd1fafcef21b39943f8ece0c130b54eda531bd3d964b5a8a82ef519715686ae317f0ebf2370d91763144af4faca65231
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Pablo Crivella.
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,144 @@
1
+ # Statics
2
+
3
+ [![Gem](https://img.shields.io/gem/v/statics.svg?style=flat)](http://rubygems.org/gems/statics)
4
+ [![CircleCI](https://img.shields.io/circleci/project/github/pablocrivella/statics.svg)](https://circleci.com/gh/pablocrivella/statics)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/935822c7c481aa464186/maintainability)](https://codeclimate.com/github/pablocrivella/statics/maintainability)
6
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/935822c7c481aa464186/test_coverage)](https://codeclimate.com/github/pablocrivella/statics/test_coverage)
7
+
8
+ Base class and modules for static models.
9
+
10
+ Links:
11
+
12
+ - [API Docs](https://www.rubydoc.info/gems/statics)
13
+ - [Contributing](https://github.com/pablocrivella/statics/blob/master/CONTRIBUTING.md)
14
+ - [Code of Conduct](https://github.com/pablocrivella/statics/blob/master/CODE_OF_CONDUCT.md)
15
+
16
+ ## Requirements
17
+
18
+ 1. [Ruby 2.5.0](https://www.ruby-lang.org)
19
+
20
+ ## Installation
21
+
22
+ To install, run:
23
+
24
+ ```
25
+ gem install statics
26
+ ```
27
+
28
+ Or add the following to your Gemfile:
29
+
30
+ ```
31
+ gem "statics"
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ Setting the data path:
37
+
38
+ ```ruby
39
+ Statics.configure do |config|
40
+ config.data_path = "data/"
41
+ end
42
+ ```
43
+
44
+ Defining a static model:
45
+
46
+ ```ruby
47
+ class Post < Statics::Model
48
+ filename "posts"
49
+
50
+ attribute :title, Types::Strict::String
51
+ end
52
+ ```
53
+
54
+ ```yml
55
+ # data/posts.yml
56
+ ---
57
+ post1:
58
+ title: "Post 1"
59
+
60
+ post2:
61
+ title: "Post 2"
62
+ ```
63
+
64
+ ```ruby
65
+ Post.all
66
+ #=> #<Statics::Collection records=[#<Post key=:post1 title="Post 1">, #<Post key=:post2 title="Post 2">]>
67
+ Post.where(title: "Post 1")
68
+ #=> #<Statics::Collection records=[#<Post key=:post1 title="Post 1">]>
69
+ Post.where_not(title: "Post 1")
70
+ #=> #<Statics::Collection records=[#<Post key=:post2 title="Post 2">]>
71
+ Post.find_by(key: :post1)
72
+ #=> #<Post key=:post1 title="Post 1">
73
+ Post[:post1]
74
+ #=> #<Post key=:post1 title="Post 1">
75
+ Post.pluck(:title)
76
+ #=> ["Post 1", "Post 2"]
77
+ post = Post.first
78
+ #=> #<Post key=:post1 title="Post 1">
79
+ post.key
80
+ #=> :post1
81
+ post.title
82
+ #=> "Post 1"
83
+ post.attributes
84
+ #=> {:title=>"Post 1", :key=>:post1}
85
+ ```
86
+
87
+ Defining translatable attributes:
88
+
89
+ ```ruby
90
+ class Post < Statics::Model
91
+ include Statics::Translatable
92
+
93
+ filename "posts"
94
+
95
+ attribute :title, Types::Strict::String
96
+ translatable_attribute :body
97
+ end
98
+
99
+ ```
100
+
101
+ ```yml
102
+ # data/posts.yml
103
+ ---
104
+ post1:
105
+ title: "Post 1"
106
+ body:
107
+ en: "Hello!"
108
+ nl: "Hallo!"
109
+
110
+ post2:
111
+ title: "Post 2"
112
+ body:
113
+ en: "Bye!"
114
+ nl: "Doei!"
115
+
116
+ ```
117
+
118
+ ```ruby
119
+ post = Post.first
120
+ # when I18n.locale is :en
121
+ post.body #=> "Hello!"
122
+ post.body(locale: :nl) #=> "Hallo!"
123
+ ```
124
+
125
+ ## Tests
126
+
127
+ To test, run:
128
+
129
+ ```
130
+ bundle exec rspec spec/
131
+ ```
132
+
133
+ ## Versioning
134
+
135
+ Read [Semantic Versioning](https://semver.org) for details. Briefly, it means:
136
+
137
+ - Major (X.y.z) - Incremented for any backwards incompatible public API changes.
138
+ - Minor (x.Y.z) - Incremented for new, backwards compatible, public API enhancements/fixes.
139
+ - Patch (x.y.Z) - Incremented for small, backwards compatible, bug fixes.
140
+
141
+ ## License
142
+
143
+ Copyright 2018 [Pablo Crivella](https://pablocrivella.me).
144
+ Read [LICENSE](LICENSE.md) for details.
data/lib/statics.rb ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/configurable"
4
+ require "dry/struct"
5
+ require "dry/core"
6
+ require "i18n"
7
+ require "yaml"
8
+ require "forwardable"
9
+
10
+ require "statics/errors"
11
+ require "statics/types"
12
+ require "statics/collection"
13
+ require "statics/model"
14
+ require "statics/translatable"
15
+ require "statics/version"
16
+
17
+ module Statics
18
+ extend Dry::Configurable
19
+
20
+ setting :data_path, reader: true
21
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Statics
4
+ class Collection
5
+ extend Forwardable
6
+ include Enumerable
7
+ include Dry::Equalizer(:records)
8
+
9
+ def_delegators :@records, :last, :size
10
+
11
+ # @param records [Array<Statics::Model>]
12
+ def initialize(records)
13
+ @records = records
14
+ end
15
+
16
+ # @return [Statics::Collection]
17
+ def each(&block)
18
+ self.class.new(records.each(&block))
19
+ end
20
+
21
+ # @return [Statics::Collection]
22
+ def select(&block)
23
+ self.class.new(records.select(&block))
24
+ end
25
+
26
+ # @return [Statics::Collection]
27
+ def reject(&block)
28
+ self.class.new(records.reject(&block))
29
+ end
30
+
31
+ # @param conditions [Hash]
32
+ # @return [Statics::Collection]
33
+ def where(conditions)
34
+ select { |record| filter?(record, conditions) }
35
+ end
36
+
37
+ # @param conditions [Hash]
38
+ # @return [Statics::Collection]
39
+ def where_not(conditions)
40
+ reject { |record| filter?(record, conditions) }
41
+ end
42
+
43
+ # @param conditions [Hash]
44
+ # @return [Statics::Model]
45
+ def find_by(conditions)
46
+ find { |record| filter?(record, conditions) }
47
+ end
48
+
49
+ # @return [Array<Symbol>]
50
+ def keys
51
+ pluck(:key)
52
+ end
53
+
54
+ # @param attributes [Array<Symbol>]
55
+ # @return [Array<Object>]
56
+ def pluck(*attributes)
57
+ map { |record| record.attributes.slice(*attributes).values }
58
+ .tap { |result| result.flatten! if attributes.size == 1 }
59
+ end
60
+
61
+ private
62
+
63
+ attr_reader :records
64
+
65
+ # @param record [Static::Model]
66
+ # @param conditions [Hash]
67
+ # @return [true, false]
68
+ def filter?(record, conditions)
69
+ conditions.all? do |attribute, value|
70
+ case value
71
+ when Array
72
+ value.include?(record.send(attribute))
73
+ else
74
+ record.send(attribute) == value
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Statics
4
+ class KeyNotFoundError < StandardError
5
+ end
6
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Statics
4
+ class Model < Dry::Struct
5
+ transform_keys(&:to_sym)
6
+
7
+ defines :filename
8
+
9
+ attribute :key, Types::Strict::Symbol.constructor(&:to_sym)
10
+
11
+ class << self
12
+ # @param key [Symbol]
13
+ # @raise [Statics::KeyNotFoundError] if key is not found.
14
+ # @return [Statics::Model]
15
+ def [](key)
16
+ new(file_contents.fetch(key.to_s) { raise KeyNotFoundError }.merge(key: key.to_sym))
17
+ end
18
+
19
+ # @return [Statics::Collection]
20
+ def all
21
+ @all ||= Collection.new(records)
22
+ end
23
+
24
+ def method_missing(method, *args, &block)
25
+ if Collection.instance_methods.include?(method)
26
+ all.public_send(method, *args, &block)
27
+ else
28
+ super
29
+ end
30
+ end
31
+
32
+ def respond_to_missing?(method, include_private = false)
33
+ Collection.instance_methods(false).include?(method) || super
34
+ end
35
+
36
+ private
37
+
38
+ # @return [Hash]
39
+ def file_contents
40
+ @file_contents ||= YAML.load_file(path)
41
+ end
42
+
43
+ # @return [Array<Statics::Model>]
44
+ def records
45
+ file_contents.map do |key, attributes|
46
+ new(attributes.merge(key: key.to_sym))
47
+ end
48
+ end
49
+
50
+ # @return [String]
51
+ def path
52
+ File.join(Statics.data_path, "#{filename}.yml")
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Statics
4
+ module Translatable
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ def translatable_attributes(*names)
11
+ names.each { |name| translatable_attribute(name) }
12
+ end
13
+
14
+ def translatable_attribute(name)
15
+ attribute(name, Types::Map(Types::Strict::Symbol.constructor(&:to_sym), Types::Strict::String))
16
+ override_translatable_attribute_getter(name)
17
+ end
18
+
19
+ def override_translatable_attribute_getter(name)
20
+ define_method(name) do |locale: I18n.locale|
21
+ attributes.dig(name, locale.to_sym)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Types
4
+ include Dry::Types.module
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Statics
4
+ VERSION = "1.0.0"
5
+ end
metadata ADDED
@@ -0,0 +1,240 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: statics
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Pablo Crivella
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-08-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-equalizer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-struct
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-types
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.13'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: i18n
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.16'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.16'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.11'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.11'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.6'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.6'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '12.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '12.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec_junit_formatter
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.4'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.4'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '0.58'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '0.58'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rubocop-rspec
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '1.29'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '1.29'
181
+ - !ruby/object:Gem::Dependency
182
+ name: simplecov
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '0.16'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '0.16'
195
+ description:
196
+ email:
197
+ - pablocrivella@gmail.com
198
+ executables: []
199
+ extensions: []
200
+ extra_rdoc_files:
201
+ - README.md
202
+ - LICENSE
203
+ files:
204
+ - LICENSE
205
+ - README.md
206
+ - lib/statics.rb
207
+ - lib/statics/collection.rb
208
+ - lib/statics/errors.rb
209
+ - lib/statics/model.rb
210
+ - lib/statics/translatable.rb
211
+ - lib/statics/types.rb
212
+ - lib/statics/version.rb
213
+ homepage: https://github.com/pablocrivella/statics
214
+ licenses:
215
+ - MIT
216
+ metadata:
217
+ bug_tracker_uri: https://github.com/pablocrivella/statics/issues
218
+ changelog_uri: https://github.com/pablocrivella/statics/blob/master/CHANGELOG.md
219
+ source_code_uri: https://github.com/pablocrivella/statics
220
+ post_install_message:
221
+ rdoc_options: []
222
+ require_paths:
223
+ - lib
224
+ required_ruby_version: !ruby/object:Gem::Requirement
225
+ requirements:
226
+ - - ">="
227
+ - !ruby/object:Gem::Version
228
+ version: '0'
229
+ required_rubygems_version: !ruby/object:Gem::Requirement
230
+ requirements:
231
+ - - ">="
232
+ - !ruby/object:Gem::Version
233
+ version: '0'
234
+ requirements: []
235
+ rubyforge_project:
236
+ rubygems_version: 2.7.7
237
+ signing_key:
238
+ specification_version: 4
239
+ summary: Base class and modules for static models.
240
+ test_files: []