form_obj 0.4.0 → 0.5.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 +4 -4
- data/.travis.yml +0 -1
- data/Appraisals +0 -6
- data/CHANGELOG.md +32 -1
- data/Gemfile.lock +4 -3
- data/README.md +997 -361
- data/Rakefile +7 -0
- data/form_obj.gemspec +2 -1
- data/lib/form_obj/form/array.rb +22 -22
- data/lib/form_obj/form/attribute.rb +1 -9
- data/lib/form_obj/form/attributes.rb +1 -9
- data/lib/form_obj/form.rb +25 -33
- data/lib/form_obj/model_mapper/array.rb +14 -6
- data/lib/form_obj/model_mapper/attribute.rb +4 -5
- data/lib/form_obj/model_mapper/model_attribute.rb +10 -1
- data/lib/form_obj/model_mapper.rb +43 -33
- data/lib/form_obj/struct/array.rb +61 -4
- data/lib/form_obj/struct/attribute.rb +35 -9
- data/lib/form_obj/struct/attributes.rb +12 -0
- data/lib/form_obj/struct.rb +52 -2
- data/lib/form_obj/version.rb +1 -1
- data/{run_spec.sh → run_tests.sh} +1 -0
- metadata +19 -6
- data/gemfiles/4.0.gemfile +0 -9
data/lib/form_obj/struct.rb
CHANGED
@@ -2,11 +2,18 @@ require "form_obj/struct/array"
|
|
2
2
|
require "form_obj/struct/attribute"
|
3
3
|
require "form_obj/struct/attributes"
|
4
4
|
require "active_support/core_ext/class/attribute"
|
5
|
+
require "active_support/core_ext/hash/indifferent_access"
|
5
6
|
|
6
7
|
module FormObj
|
8
|
+
class UnknownAttributeError < RuntimeError; end
|
9
|
+
class WrongDefaultValueClass < RuntimeError; end
|
10
|
+
|
7
11
|
class Struct
|
8
12
|
class_attribute :_attributes, instance_predicate: false, instance_reader: false, instance_writer: false
|
13
|
+
class_attribute :primary_key, instance_predicate: false, instance_reader: false, instance_writer: false
|
14
|
+
|
9
15
|
self._attributes = Attributes.new
|
16
|
+
self.primary_key = :id
|
10
17
|
|
11
18
|
class << self
|
12
19
|
def array_class
|
@@ -52,16 +59,59 @@ module FormObj
|
|
52
59
|
end
|
53
60
|
end
|
54
61
|
|
62
|
+
def initialize(*args)
|
63
|
+
super()
|
64
|
+
update_attributes(*args) if args.size > 0 && args[0]
|
65
|
+
end
|
66
|
+
|
67
|
+
def primary_key
|
68
|
+
send(self.class.primary_key)
|
69
|
+
end
|
70
|
+
|
71
|
+
def primary_key=(val)
|
72
|
+
send("#{self.class.primary_key}=", val)
|
73
|
+
end
|
74
|
+
|
75
|
+
def update_attributes(new_attrs, raise_if_not_found: true)
|
76
|
+
new_attrs = HashWithIndifferentAccess.new(new_attrs) unless new_attrs.is_a? HashWithIndifferentAccess
|
77
|
+
new_attrs.each_pair do |new_attr, new_val|
|
78
|
+
attr = self.class._attributes.find(new_attr)
|
79
|
+
if attr.nil?
|
80
|
+
raise UnknownAttributeError.new(new_attr) if raise_if_not_found
|
81
|
+
else
|
82
|
+
if attr.subform?
|
83
|
+
self.send(new_attr).update_attributes(new_val, raise_if_not_found: raise_if_not_found)
|
84
|
+
else
|
85
|
+
update_attribute(attr, new_val)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
self
|
91
|
+
end
|
92
|
+
|
55
93
|
def to_hash
|
56
|
-
Hash[self.class._attributes.map { |attribute| [attribute.name, attribute.subform? ?
|
94
|
+
Hash[self.class._attributes.map { |attribute| [attribute.name, attribute.subform? ? read_attribute(attribute).to_hash : read_attribute(attribute)] }]
|
57
95
|
end
|
58
96
|
|
59
97
|
def inspect
|
60
|
-
"#<#{self.class.name} #{self.class._attributes.map { |attribute| "#{attribute.name}: #{
|
98
|
+
"#<#{self.class.name} #{self.class._attributes.map { |attribute| "#{attribute.name}: #{read_attribute(attribute).inspect}"}.join(', ')}>"
|
61
99
|
end
|
62
100
|
|
63
101
|
private
|
64
102
|
|
103
|
+
def update_attribute(attribute, new_value)
|
104
|
+
write_attribute(attribute, new_value)
|
105
|
+
end
|
106
|
+
|
107
|
+
def read_attribute(attribute)
|
108
|
+
send(attribute.name)
|
109
|
+
end
|
110
|
+
|
111
|
+
def write_attribute(attribute, value)
|
112
|
+
send("#{attribute.name}=", value)
|
113
|
+
end
|
114
|
+
|
65
115
|
def _get_attribute_value(attribute)
|
66
116
|
value = instance_variable_get("@#{attribute.name}")
|
67
117
|
value = instance_variable_set("@#{attribute.name}", attribute.default_value) if value.nil?
|
data/lib/form_obj/version.rb
CHANGED
@@ -22,6 +22,7 @@ do
|
|
22
22
|
echo "====================================================="
|
23
23
|
echo "$i: Start Test"
|
24
24
|
echo "====================================================="
|
25
|
+
rvm $i exec bundle exec appraisal rake test
|
25
26
|
rvm $i exec bundle exec appraisal rake spec
|
26
27
|
echo "====================================================="
|
27
28
|
echo "$i: End Test"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: form_obj
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Koltun
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-06-
|
11
|
+
date: 2018-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: typed_array
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 1.0.
|
19
|
+
version: 1.0.2
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 1.0.
|
26
|
+
version: 1.0.2
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activemodel
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: minitest
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '5.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '5.0'
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
112
|
name: actionpack
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -148,7 +162,6 @@ files:
|
|
148
162
|
- bundle_install.sh
|
149
163
|
- form_obj.gemspec
|
150
164
|
- gemfiles/3.2.gemfile
|
151
|
-
- gemfiles/4.0.gemfile
|
152
165
|
- gemfiles/4.1.gemfile
|
153
166
|
- gemfiles/4.2.gemfile
|
154
167
|
- gemfiles/5.0.gemfile
|
@@ -171,7 +184,7 @@ files:
|
|
171
184
|
- lib/form_obj/struct/attribute.rb
|
172
185
|
- lib/form_obj/struct/attributes.rb
|
173
186
|
- lib/form_obj/version.rb
|
174
|
-
-
|
187
|
+
- run_tests.sh
|
175
188
|
homepage: https://github.com/akoltun/form_obj
|
176
189
|
licenses:
|
177
190
|
- MIT
|