redress 0.1.1 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 41402aaf89ea4b30ca985c6748259e94f43a3bf0
4
- data.tar.gz: f38303010581b1cc238927768afe86e48bbab4ba
3
+ metadata.gz: 9f35ba289da039ddbe9b65ba1b27384d6dcb25bf
4
+ data.tar.gz: 3182c116ee253aa615d3aba5e950fa6357278641
5
5
  SHA512:
6
- metadata.gz: 372b234a3fc2d5723938e4486aa1cde2876013c67b6e3d2195ed89ead3928c6504a724262cdcb81e4149dc495d1c36b632d4018483010f8ecdb8ed20d34e2518
7
- data.tar.gz: 4f51c86618c0e981532cdb4728a9910d8692a0011d77397c7ff316bc35091e09249cf362c18c5229b2c6125e7e484950b1983da220c3969b7539bbf164fc03e3
6
+ metadata.gz: 0a1ae51dd6951cda34468e64fc5f08eddb8d2a86471ee132885dd9ea04b4e626112d04245029adf952da6bb4948959d2192f795d7f334efaf5e94098851af557
7
+ data.tar.gz: 885f68704511aaa6165cf00b10e2d05cbe2c306a27c2b53178db6512b9b0597f0e22880ad16cdb29b46351f2623e80a553825229a4c2caae678451642ee14efd
data/README.md CHANGED
@@ -31,7 +31,7 @@ The command pattern is sometimes called a service object, an operation, an actio
31
31
 
