redress 0.1.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
+ SHA1:
3
+ metadata.gz: ae54aefaea2030d1a65f9ae21a2a9963f9c42419
4
+ data.tar.gz: 26c2f1c1b2e22857d8de318f0a8b4ab129f29462
5
+ SHA512:
6
+ metadata.gz: 163a1f58f0d429d8b04f0179e5ce82dc5d001993a422157dc3e50ef86bd9314783cefea8f571214639dd1c26221e0ee9007d0ef15f69786635de04100a91819b
7
+ data.tar.gz: 59ea7e648d34e13b43ebaa7a413d141d9fd065bf71460856936d1ce00364fd4d56a545b234cd29df4732cf88eda07279cb3b057a6c7a363bcb5c562395d78f2b
data/LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2017 [Fodojo LLC](https://www.fodojo.com/).
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,121 @@
1
+ # Redress
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/redress.svg)](http://badge.fury.io/rb/redress)
4
+ [![Code Climate](https://codeclimate.com/github/galetahub/redress/badges/gpa.svg)](https://codeclimate.com/github/galetahub/redress)
5
+ [![Build Status](https://semaphoreci.com/api/v1/igor-galeta/redress/branches/master/shields_badge.svg)](https://semaphoreci.com/igor-galeta/redress)
6
+ [![Coverage Status](https://coveralls.io/repos/github/galetahub/redress/badge.svg?branch=master)](https://coveralls.io/github/galetahub/redress?branch=master)
7
+ [![Dependency Status](https://gemnasium.com/badges/github.com/galetahub/redress.svg)](https://gemnasium.com/github.com/galetahub/redress)
8
+
9
+ ## Motivation
10
+
11
+ TODO:
12
+
13
+ <!-- Tocer[start]: Auto-generated, don't remove. -->
14
+
15
+ ## Table of Contents
16
+
17
+ - [Features](#features)
18
+ - [Screencasts](#screencasts)
19
+ - [Requirements](#requirements)
20
+ - [Setup](#setup)
21
+ - [Usage](#usage)
22
+ - [Tests](#tests)
23
+ - [Versioning](#versioning)
24
+ - [Code of Conduct](#code-of-conduct)
25
+ - [Contributions](#contributions)
26
+ - [License](#license)
27
+ - [History](#history)
28
+ - [Credits](#credits)
29
+
30
+ <!-- Tocer[finish]: Auto-generated, don't remove. -->
31
+
32
+ ## Features
33
+
34
+ TODO:
35
+
36
+ ## Requirements
37
+
38
+ 0. [Ruby 2.3](https://www.ruby-lang.org)
39
+
40
+ ## Setup
41
+
42
+ For an insecure install, type the following:
43
+
44
+ gem install redress
45
+
46
+ Add the following to your Gemfile:
47
+
48
+ gem "redress"
49
+
50
+ ## Usage
51
+
52
+ ### Forms
53
+
54
+ ``` ruby
55
+ # app/forms/application_form.rb
56
+ require 'redress/form'
57
+
58
+ class ApplicationForm < Redress::Form
59
+ end
60
+ ```
61
+
62
+ ### Commands
63
+
64
+ ``` ruby
65
+ # app/commands/application_command.rb
66
+ require 'redress/form'
67
+
68
+ class ApplicationCommand < Redress::Commad
69
+ end
70
+ ```
71
+
72
+ Or if you are using ActiveRecord:
73
+
74
+ ``` ruby
75
+ # app/commands/application_command.rb
76
+ require 'redress/form'
77
+
78
+ class ApplicationCommand < Redress::Commad
79
+ def transaction(&block)
80
+ ActiveRecord::Base.transaction(&block) if block_given?
81
+ end
82
+ end
83
+ ```
84
+
85
+ ## Tests
86
+
87
+ To test, run:
88
+
89
+ bundle exec rspec ./spec/
90
+
91
+ ## Versioning
92
+
93
+ Read [Semantic Versioning](http://semver.org) for details. Briefly, it means:
94
+
95
+ - Major (X.y.z) - Incremented for any backwards incompatible public API changes.
96
+ - Minor (x.Y.z) - Incremented for new, backwards compatible, public API enhancements/fixes.
97
+ - Patch (x.y.Z) - Incremented for small, backwards compatible, bug fixes.
98
+
99
+ ## Code of Conduct
100
+
101
+ Please note that this project is released with a [CODE OF CONDUCT](CODE_OF_CONDUCT.md). By
102
+ participating in this project you agree to abide by its terms.
103
+
104
+ ## Contributions
105
+
106
+ Read [CONTRIBUTING](CONTRIBUTING.md) for details.
107
+
108
+ ## License
109
+
110
+ Copyright (c) 2017 [Fodojo LLC](https://www.fodojo.com/).
111
+ Read [LICENSE](LICENSE.md) for details.
112
+
113
+ ## History
114
+
115
+ Read [CHANGES](CHANGES.md) for details.
116
+ Built with [Gemsmith](https://github.com/bkuhlmann/gemsmith).
117
+
118
+ ## Credits
119
+
120
+ Developed by [Igor Galeta](https://www.linkedin.com/in/igor-galeta-585a9730/) at
121
+ [Fodojo LLC](https://www.fodojo.com/).
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "wisper"
4
+
5
+ module Redress
6
+ class Command
7
+ include Wisper::Publisher
8
+
9
+ def self.call(*args, &block)
10
+ run(*args, &block)
11
+ nil
12
+ end
13
+
14
+ def self.run(*args)
15
+ command = new(*args)
16
+ yield command if block_given?
17
+ command.call
18
+ command
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fast_attributes"
4
+ require "hashie/mash"
5
+ require "active_model"
6
+
7
+ require "redress/utils/parse_attributes_from_params"
8
+ require "redress/utils/build_form_from_model"
9
+ require "redress/utils/attributes_builder"
10
+ require "redress/utils/attribute_set"
11
+ require "redress/utils/attributes_hash"
12
+
13
+ module Redress
14
+ class Form
15
+ extend FastAttributes
16
+ include ActiveModel::Validations
17
+
18
+ DEFAULT_NAME = "Form"
19
+ SPLITTER = "::"
20
+
21
+ attr_reader :context
22
+
23
+ def self.model_name
24
+ ActiveModel::Name.new(self, nil, mimicked_model_name.to_s.camelize)
25
+ end
26
+
27
+ def self.mimic(model_name)
28
+ @model_name = model_name.to_s.underscore.to_sym
29
+ end
30
+
31
+ def self.mimicked_model_name
32
+ @model_name || infer_model_name
33
+ end
34
+
35
+ def self.infer_model_name
36
+ class_name = name.split(SPLITTER).last
37
+ return :form if class_name == DEFAULT_NAME
38
+
39
+ class_name.chomp(DEFAULT_NAME).underscore.to_sym
40
+ end
41
+
42
+ def self.from_params(params, options = nil)
43
+ new(Redress::Utils::ParseAttributesFromParams.new(self, params, options).to_h)
44
+ end
45
+
46
+ def self.from_model(model)
47
+ Redress::Utils::BuildFormFromModel.new(self, model).build
48
+ end
49
+
50
+ def self.schema(options = {}, &block)
51
+ options = {
52
+ initialize: true,
53
+ attributes: true
54
+ }.merge!(options)
55
+
56
+ builder = Redress::Utils::AttributesBuilder.new(self, options)
57
+ builder.instance_eval(&block)
58
+ builder.compile!
59
+
60
+ @raw_attributes = builder.attributes
61
+ end
62
+
63
+ def self.attribute_set
64
+ @attribute_set ||= Redress::Utils::AttributeSet.new(@raw_attributes || [])
65
+ end
66
+
67
+ def to_model
68
+ self
69
+ end
70
+
71
+ def to_param
72
+ id.to_s
73
+ end
74
+
75
+ def with_context(options)
76
+ @context = Hashie::Mash.new(options)
77
+ end
78
+
79
+ def properties
80
+ @properties ||= Redress::Utils::AttributesHash.new(attributes)
81
+ end
82
+
83
+ def map_model(model)
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Redress
4
+ # Gem identity information.
5
+ module Identity
6
+ def self.name
7
+ "redress"
8
+ end
9
+
10
+ def self.label
11
+ "Redress"
12
+ end
13
+
14
+ def self.version
15
+ "0.1.0"
16
+ end
17
+
18
+ def self.version_label
19
+ "#{label} #{version}"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Redress
4
+ module Utils
5
+ class AttributeNode
6
+ attr_reader :name, :type, :options
7
+
8
+ def initialize(item)
9
+ @name = Array(item[0])[0]
10
+ @type = item[1]
11
+ @options = item[2]
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "redress/utils/attribute_node"
4
+
5
+ module Redress
6
+ module Utils
7
+ class AttributeSet
8
+ include Enumerable
9
+
10
+ def initialize(raw_attributes)
11
+ @raw_attributes = Array(raw_attributes)
12
+
13
+ @attributes = @raw_attributes.map { |item| AttributeNode.new(item) }
14
+ end
15
+
16
+ def each(&block)
17
+ @attributes.each(&block)
18
+ end
19
+
20
+ def names
21
+ @names ||= map(&:name)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fast_attributes"
4
+
5
+ module Redress
6
+ module Utils
7
+ class AttributesBuilder < FastAttributes::Builder
8
+ attr_reader :attributes
9
+
10
+ def compile!
11
+ super
12
+ self
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "hashie"
4
+
5
+ module Redress
6
+ module Utils
7
+ class AttributesHash < Hash
8
+ include Hashie::Extensions::MergeInitializer
9
+ include Hashie::Extensions::IndifferentAccess
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Redress
4
+ module Utils
5
+ class BuildFormFromModel
6
+ def initialize(form_class, model)
7
+ @form_class = form_class
8
+ @model = model
9
+ end
10
+
11
+ def build
12
+ form.tap do
13
+ matching_attributes.each do |name, _|
14
+ model_value = @model.public_send(name)
15
+ form.public_send("#{name}=", model_value)
16
+ end
17
+
18
+ form.map_model(@model)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def form
25
+ @form ||= @form_class.new
26
+ end
27
+
28
+ def matching_attributes
29
+ form.attributes.select { |name, _| @model.respond_to?(name) }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "redress/utils/attributes_hash"
4
+
5
+ module Redress
6
+ module Utils
7
+ class ParseAttributesFromParams
8
+ def initialize(klass, params, options = nil)
9
+ @klass = klass
10
+ @params = params
11
+ @options = (options || {})
12
+ end
13
+
14
+ def attributes
15
+ @attributes ||= extract_attributes
16
+ end
17
+ alias to_h attributes
18
+
19
+ def model_attributes
20
+ @params.fetch(@klass.mimicked_model_name, {})
21
+ end
22
+
23
+ def prefix
24
+ return if @options[:prefix].blank?
25
+ @prefix ||= @options[:prefix].to_s
26
+ end
27
+
28
+ def full_prefix
29
+ @full_prefix ||= "#{prefix}_"
30
+ end
31
+
32
+ protected
33
+
34
+ def extract_attributes
35
+ hash = model_attributes.merge!(prefix_attibutes)
36
+ AttributesHash.new(hash).extract!(*@klass.attribute_set.names)
37
+ end
38
+
39
+ def prefix_attibutes
40
+ return {} if prefix.nil?
41
+
42
+ hash = {}
43
+
44
+ @params.each do |key, value|
45
+ new_key = key.to_s
46
+ next unless new_key.start_with?(prefix)
47
+
48
+ new_key.slice!(full_prefix)
49
+
50
+ hash[new_key] = value
51
+ end
52
+
53
+ hash
54
+ end
55
+ end
56
+ end
57
+ end
data/lib/redress.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "redress/identity"
4
+
5
+ module Redress
6
+ end
7
+
8
+ require "redress/form"
9
+ require "redress/command"
metadata ADDED
@@ -0,0 +1,240 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redress
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Igor Galeta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: wisper
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: fast_attributes
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: hashie
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.5.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.5.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 4.2.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 4.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '12.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '12.0'
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.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.10'
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.4'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.4'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-state
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.1'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.1'
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.6'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.6'
139
+ - !ruby/object:Gem::Dependency
140
+ name: guard-rspec
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '4.7'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '4.7'
153
+ - !ruby/object:Gem::Dependency
154
+ name: reek
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '4.7'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '4.7'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rubocop
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '0.49'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '0.49'
181
+ - !ruby/object:Gem::Dependency
182
+ name: coveralls
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: 0.8.21
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: 0.8.21
195
+ description:
196
+ email:
197
+ - galeta.igor@gmail.com
198
+ executables: []
199
+ extensions: []
200
+ extra_rdoc_files:
201
+ - README.md
202
+ - LICENSE.md
203
+ files:
204
+ - LICENSE.md
205
+ - README.md
206
+ - lib/redress.rb
207
+ - lib/redress/command.rb
208
+ - lib/redress/form.rb
209
+ - lib/redress/identity.rb
210
+ - lib/redress/utils/attribute_node.rb
211
+ - lib/redress/utils/attribute_set.rb
212
+ - lib/redress/utils/attributes_builder.rb
213
+ - lib/redress/utils/attributes_hash.rb
214
+ - lib/redress/utils/build_form_from_model.rb
215
+ - lib/redress/utils/parse_attributes_from_params.rb
216
+ homepage: https://github.com/galetahub/redress
217
+ licenses:
218
+ - MIT
219
+ metadata: {}
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: '2.3'
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.6.12
237
+ signing_key:
238
+ specification_version: 4
239
+ summary: Build maintainable Ruby apps
240
+ test_files: []