hashcraft 1.0.0.pre.alpha.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9dfbb2b3856098765290235f1fd1debd3fbd267cac38235e2aa2cf87fe35dac8
4
+ data.tar.gz: 41888aa7d9b3f977308d47039a3732d0b5d0973af8ab9bcd45f08ee986408027
5
+ SHA512:
6
+ metadata.gz: de2140ec7116e7e284e11d7fe11eaa3266b91025f0e2235050492a9eef1bb35ceb88ab7557728acd808516413b25f6a85928e0190a9779a58447f2ef51f78e6f
7
+ data.tar.gz: 8a150eefc4294b49d65e7fc950d50a5ce01739c212863546624b3775cf00af5dd5fab320573f8f2a93e86beac53773299506d7de1f3a59affc2274bedd5ab716
@@ -0,0 +1,8 @@
1
+ # See http://editorconfig.org/
2
+
3
+ [*]
4
+ trim_trailing_whitespace = true
5
+ indent_style = space
6
+ indent_size = 2
7
+ insert_final_newline = true
8
+ end_of_line = lf
@@ -0,0 +1,8 @@
1
+ .DS_Store
2
+ *.gem
3
+ /tmp
4
+ /coverage
5
+ Gemfile.lock
6
+ /pkg
7
+
8
+
@@ -0,0 +1,31 @@
1
+ Layout/LineLength:
2
+ Max: 100
3
+ Exclude:
4
+ - hashcraft.gemspec
5
+
6
+ Metrics/BlockLength:
7
+ ExcludedMethods:
8
+ - let
9
+ - it
10
+ - describe
11
+ - context
12
+ - specify
13
+ - define
14
+
15
+ Metrics/MethodLength:
16
+ Max: 30
17
+
18
+ AllCops:
19
+ TargetRubyVersion: 2.5
20
+
21
+ Metrics/AbcSize:
22
+ Max: 16
23
+
24
+ Metrics/ClassLength:
25
+ Max: 125
26
+
27
+ Style/TrailingCommaInHashLiteral:
28
+ Enabled: false
29
+
30
+ Style/TrailingCommaInArrayLiteral:
31
+ Enabled: false
@@ -0,0 +1 @@
1
+ 2.6.6
@@ -0,0 +1,24 @@
1
+ env:
2
+ global:
3
+ - CC_TEST_REPORTER_ID=dfacb8b1a535e3d102b4353f2d3aa2bdd303bc139785a3a28a7d724e57a467ac
4
+ language: ruby
5
+ rvm:
6
+ # Build on the latest stable of all supported Rubies (https://www.ruby-lang.org/en/downloads/):
7
+ - 2.5.5
8
+ - 2.6.5
9
+ - 2.7.0
10
+ cache: bundler
11
+ before_script:
12
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
13
+ - chmod +x ./cc-test-reporter
14
+ - ./cc-test-reporter before-build
15
+ script:
16
+ - bundle exec rubocop
17
+ - bundle exec rspec spec --format documentation
18
+ after_script:
19
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
20
+ addons:
21
+ # https://docs.travis-ci.com/user/uploading-artifacts/
22
+ artifacts:
23
+ paths:
24
+ - Gemfile.lock
@@ -0,0 +1,7 @@
1
+ # 1.0.0 TBD
2
+
3
+ Initial, feature-complete implementation.
4
+
5
+ # 0.0.0 June 28th, 2020
6
+
7
+ Initial library shell.
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at oss@bluemarblepayroll.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ guard :rspec, cmd: 'DISABLE_SIMPLECOV=true bundle exec rspec --format=documentation' do
4
+ require 'guard/rspec/dsl'
5
+ dsl = Guard::RSpec::Dsl.new(self)
6
+
7
+ # RSpec files
8
+ rspec = dsl.rspec
9
+ watch(rspec.spec_helper) { rspec.spec_dir }
10
+ watch(rspec.spec_support) { rspec.spec_dir }
11
+ watch(rspec.spec_files)
12
+
13
+ # Ruby files
14
+ ruby = dsl.ruby
15
+ dsl.watch_spec_files_for(ruby.lib_files)
16
+ end
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2020 Blue Marble Payroll, LLC
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,305 @@
1
+ # Hashcraft
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/hashcraft.svg)](https://badge.fury.io/rb/hashcraft) [![Build Status](https://travis-ci.org/bluemarblepayroll/hashcraft.svg?branch=master)](https://travis-ci.org/bluemarblepayroll/hashcraft) [![Maintainability](https://api.codeclimate.com/v1/badges/d0efe1bf0603a5dcd4e4/maintainability)](https://codeclimate.com/github/bluemarblepayroll/hashcraft/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/d0efe1bf0603a5dcd4e4/test_coverage)](https://codeclimate.com/github/bluemarblepayroll/hashcraft/test_coverage) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4
+
5
+ Provides a DSL for implementing classes which can then be consumed to create pre-defined hashes. At Blue Marble we use this library to help define user interface components like grids, modals, panes, tabs, dialogs, etc. It allows for our team to create code-first Ruby contracts for a React component library, essentially helping to create a "transpilation" pipeline: Ruby -> JSON -> React.
6
+
7
+ ## Installation
8
+
9
+ To install through Rubygems:
10
+
11
+ ````
12
+ gem install install hashcraft
13
+ ````
14
+
15
+ You can also add this to your Gemfile:
16
+
17
+ ````
18
+ bundle add hashcraft
19
+ ````
20
+
21
+ ## Getting Started
22
+
23
+ ### A Simple Example
24
+
25
+ Imagine we want to build a Ruby class that defines a grid. Imagine we would also like to use this class to derive grids from it, using the class as the "contract". We could start with this:
26
+
27
+ ````ruby
28
+ class Grid < Hashcraft::Base
29
+ option :api_url,
30
+ :name
31
+ end
32
+ ````
33
+
34
+ We could then derive grids from it using the constructor:
35
+
36
+ ````ruby
37
+ config = Grid.new(api_url: '/patients', name: 'PatientsGrid').to_h
38
+ ````
39
+
40
+ or using blocks:
41
+
42
+ ````ruby
43
+ config = Grid.new do
44
+ api_url '/patients'
45
+ name 'PatientsGrid'
46
+ end.to_h
47
+ ````
48
+
49
+ Both net the same value for `config`:
50
+
51
+ ````ruby
52
+ {
53
+ "api_url" => "/patients",
54
+ "name" => "PatientsGrid"
55
+ }
56
+ ````
57
+
58
+ But what if we want to add columns? We could add two new building blocks: the column and the content (what goes in a column):
59
+
60
+ ````ruby
61
+ class Content < Hashcraft::Base
62
+ option :property
63
+ end
64
+
65
+ class Column < Hashcraft::Base
66
+ option :header
67
+
68
+ option :content, craft: Content,
69
+ mutator: :array,
70
+ key: :contents
71
+ end
72
+
73
+ class Grid < Hashcraft::Base
74
+ option :api_url,
75
+ :name
76
+
77
+ option :column, craft: Column,
78
+ mutator: :array,
79
+ key: :columns
80
+ end
81
+ ````
82
+
83
+ Now that we have declared the overall structure of the contract, we can use it like this:
84
+
85
+ ````ruby
86
+ config = Grid.new do
87
+ api_url '/patients'
88
+ name 'PatientsGrid'
89
+
90
+ column header: 'ID #' do
91
+ content property: :id
92
+ end
93
+
94
+ column header: 'First Name' do
95
+ content property: :first
96
+ end
97
+
98
+ column header: 'Last Name' do
99
+ content property: :last
100
+ end
101
+ end.to_h
102
+ ````
103
+
104
+ This would net us the following value for config:
105
+
106
+ ````ruby
107
+ {
108
+ "api_url" => "/patients",
109
+ "name" => "PatientsGrid",
110
+ "columns" => [
111
+ {
112
+ "header" => "ID #",
113
+ "contents" => [
114
+ { "property" => :id }
115
+ ]
116
+ },
117
+ {
118
+ "header" => "First Name",
119
+ "contents" => [
120
+ { "property" => :first }
121
+ ]
122
+ },
123
+ {
124
+ "header" => "Last Name",
125
+ "contents" => [
126
+ { "property" => :last }
127
+ ]
128
+ }
129
+ ]
130
+ }
131
+ ````
132
+
133
+ ### The Option API
134
+
135
+ The minimal declaration of an available option for a class is as follows:
136
+
137
+ ````ruby
138
+ class Grid < Hashcraft::Base
139
+ option :api_url
140
+ end
141
+ ````
142
+
143
+ This means there is an available method called api_url that can be called to set a key, called api_url, to its passed in value. But there are several additional options available:
144
+
145
+ * **craft**: Hashable::Base subclass constant used as a building block (Column in the above example.) When defined it will be hydrated with the declared arguments and block and have #to_h called on it.
146
+ * **default**: the value to initialize the key to, used in conjunction with eager. When eager is true then this value will be used to set the key's default value to. Note that this value will be simply overridden if it is declared at run-time.
147
+ * **eager**: always assign a value. When true it will always assign the key a value.
148
+ * **key**: allows for aliasing keys. If omitted, the key will be the option's method name (api_url as noted above).
149
+ * **meta**: used to store any arbitrary data that can be accessed with transformers.
150
+ * **mutator**: defines the type of the data backing the method, defaulting to property. When the default property is used then it will simply assign the passed in value. Other values are: `hash` and `array`. When hash is used then the passed in value will be merged onto the key's value. When array is used then the passed in value will be pushed onto the key's value.
151
+
152
+ ### Internationalization Support
153
+
154
+ There is currently no first-class support for internationalization, but you can easily leverage the Option API meta field along with a custom value transformer to achieve this. See the Transformers section for an example using Rails I18n mechanic.
155
+
156
+ ### Transformers
157
+
158
+ Transformers are optional but come into play when you need any additional/special processing of keys and values. By default, keys and values use the pass-thru transformer (Hashable::Transformers::PassThru) but can be explicitly passed any object that responds to `#transform(value, option)`.
159
+
160
+ #### Key Transformer Example
161
+
162
+ Say, for example, we wanted to transform all keys to camel case. We could create our own transformer, such as:
163
+
164
+ ````ruby
165
+ class CamelCase
166
+ def transform(value, _option)
167
+ name = value.to_s.split('_').collect(&:capitalize).join
168
+
169
+ name[0, 1].downcase + name[1..-1]
170
+ end
171
+ end
172
+ ````
173
+
174
+ We can then use this when deriving hashes (using the above Grid example):
175
+
176
+ ````ruby
177
+ config = Grid.new do
178
+ api_url '/patients'
179
+ end.to_h(key_transformer: CamelCase.new)
180
+ ````
181
+
182
+ The resulting `config` value will now be:
183
+
184
+ ````ruby
185
+ {
186
+ "apiUrl" => "/patients"
187
+ }
188
+ ````
189
+
190
+ Note that this library ships with some basic transformers like the one mentioned above. If you want to use this then you can simply do this:
191
+
192
+ ````ruby
193
+ config = Grid.new do
194
+ api_url '/patients'
195
+ end.to_h(key_transformer: :camel_case)
196
+ ````
197
+
198
+ See Hashcraft::TransformerRegistry for a full list of provided transformers.
199
+
200
+ #### Value Transformer Example
201
+
202
+ You can plug in internationalization by creating a custom value transformer and leveraging the Option API meta directive:
203
+
204
+ ````ruby
205
+ class Localizer
206
+ def transform(value, option)
207
+ if option.meta(:localize)
208
+ I18n.l(value)
209
+ else
210
+ value
211
+ end
212
+ end
213
+ end
214
+ ````
215
+
216
+ Building on our Grid example, we could enhance the Column object as such:
217
+
218
+ ````ruby
219
+ class Column < Hashcraft::Base
220
+ option :header, meta: { localize: true }
221
+
222
+ option :content, craft: Content,
223
+ mutator: :array,
224
+ key: :contents
225
+ end
226
+ ````
227
+
228
+ We can then use the new value transformer (Localizer) when deriving hashes (using the above Grid and updated Column example):
229
+
230
+ ````yaml
231
+ en:
232
+ id: Identification Number
233
+ first: First Name
234
+ ````
235
+
236
+ ````ruby
237
+ config = Grid.new do
238
+ column header: :id
239
+ column header: :first
240
+ end.to_h(value_transformer: Localizer.new)
241
+ ````
242
+
243
+ Assuming our en.yml looks like the above example and our locale is set to:en then the resulting `config` value will now be:
244
+
245
+ ````ruby
246
+ {
247
+ "columns" => [
248
+ { header: 'Identification Number' },
249
+ { header: 'First Name' },
250
+ ]
251
+ }
252
+ ````
253
+
254
+ ## Contributing
255
+
256
+ ### Development Environment Configuration
257
+
258
+ Basic steps to take to get this repository compiling:
259
+
260
+ 1. Install [Ruby](https://www.ruby-lang.org/en/documentation/installation/) (check hashcraft.gemspec for versions supported)
261
+ 2. Install bundler (gem install bundler)
262
+ 3. Clone the repository (git clone git@github.com:bluemarblepayroll/hashcraft.git)
263
+ 4. Navigate to the root folder (cd hashcraft)
264
+ 5. Install dependencies (bundle)
265
+
266
+ ### Running Tests
267
+
268
+ To execute the test suite run:
269
+
270
+ ````
271
+ bundle exec rspec spec --format documentation
272
+ ````
273
+
274
+ Alternatively, you can have Guard watch for changes:
275
+
276
+ ````
277
+ bundle exec guard
278
+ ````
279
+
280
+ Also, do not forget to run Rubocop:
281
+
282
+ ````
283
+ bundle exec rubocop
284
+ ````
285
+
286
+ ### Publishing
287
+
288
+ Note: ensure you have proper authorization before trying to publish new versions.
289
+
290
+ After code changes have successfully gone through the Pull Request review process then the following steps should be followed for publishing new versions:
291
+
292
+ 1. Merge Pull Request into master
293
+ 2. Update `lib/hashcraft/version.rb` using [semantic versioning](https://semver.org/)
294
+ 3. Install dependencies: `bundle`
295
+ 4. Update `CHANGELOG.md` with release notes
296
+ 5. Commit & push master to remote and ensure CI builds master successfully
297
+ 6. 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).
298
+
299
+ ## Code of Conduct
300
+
301
+ Everyone interacting in this codebase, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bluemarblepayroll/hashcraft/blob/master/CODE_OF_CONDUCT.md).
302
+
303
+ ## License
304
+
305
+ This project is MIT Licensed.