objective_form 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
data/lib/objective_form/base.rb
CHANGED
@@ -1,104 +1,106 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
attributes
|
9
|
-
|
10
|
-
|
1
|
+
module ObjectiveForm
|
2
|
+
class Base
|
3
|
+
extend ActiveModel::Naming
|
4
|
+
include ActiveModel::Conversion
|
5
|
+
include ActiveModel::Validations
|
6
|
+
class_attribute :pseudo_relations, :attributes
|
7
|
+
|
8
|
+
def initialize(attributes = {})
|
9
|
+
attributes.each do |name, value|
|
10
|
+
send("#{name}=", value)
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
13
|
+
pseudo_relations.each do |rel|
|
14
|
+
instance_variable_set("@#{rel.name}", []) unless send(rel.name)
|
15
|
+
end
|
14
16
|
end
|
15
|
-
end
|
16
17
|
|
17
|
-
|
18
|
-
false
|
19
|
-
end
|
20
|
-
|
21
|
-
def save
|
22
|
-
if valid?
|
23
|
-
persist!
|
24
|
-
true
|
25
|
-
else
|
18
|
+
def persisted?
|
26
19
|
false
|
27
20
|
end
|
28
|
-
end
|
29
21
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
return value.to_s if klass_or_proc == String
|
38
|
-
|
39
|
-
if klass_or_proc == Array
|
40
|
-
return [] if value.blank?
|
41
|
-
return Array.wrap(value)
|
22
|
+
def save
|
23
|
+
if valid?
|
24
|
+
persist!
|
25
|
+
true
|
26
|
+
else
|
27
|
+
false
|
28
|
+
end
|
42
29
|
end
|
43
30
|
|
44
|
-
|
45
|
-
|
46
|
-
elsif klass_or_proc.respond_to?(:call)
|
47
|
-
klass_or_proc.call(value)
|
48
|
-
else
|
49
|
-
value
|
31
|
+
def persist!
|
32
|
+
raise NotImplementedError
|
50
33
|
end
|
51
|
-
end
|
52
34
|
|
53
|
-
|
54
|
-
public
|
35
|
+
private
|
55
36
|
|
56
|
-
def
|
57
|
-
|
58
|
-
define_relation_accessors(name, pseudo_record)
|
59
|
-
end
|
37
|
+
def initialize_attribute_value(klass_or_proc, value)
|
38
|
+
return value.to_s if klass_or_proc == String
|
60
39
|
|
61
|
-
|
62
|
-
|
63
|
-
|
40
|
+
if klass_or_proc == Array
|
41
|
+
return [] if value.blank?
|
42
|
+
return Array.wrap(value)
|
43
|
+
end
|
44
|
+
|
45
|
+
if klass_or_proc.respond_to?(:new)
|
46
|
+
klass_or_proc.new(value)
|
47
|
+
elsif klass_or_proc.respond_to?(:call)
|
48
|
+
klass_or_proc.call(value)
|
49
|
+
else
|
50
|
+
value
|
51
|
+
end
|
64
52
|
end
|
65
53
|
|
66
|
-
|
54
|
+
class << self
|
55
|
+
public
|
67
56
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
57
|
+
def has_many(name, pseudo_record)
|
58
|
+
store_relation(name, pseudo_record)
|
59
|
+
define_relation_accessors(name, pseudo_record)
|
60
|
+
end
|
72
61
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
62
|
+
def attribute(name, klass_or_proc = String)
|
63
|
+
store_attribute(name, klass_or_proc)
|
64
|
+
define_attribute_accessors(name, klass_or_proc)
|
65
|
+
end
|
77
66
|
|
78
|
-
|
79
|
-
attr_reader name
|
67
|
+
private
|
80
68
|
|
81
|
-
|
82
|
-
|
69
|
+
def store_attribute(name, klass_or_proc)
|
70
|
+
self.attributes ||= []
|
71
|
+
self.attributes += Array.wrap(ObjectiveForm::Attribute.new(name, klass_or_proc))
|
72
|
+
end
|
73
|
+
|
74
|
+
def store_relation(name, pseudo_record)
|
75
|
+
self.pseudo_relations ||= []
|
76
|
+
self.pseudo_relations += Array.wrap(ObjectiveForm::PseudoRelation.new(name, pseudo_record))
|
83
77
|
end
|
84
|
-
end
|
85
78
|
|
86
|
-
|
87
|
-
|
79
|
+
def define_attribute_accessors(name, klass_or_proc)
|
80
|
+
attr_reader name
|
88
81
|
|
89
|
-
|
90
|
-
|
91
|
-
pseudo_record_instance = pseudo_record.new(value)
|
92
|
-
instance_variable_set("@#{name}", instance_variable_get("@#{name}") + [pseudo_record_instance])
|
82
|
+
define_method("#{name}=") do |value|
|
83
|
+
instance_variable_set("@#{name}", initialize_attribute_value(klass_or_proc, value))
|
93
84
|
end
|
94
85
|
end
|
95
86
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
87
|
+
def define_relation_accessors(name, pseudo_record)
|
88
|
+
attr_reader name
|
89
|
+
|
90
|
+
define_method("#{name}=") do |values|
|
91
|
+
Array.wrap(values).each do |value|
|
92
|
+
pseudo_record_instance = pseudo_record.new(value)
|
93
|
+
instance_variable_set("@#{name}", instance_variable_get("@#{name}") + [pseudo_record_instance])
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
define_method("#{name}_attributes=") do |values|
|
98
|
+
instance_variable_set("@#{name}", []) unless instance_variable_defined?("@#{name}")
|
99
|
+
values.each do |_, attributes|
|
100
|
+
next if attributes['_destroy'] and ::ActiveRecord::ConnectionAdapters::Column.value_to_boolean(attributes['_destroy'])
|
101
|
+
pseudo_record_instance = pseudo_record.new(*attributes.except('_destroy').values)
|
102
|
+
instance_variable_set("@#{name}", instance_variable_get("@#{name}") + [pseudo_record_instance])
|
103
|
+
end
|
102
104
|
end
|
103
105
|
end
|
104
106
|
end
|
@@ -1,19 +1,21 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module ObjectiveForm
|
2
|
+
class PseudoRecord < Struct
|
3
|
+
class_attribute :_fields
|
4
|
+
attr_writer :_destroy
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
def self.new(*args)
|
7
|
+
fields = args.extract_options!
|
8
|
+
super(*fields.keys).tap do |klass|
|
9
|
+
klass._fields = fields
|
10
|
+
end
|
9
11
|
end
|
10
|
-
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def persisted?
|
14
|
+
false
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
-
|
17
|
+
def _destroy
|
18
|
+
false
|
19
|
+
end
|
18
20
|
end
|
19
21
|
end
|