ciesta 0.2.5 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/Gemfile.lock +3 -3
- data/README.md +12 -4
- data/lib/ciesta.rb +9 -5
- data/lib/ciesta/class_methods.rb +59 -0
- data/lib/ciesta/instance_methods.rb +88 -0
- data/lib/ciesta/version.rb +1 -1
- metadata +4 -3
- data/lib/ciesta/form.rb +0 -130
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5eaf463aca332576deca118786495fe23b4c13c231e4b987061dd7b150ea4364
|
4
|
+
data.tar.gz: ff94edfb892a18df09df01b0bce6706557475d69b0ad07f2d11b618bbda03fc2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dec6837b70b3d3169620e121290e5bcbe61f65ed94dbd461233daa98f66bd48b53e42143481fb9cebece32b21483a87391553b52b676e0b8cc0fbd1d8b8419f2
|
7
|
+
data.tar.gz: 6d74105fe737dd8b202c3d995c5dbd0f43d5bca054dd2778baab3bcfc2c16844b4d26f7653719047a42493213b64a2cde9cbc6312464d798da3728ce993107ff
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ciesta (0.
|
4
|
+
ciesta (0.3.1)
|
5
5
|
dry-types (~> 0.12.1)
|
6
6
|
dry-validation (~> 0.11.1)
|
7
7
|
|
@@ -24,9 +24,9 @@ GEM
|
|
24
24
|
dry-container (0.6.0)
|
25
25
|
concurrent-ruby (~> 1.0)
|
26
26
|
dry-configurable (~> 0.1, >= 0.1.3)
|
27
|
-
dry-core (0.4.
|
27
|
+
dry-core (0.4.6)
|
28
28
|
concurrent-ruby (~> 1.0)
|
29
|
-
dry-equalizer (0.2.
|
29
|
+
dry-equalizer (0.2.1)
|
30
30
|
dry-logic (0.4.2)
|
31
31
|
dry-container (~> 0.2, >= 0.2.6)
|
32
32
|
dry-core (~> 0.2)
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Ciesta
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/nulldef/ciesta.svg?branch=master)](https://travis-ci.org/nulldef/ciesta)
|
4
|
-
[![Coverage Status](https://coveralls.io/repos/github/nulldef/ciesta/badge.svg?branch=master&rand=
|
4
|
+
[![Coverage Status](https://coveralls.io/repos/github/nulldef/ciesta/badge.svg?branch=master&rand=23)](https://coveralls.io/github/nulldef/ciesta?branch=master)
|
5
5
|
[![Gem Version](https://badge.fury.io/rb/ciesta.svg)](https://badge.fury.io/rb/ciesta)
|
6
6
|
|
7
7
|
Create simple form objects
|
@@ -54,9 +54,15 @@ user = User.new(nil, nil)
|
|
54
54
|
For setting and syncing new values let's create a form object:
|
55
55
|
|
56
56
|
```ruby
|
57
|
-
class Form
|
57
|
+
class Form
|
58
|
+
include Ciesta
|
59
|
+
|
58
60
|
field :name
|
59
61
|
field :age
|
62
|
+
|
63
|
+
def age
|
64
|
+
super.to_i
|
65
|
+
end
|
60
66
|
end
|
61
67
|
|
62
68
|
form = Form.new(user)
|
@@ -64,7 +70,7 @@ form = Form.new(user)
|
|
64
70
|
|
65
71
|
```ruby
|
66
72
|
form.name = "John"
|
67
|
-
form.age = 33
|
73
|
+
form.age = "33"
|
68
74
|
form.sync!
|
69
75
|
|
70
76
|
user.name # => "John"
|
@@ -85,7 +91,9 @@ Both `sync` and `sync!` provide this DSL.
|
|
85
91
|
For validating incoming values you can use `validate` method:
|
86
92
|
|
87
93
|
```ruby
|
88
|
-
class Form
|
94
|
+
class Form
|
95
|
+
include Ciesta
|
96
|
+
|
89
97
|
field :name
|
90
98
|
field :age
|
91
99
|
|
data/lib/ciesta.rb
CHANGED
@@ -2,10 +2,6 @@
|
|
2
2
|
|
3
3
|
require "dry-types"
|
4
4
|
require "dry-validation"
|
5
|
-
|
6
|
-
module Ciesta
|
7
|
-
end
|
8
|
-
|
9
5
|
require "ciesta/delegator"
|
10
6
|
require "ciesta/version"
|
11
7
|
require "ciesta/field_list"
|
@@ -14,4 +10,12 @@ require "ciesta/types"
|
|
14
10
|
require "ciesta/validator"
|
15
11
|
require "ciesta/errors"
|
16
12
|
require "ciesta/field"
|
17
|
-
require "ciesta/
|
13
|
+
require "ciesta/class_methods"
|
14
|
+
require "ciesta/instance_methods"
|
15
|
+
|
16
|
+
module Ciesta
|
17
|
+
def self.included(base)
|
18
|
+
base.extend(ClassMethods)
|
19
|
+
base.include(InstanceMethods)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Ciesta
|
2
|
+
module ClassMethods
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def form_from(**hash)
|
6
|
+
form = new
|
7
|
+
hash.each do |(k, v)|
|
8
|
+
form.public_send("#{k}=", v) if form.respond_to?("#{k}=")
|
9
|
+
end
|
10
|
+
form
|
11
|
+
end
|
12
|
+
|
13
|
+
# Declare new form field
|
14
|
+
#
|
15
|
+
# @param [Symbol] name Field name
|
16
|
+
# @param [Hash] options Options
|
17
|
+
# @option (see Ciesta::Field)
|
18
|
+
def field(name, **options)
|
19
|
+
name = name.to_sym
|
20
|
+
fields << Ciesta::Field.new(name, options)
|
21
|
+
proxy.instance_eval do
|
22
|
+
define_method(name) { self.class.fields[name] }
|
23
|
+
define_method("#{name}=") { |value| self.class.fields[name] = value }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Declare rules for valudation
|
28
|
+
#
|
29
|
+
# @param [Block] block Block with validation rules
|
30
|
+
# @see http://dry-rb.org/gems/dry-validation
|
31
|
+
def validate(&block)
|
32
|
+
validator.use(&block)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Returns field list
|
36
|
+
#
|
37
|
+
# @api private
|
38
|
+
# @return [Ciesta::FieldList]
|
39
|
+
def fields
|
40
|
+
@fields ||= Ciesta::FieldList.new
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns form validator
|
44
|
+
#
|
45
|
+
# @api private
|
46
|
+
# @return [Ciesta::Validator]
|
47
|
+
def validator
|
48
|
+
@validator ||= Ciesta::Validator.new
|
49
|
+
end
|
50
|
+
|
51
|
+
def proxy
|
52
|
+
@proxy ||= begin
|
53
|
+
m = Module.new
|
54
|
+
include m
|
55
|
+
m
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module Ciesta
|
2
|
+
module InstanceMethods
|
3
|
+
extend Ciesta::Delegator
|
4
|
+
|
5
|
+
# @!method assign
|
6
|
+
# @!method assign!
|
7
|
+
# @!method attributes
|
8
|
+
# @see Ciesta::FieldList
|
9
|
+
delegate :assign, :assign!, :attributes, :clear!, to: :fields
|
10
|
+
|
11
|
+
# @!method errors
|
12
|
+
# @see Ciesta::Validator
|
13
|
+
delegate :errors, to: :validator
|
14
|
+
|
15
|
+
# @!method sync
|
16
|
+
# @!method sync!
|
17
|
+
# @see Ciesta::Syncer
|
18
|
+
delegate :sync, to: :syncer
|
19
|
+
|
20
|
+
attr_accessor :object
|
21
|
+
|
22
|
+
# Constructor
|
23
|
+
#
|
24
|
+
# @param [Object] object Object wich will be updated though this form
|
25
|
+
def initialize(object = nil)
|
26
|
+
@object = object
|
27
|
+
clear!
|
28
|
+
|
29
|
+
return if object.nil?
|
30
|
+
|
31
|
+
values = fields.keys.each_with_object({}) do |key, mem|
|
32
|
+
mem[key] = object.public_send(key) if object.respond_to?(key)
|
33
|
+
end
|
34
|
+
|
35
|
+
assign(values)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Checks if form is valid
|
39
|
+
#
|
40
|
+
# @param [Hash] params Attrubutes to assign before validation
|
41
|
+
#
|
42
|
+
# @return [Boolean]
|
43
|
+
def valid?(params = nil)
|
44
|
+
assign(params) if params
|
45
|
+
validator.valid?(attributes)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Sync form attributes to object
|
49
|
+
#
|
50
|
+
# @see Ciesta::Syncer
|
51
|
+
#
|
52
|
+
# @param [Block] block Block wich will be yielded after synfing
|
53
|
+
#
|
54
|
+
# @raise Ciesta::ModelNotPresent
|
55
|
+
# @raise Ciesta::FormNotValid
|
56
|
+
# @return [Boolean]
|
57
|
+
def sync!(&block)
|
58
|
+
raise Ciesta::FormNotValid, "Form is not valid" unless valid?
|
59
|
+
syncer.sync!(&block)
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
# Sync class for form
|
65
|
+
#
|
66
|
+
# @api private
|
67
|
+
# @return [Ciesta::Syncer]
|
68
|
+
def syncer
|
69
|
+
@syncer ||= Ciesta::Syncer.new(object, fields)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Returns form validator
|
73
|
+
#
|
74
|
+
# @api private
|
75
|
+
# @see Ciesta::Form.validator
|
76
|
+
def validator
|
77
|
+
self.class.validator
|
78
|
+
end
|
79
|
+
|
80
|
+
# Returns field list
|
81
|
+
#
|
82
|
+
# @api private
|
83
|
+
# @see Ciesta::Form.fields
|
84
|
+
def fields
|
85
|
+
self.class.fields
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/lib/ciesta/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ciesta
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexey
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-06-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-types
|
@@ -144,11 +144,12 @@ files:
|
|
144
144
|
- bin/setup
|
145
145
|
- ciesta.gemspec
|
146
146
|
- lib/ciesta.rb
|
147
|
+
- lib/ciesta/class_methods.rb
|
147
148
|
- lib/ciesta/delegator.rb
|
148
149
|
- lib/ciesta/errors.rb
|
149
150
|
- lib/ciesta/field.rb
|
150
151
|
- lib/ciesta/field_list.rb
|
151
|
-
- lib/ciesta/
|
152
|
+
- lib/ciesta/instance_methods.rb
|
152
153
|
- lib/ciesta/syncer.rb
|
153
154
|
- lib/ciesta/types.rb
|
154
155
|
- lib/ciesta/validator.rb
|
data/lib/ciesta/form.rb
DELETED
@@ -1,130 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Main form class
|
4
|
-
#
|
5
|
-
# @attr_reader [Object] object Object of form
|
6
|
-
class Ciesta::Form
|
7
|
-
extend Ciesta::Delegator
|
8
|
-
|
9
|
-
# @!method assign
|
10
|
-
# @!method assign!
|
11
|
-
# @!method attributes
|
12
|
-
# @see Ciesta::FieldList
|
13
|
-
delegate :assign, :assign!, :attributes, :clear!, to: :fields
|
14
|
-
|
15
|
-
# @!method errors
|
16
|
-
# @see Ciesta::Validator
|
17
|
-
delegate :errors, to: :validator
|
18
|
-
|
19
|
-
# @!method sync
|
20
|
-
# @!method sync!
|
21
|
-
# @see Ciesta::Syncer
|
22
|
-
delegate :sync, to: :syncer
|
23
|
-
|
24
|
-
class << self
|
25
|
-
# Declare new form field
|
26
|
-
#
|
27
|
-
# @param [Symbol] name Field name
|
28
|
-
# @param [Hash] options Options
|
29
|
-
# @option (see Ciesta::Field)
|
30
|
-
def field(name, options = {})
|
31
|
-
name = name.to_sym
|
32
|
-
fields << Ciesta::Field.new(name, options)
|
33
|
-
|
34
|
-
define_method(name) { fields[name] }
|
35
|
-
define_method("#{name}=") { |value| fields[name] = value }
|
36
|
-
end
|
37
|
-
|
38
|
-
# Declare rules for valudation
|
39
|
-
#
|
40
|
-
# @param [Block] block Block with validation rules
|
41
|
-
# @see http://dry-rb.org/gems/dry-validation
|
42
|
-
def validate(&block)
|
43
|
-
validator.use(&block)
|
44
|
-
end
|
45
|
-
|
46
|
-
# Returns field list
|
47
|
-
#
|
48
|
-
# @api private
|
49
|
-
# @return [Ciesta::FieldList]
|
50
|
-
def fields
|
51
|
-
@fields ||= Ciesta::FieldList.new
|
52
|
-
end
|
53
|
-
|
54
|
-
# Returns form validator
|
55
|
-
#
|
56
|
-
# @api private
|
57
|
-
# @return [Ciesta::Validator]
|
58
|
-
def validator
|
59
|
-
@validator ||= Ciesta::Validator.new
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
attr_accessor :object
|
64
|
-
|
65
|
-
# Constructor
|
66
|
-
#
|
67
|
-
# @param [Object] object Object wich will be updated though this form
|
68
|
-
def initialize(object = nil)
|
69
|
-
@object = object
|
70
|
-
clear!
|
71
|
-
|
72
|
-
return if object.nil?
|
73
|
-
|
74
|
-
values = fields.keys.each_with_object({}) do |key, mem|
|
75
|
-
mem[key] = object.public_send(key) if object.respond_to?(key)
|
76
|
-
end
|
77
|
-
|
78
|
-
assign(values)
|
79
|
-
end
|
80
|
-
|
81
|
-
# Checks if form is valid
|
82
|
-
#
|
83
|
-
# @param [Hash] params Attrubutes to assign before validation
|
84
|
-
#
|
85
|
-
# @return [Boolean]
|
86
|
-
def valid?(params = nil)
|
87
|
-
assign(params) if params
|
88
|
-
validator.valid?(attributes)
|
89
|
-
end
|
90
|
-
|
91
|
-
# Sync form attributes to object
|
92
|
-
#
|
93
|
-
# @see Ciesta::Syncer
|
94
|
-
#
|
95
|
-
# @param [Block] block Block wich will be yielded after synfing
|
96
|
-
#
|
97
|
-
# @raise Ciesta::ModelNotPresent
|
98
|
-
# @raise Ciesta::FormNotValid
|
99
|
-
# @return [Boolean]
|
100
|
-
def sync!(&block)
|
101
|
-
raise Ciesta::FormNotValid, "Form is not valid" unless valid?
|
102
|
-
syncer.sync!(&block)
|
103
|
-
end
|
104
|
-
|
105
|
-
private
|
106
|
-
|
107
|
-
# Sync class for form
|
108
|
-
#
|
109
|
-
# @api private
|
110
|
-
# @return [Ciesta::Syncer]
|
111
|
-
def syncer
|
112
|
-
@syncer ||= Ciesta::Syncer.new(object, fields)
|
113
|
-
end
|
114
|
-
|
115
|
-
# Returns form validator
|
116
|
-
#
|
117
|
-
# @api private
|
118
|
-
# @see Ciesta::Form.validator
|
119
|
-
def validator
|
120
|
-
self.class.validator
|
121
|
-
end
|
122
|
-
|
123
|
-
# Returns field list
|
124
|
-
#
|
125
|
-
# @api private
|
126
|
-
# @see Ciesta::Form.fields
|
127
|
-
def fields
|
128
|
-
self.class.fields
|
129
|
-
end
|
130
|
-
end
|