formwandler 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/formwandler/field.rb +23 -0
- data/lib/formwandler/field_definition.rb +18 -0
- data/lib/formwandler/field_value_transformer.rb +19 -0
- data/lib/formwandler/form.rb +8 -11
- data/lib/formwandler/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66c573ed311eeaed84bd8a92801b60d04d2fc550
|
4
|
+
data.tar.gz: d8c0f269f19e42d4a31ccbecfdbb69db6c04ba89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1f2fa32f9e1248e50bf8bd4dc3d6dd17aacf85650832cc93beac1cca3172b6462ea1d3b87e71a8d0a4fb476b911cc91bc30b296bf5bfdf4762266752071ed3f
|
7
|
+
data.tar.gz: 4c6b3d9bd15d0cde087c295a9996ccd3e6a314bd5f6e98a447a0a19f354ae0a390b1b8de0d2a8d6dff8ffec5e899d3f06ec1be55362c459d27ef7896ba78d738
|
data/lib/formwandler/field.rb
CHANGED
@@ -11,6 +11,20 @@ module Formwandler
|
|
11
11
|
@field_definition = field_definition
|
12
12
|
end
|
13
13
|
|
14
|
+
def value=(new_value)
|
15
|
+
new_value = transform_value(new_value, field_definition.in_transformation)
|
16
|
+
if model_instance
|
17
|
+
model_instance.public_send("#{field_definition.source}=", new_value)
|
18
|
+
else
|
19
|
+
@value = new_value
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def value
|
24
|
+
_value = (model_instance ? model_instance.public_send(field_definition.source) : @value)
|
25
|
+
transform_value(_value, field_definition.out_transformation)
|
26
|
+
end
|
27
|
+
|
14
28
|
def hidden?
|
15
29
|
_evaluate_field_definition_value(:hidden)
|
16
30
|
end
|
@@ -41,6 +55,15 @@ module Formwandler
|
|
41
55
|
|
42
56
|
private
|
43
57
|
|
58
|
+
def transform_value(value, transformation)
|
59
|
+
return if value.nil?
|
60
|
+
if transformation.respond_to?(:call)
|
61
|
+
form.instance_exec(value, &transformation)
|
62
|
+
else
|
63
|
+
value
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
44
67
|
def all_options
|
45
68
|
_evaluate_field_definition_value(:options) || options_from_model || []
|
46
69
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'field_value_transformer'
|
4
|
+
|
3
5
|
module Formwandler
|
4
6
|
class FieldDefinition
|
5
7
|
attr_reader :name, :hidden, :disabled, :default, :delocalize, :model, :source, :options
|
@@ -22,5 +24,21 @@ module Formwandler
|
|
22
24
|
def hidden_options
|
23
25
|
@hidden_options ||= {}
|
24
26
|
end
|
27
|
+
|
28
|
+
def transform(&block)
|
29
|
+
@value_transformer = FieldValueTransformer.new(&block)
|
30
|
+
end
|
31
|
+
|
32
|
+
def in_transformation
|
33
|
+
value_transformer&.in_transformation
|
34
|
+
end
|
35
|
+
|
36
|
+
def out_transformation
|
37
|
+
value_transformer&.out_transformation
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
attr_reader :value_transformer
|
25
43
|
end
|
26
44
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Formwandler
|
4
|
+
class FieldValueTransformer
|
5
|
+
attr_reader :in_transformation, :out_transformation
|
6
|
+
|
7
|
+
def initialize(&block)
|
8
|
+
instance_exec(&block)
|
9
|
+
end
|
10
|
+
|
11
|
+
def incoming(&block)
|
12
|
+
@in_transformation = block
|
13
|
+
end
|
14
|
+
|
15
|
+
def outgoing(&block)
|
16
|
+
@out_transformation = block
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/formwandler/form.rb
CHANGED
@@ -17,25 +17,22 @@ module Formwandler
|
|
17
17
|
@field_definitions ||= {}
|
18
18
|
end
|
19
19
|
|
20
|
-
def field(name, opts = {})
|
21
|
-
|
20
|
+
def field(name, opts = {}, &block)
|
21
|
+
field_definition = FieldDefinition.new(name, opts)
|
22
|
+
field_definitions[name] = field_definition
|
22
23
|
|
23
|
-
|
24
|
+
field_definition.instance_exec(&block) if block_given?
|
24
25
|
|
25
|
-
|
26
|
-
attribute_accessor(name, field_definitions[name])
|
27
|
-
else
|
28
|
-
attr_accessor name
|
29
|
-
end
|
26
|
+
attribute_accessor(name)
|
30
27
|
end
|
31
28
|
|
32
|
-
def attribute_accessor(name
|
29
|
+
def attribute_accessor(name)
|
33
30
|
define_method "#{name}=" do |value|
|
34
|
-
|
31
|
+
field(name).value = value
|
35
32
|
end
|
36
33
|
|
37
34
|
define_method name do
|
38
|
-
|
35
|
+
field(name).value
|
39
36
|
end
|
40
37
|
end
|
41
38
|
|
data/lib/formwandler/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: formwandler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Bilharz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-09-
|
11
|
+
date: 2017-09-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -118,6 +118,7 @@ files:
|
|
118
118
|
- lib/formwandler.rb
|
119
119
|
- lib/formwandler/field.rb
|
120
120
|
- lib/formwandler/field_definition.rb
|
121
|
+
- lib/formwandler/field_value_transformer.rb
|
121
122
|
- lib/formwandler/form.rb
|
122
123
|
- lib/formwandler/form_loader.rb
|
123
124
|
- lib/formwandler/railtie.rb
|