form_core 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/form_core/coder.rb +32 -0
- data/lib/form_core/coders/yaml_coder.rb +26 -0
- data/lib/form_core/errors.rb +10 -0
- data/lib/form_core/version.rb +1 -1
- data/lib/form_core/virtual_model.rb +18 -1
- data/lib/form_core.rb +18 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 819b17a80a69cb0746675b706757485d0cc686e8
|
4
|
+
data.tar.gz: 046e9e86b856786834df1093ffc7cf572e660520
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62edafb741f3196caa8a69c5d344112d4a5f7695e4955f722f7e9e1f7134a035984aa5fae0dbd8731ab8bc52f64fff3443c8d4aaf7e004e3bae4373ce93983a7
|
7
|
+
data.tar.gz: 167d4bf390c740232cf4e88a9e9c5e07828e94770c8affd995964e1967c2847212abe1436abeb7ab178e3c8a08764e52d877c80de65c0dab92e51fd762d91346
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module FormCore
|
2
|
+
class Coder
|
3
|
+
attr_reader :object_class
|
4
|
+
|
5
|
+
def initialize(object_class, strict: false)
|
6
|
+
@object_class = object_class
|
7
|
+
@strict = strict
|
8
|
+
end
|
9
|
+
|
10
|
+
def strict?
|
11
|
+
@strict
|
12
|
+
end
|
13
|
+
|
14
|
+
def dump(_obj)
|
15
|
+
raise NotImplementedError
|
16
|
+
end
|
17
|
+
|
18
|
+
def load(_src)
|
19
|
+
raise NotImplementedError
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def new_or_raise_decoding_error
|
25
|
+
if strict?
|
26
|
+
raise DecodingDataCorrupted
|
27
|
+
else
|
28
|
+
object_class.new
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "yaml"
|
2
|
+
|
3
|
+
module FormCore
|
4
|
+
class YAMLCoder < FormCore::Coder # :nodoc:
|
5
|
+
def dump(obj)
|
6
|
+
return YAML.dump({}) unless obj
|
7
|
+
|
8
|
+
YAML.dump obj.to_h
|
9
|
+
end
|
10
|
+
|
11
|
+
def load(yaml)
|
12
|
+
return object_class.new if yaml.blank?
|
13
|
+
|
14
|
+
unless yaml.is_a?(String) && /^---/.match?(yaml)
|
15
|
+
return new_or_raise_decoding_error
|
16
|
+
end
|
17
|
+
|
18
|
+
decoded = YAML.load(yaml)
|
19
|
+
unless decoded.is_a? Hash
|
20
|
+
return new_or_raise_decoding_error
|
21
|
+
end
|
22
|
+
|
23
|
+
object_class.new decoded
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/form_core/version.rb
CHANGED
@@ -2,7 +2,6 @@ require "duck_record"
|
|
2
2
|
|
3
3
|
module FormCore
|
4
4
|
class VirtualModel < ::DuckRecord::Base
|
5
|
-
|
6
5
|
# Returns the contents of the record as a nicely formatted string.
|
7
6
|
def inspect
|
8
7
|
# We check defined?(@attributes) not to issue warnings if the object is
|
@@ -21,6 +20,10 @@ module FormCore
|
|
21
20
|
"#<VirtualModel:#{self.class.name}:#{object_id} #{inspection}>"
|
22
21
|
end
|
23
22
|
|
23
|
+
def dump
|
24
|
+
self.class.dump(self)
|
25
|
+
end
|
26
|
+
|
24
27
|
public_class_method :define_method
|
25
28
|
class << self
|
26
29
|
def name
|
@@ -34,6 +37,20 @@ module FormCore
|
|
34
37
|
@_name = value
|
35
38
|
end
|
36
39
|
|
40
|
+
def coder
|
41
|
+
@_coder ||= FormCore.virtual_model_coder_class.new(self)
|
42
|
+
end
|
43
|
+
|
44
|
+
def coder=(klass)
|
45
|
+
unless klass && klass < Coder
|
46
|
+
raise ArgumentError, "#{klass} should be sub-class of #{Coder}."
|
47
|
+
end
|
48
|
+
|
49
|
+
@_coder = klass.new(self)
|
50
|
+
end
|
51
|
+
|
52
|
+
delegate :dump, :load, to: :coder, allow_nil: false
|
53
|
+
|
37
54
|
def build(name = nil)
|
38
55
|
klass = Class.new(self)
|
39
56
|
klass.name = name
|
data/lib/form_core.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
require "form_core/engine"
|
2
|
-
require "form_core/
|
2
|
+
require "form_core/errors"
|
3
|
+
|
4
|
+
require "form_core/coder"
|
5
|
+
require "form_core/coders/yaml_coder"
|
3
6
|
|
7
|
+
require "form_core/virtual_model"
|
4
8
|
require "form_core/concerns/models/form"
|
5
9
|
require "form_core/concerns/models/field"
|
6
10
|
|
@@ -15,6 +19,7 @@ module FormCore
|
|
15
19
|
raise ArgumentError, "#{klass} should be sub-class of #{VirtualModel}."
|
16
20
|
end
|
17
21
|
|
22
|
+
@reserved_names = nil
|
18
23
|
@virtual_model_class = klass
|
19
24
|
end
|
20
25
|
|
@@ -24,5 +29,17 @@ module FormCore
|
|
24
29
|
virtual_model_class.instance_methods(true)
|
25
30
|
)
|
26
31
|
end
|
32
|
+
|
33
|
+
def virtual_model_coder_class
|
34
|
+
@virtual_model_coder_class ||= YAMLCoder
|
35
|
+
end
|
36
|
+
|
37
|
+
def virtual_model_coder_class=(klass)
|
38
|
+
unless klass && klass < Coder
|
39
|
+
raise ArgumentError, "#{klass} should be sub-class of #{Coder}."
|
40
|
+
end
|
41
|
+
|
42
|
+
@virtual_model_coder_class = klass
|
43
|
+
end
|
27
44
|
end
|
28
45
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: form_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jasl
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -56,9 +56,12 @@ files:
|
|
56
56
|
- db/migrate/20170430190404_create_forms.rb
|
57
57
|
- db/migrate/20170430191336_create_fields.rb
|
58
58
|
- lib/form_core.rb
|
59
|
+
- lib/form_core/coder.rb
|
60
|
+
- lib/form_core/coders/yaml_coder.rb
|
59
61
|
- lib/form_core/concerns/models/field.rb
|
60
62
|
- lib/form_core/concerns/models/form.rb
|
61
63
|
- lib/form_core/engine.rb
|
64
|
+
- lib/form_core/errors.rb
|
62
65
|
- lib/form_core/version.rb
|
63
66
|
- lib/form_core/virtual_model.rb
|
64
67
|
- lib/tasks/form_core_tasks.rake
|