32
32
  0. [Ruby 2.3](https://www.ruby-lang.org)
33
33
  1. wisper
34
- 2. fast_attributes
34
+ 2. dry-struct
35
35
  3. hashie
36
36
  4. activemodel
37
37
 
@@ -63,10 +63,12 @@ Let's define simple form:
63
63
  class SimpleForm < ApplicationForm
64
64
  mimic :user
65
65
 
66
- schema do
66
+ define_schema do
67
+ attribute :nickname, Redress::Types::Strict::String.default("superman")
67
68
  attribute :name, String
68
69
  attribute :email, String
69
70
  attribute :name_with_email, String
71
+ attribute :age, Redress::Types::Form::Int
70
72
  end
71
73
 
72
74
  validates :name, presence: true
data/lib/redress/form.rb CHANGED
@@ -1,18 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "fast_attributes"
4
3
  require "hashie/mash"
5
4
  require "active_model"
5
+ require "dry-struct"
6
6
 
7
7
  require "redress/utils/parse_attributes_from_params"
8
8
  require "redress/utils/build_form_from_model"
9
- require "redress/utils/attributes_builder"
10
- require "redress/utils/attribute_set"
11
9
  require "redress/utils/attributes_hash"
12
10
 
13
11
  module Redress
14
- class Form
15
- extend FastAttributes
12
+ class Form < Dry::Struct
16
13
  include ActiveModel::Validations
17
14
 
18
15
  DEFAULT_NAME = "Form"
@@ -20,6 +17,12 @@ module Redress
20
17
 
21
18
  attr_reader :context
22
19
 
20
+ # https://github.com/dry-rb/dry-struct/blob/master/lib/dry/struct.rb
21
+ # schema - missing keys will result in setting them using default values,
22
+ # unexpected keys will be ignored.
23
+ #
24
+ constructor_type :schema
25
+
23
26
  def self.model_name
24
27
  ActiveModel::Name.new(self, nil, mimicked_model_name.to_s.camelize)
25
28
  end
@@ -47,21 +50,19 @@ module Redress
47
50
  Redress::Utils::BuildFormFromModel.new(self, model).build
48
51
  end
49
52
 
50
- def self.schema(options = {}, &block)
51
- options = {
52
- initialize: true,
53
- attributes: true
54
- }.merge!(options)
53
+ def self.define_schema(options = nil)
54
+ options.each { |key, value| public_send(key, value) } if options
55
55
 
56
- builder = Redress::Utils::AttributesBuilder.new(self, options)
57
- builder.instance_eval(&block)
58
- builder.compile!
56
+ yield
59
57
 
60
- @raw_attributes = builder.attributes
61
- end
58
+ attribute_names.each do |name|
59
+ method_name = :"#{name}="
60
+ next if instance_methods.include?(method_name)
62
61
 
63
- def self.attribute_set
64
- @attribute_set ||= Redress::Utils::AttributeSet.new(@raw_attributes || [])
62
+ define_method(method_name) do |value|
63
+ writer_attribute(name, value)
64
+ end
65
+ end
65
66
  end
66
67
 
67
68
  def to_model
@@ -76,11 +77,21 @@ module Redress
76
77
  @context = Hashie::Mash.new(options)
77
78
  end
78
79
 
79
- def properties
80
- @properties ||= Redress::Utils::AttributesHash.new(attributes)
80
+ def attributes
81
+ @attributes ||= Redress::Utils::AttributesHash.new(to_hash)
81
82
  end
83
+ alias properties attributes
82
84
 
83
85
  def map_model(model)
84
86
  end
87
+
88
+ protected
89
+
90
+ def writer_attribute(name, value)
91
+ return unless self.class.attribute?(name)
92
+
93
+ type = self.class.schema[name]
94
+ instance_variable_set("@#{name}", type[value])
95
+ end
85
96
  end
86
97
  end
@@ -12,7 +12,7 @@ module Redress
12
12
  end
13
13
 
14
14
  def self.version
15
- "0.1.1"
15
+ "0.2.0"
16
16
  end
17
17
 
18
18
  def self.version_label
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry-types"
4
+
5
+ module Redress
6
+ module Types
7
+ include Dry::Types.module
8
+ end
9
+ end
@@ -9,24 +9,24 @@ module Redress
9
9
  end
10
10
 
11
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
12
+ form.map_model(@model)
13
+ form
20
14
  end
21
15
 
22
16
  private
23
17
 
24
18
  def form
25
- @form ||= @form_class.new
19
+ @form ||= @form_class.new(model_attributes)
20
+ end
21
+
22
+ def model_attributes
23
+ matching_attributes.each_with_object({}) do |name, hash|
24
+ hash[name] = @model.public_send(name)
25
+ end
26
26
  end
27
27
 
28
28
  def matching_attributes
29
- form.attributes.select { |name, _| @model.respond_to?(name) }
29
+ @form_class.attribute_names.select { |name| @model.respond_to?(name) }
30
30
  end
31
31
  end
32
32
  end
@@ -33,7 +33,7 @@ module Redress
33
33
 
34
34
  def extract_attributes
35
35
  hash = model_attributes.merge!(prefix_attibutes)
36
- AttributesHash.new(hash).extract!(*@klass.attribute_set.names)
36
+ AttributesHash.new(hash).extract!(*@klass.schema.keys)
37
37
  end
38
38
 
39
39
  def prefix_attibutes
data/lib/redress.rb CHANGED
@@ -5,5 +5,6 @@ require "redress/identity"
5
5
  module Redress
6
6
  end
7
7
 
8
+ require "redress/types"
8
9
  require "redress/form"
9
10
  require "redress/command"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redress
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Galeta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-05 00:00:00.000000000 Z
11
+ date: 2017-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: wisper
@@ -25,19 +25,19 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.6.1
27
27
  - !ruby/object:Gem::Dependency
28
- name: fast_attributes
28
+ name: dry-struct
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.9.0
33
+ version: 0.3.1
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 0.9.0
40
+ version: 0.3.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: hashie
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -207,9 +207,7 @@ files:
207
207
  - lib/redress/command.rb
208
208
  - lib/redress/form.rb
209
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
210
+ - lib/redress/types.rb
213
211
  - lib/redress/utils/attributes_hash.rb
214
212
  - lib/redress/utils/build_form_from_model.rb
215
213
  - lib/redress/utils/parse_attributes_from_params.rb
@@ -1,15 +0,0 @@
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
@@ -1,25 +0,0 @@
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
@@ -1,16 +0,0 @@
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