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.
@@ -1,2 +1,4 @@
1
- class ObjectiveForm::Attribute < Struct.new(:name, :klass_or_proc)
1
+ module ObjectiveForm
2
+ class Attribute < Struct.new(:name, :klass_or_proc)
3
+ end
2
4
  end
@@ -1,104 +1,106 @@
1
- class ObjectiveForm::Base
2
- extend ActiveModel::Naming
3
- include ActiveModel::Conversion
4
- include ActiveModel::Validations
5
- class_attribute :pseudo_relations, :attributes
6
-
7
- def initialize(attributes = {})
8
- attributes.each do |name, value|
9
- send("#{name}=", value)
10
- end
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
- pseudo_relations.each do |rel|
13
- instance_variable_set("@#{rel.name}", []) unless send(rel.name)
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
- def persisted?
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
- def persist!
31
- raise NotImplementedError
32
- end
33
-
34
- private
35
-
36
- def initialize_attribute_value(klass_or_proc, value)
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
- if klass_or_proc.respond_to?(:new)
45
- klass_or_proc.new(value)
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
- class << self
54
- public
35
+ private
55
36
 
56
- def has_many(name, pseudo_record)
57
- store_relation(name, pseudo_record)
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
- def attribute(name, klass_or_proc = String)
62
- store_attribute(name, klass_or_proc)
63
- define_attribute_accessors(name, klass_or_proc)
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
- private
54
+ class << self
55
+ public
67
56
 
68
- def store_attribute(name, klass_or_proc)
69
- self.attributes ||= []
70
- self.attributes += Array.wrap(ObjectiveForm::Attribute.new(name, klass_or_proc))
71
- end
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
- def store_relation(name, pseudo_record)
74
- self.pseudo_relations ||= []
75
- self.pseudo_relations += Array.wrap(ObjectiveForm::PseudoRelation.new(name, pseudo_record))
76
- end
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
- def define_attribute_accessors(name, klass_or_proc)
79
- attr_reader name
67
+ private
80
68
 
81
- define_method("#{name}=") do |value|
82
- instance_variable_set("@#{name}", initialize_attribute_value(klass_or_proc, value))
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
- def define_relation_accessors(name, pseudo_record)
87
- attr_reader name
79
+ def define_attribute_accessors(name, klass_or_proc)
80
+ attr_reader name
88
81
 
89
- define_method("#{name}=") do |values|
90
- Array.wrap(values).each do |value|
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
- define_method("#{name}_attributes=") do |values|
97
- instance_variable_set("@#{name}", []) unless instance_variable_defined?("@#{name}")
98
- values.each do |_, attributes|
99
- next if attributes['_destroy'] and ::ActiveRecord::ConnectionAdapters::Column.value_to_boolean(attributes['_destroy'])
100
- pseudo_record_instance = pseudo_record.new(*attributes.except('_destroy').values)
101
- instance_variable_set("@#{name}", instance_variable_get("@#{name}") + [pseudo_record_instance])
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
- class ObjectiveForm::PseudoRecord < Struct
2
- class_attribute :_fields
3
- attr_writer :_destroy
1
+ module ObjectiveForm
2
+ class PseudoRecord < Struct
3
+ class_attribute :_fields
4
+ attr_writer :_destroy
4
5
 
5
- def self.new(*args)
6
- fields = args.extract_options!
7
- super(*fields.keys).tap do |klass|
8
- klass._fields = fields
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
- def persisted?
13
- false
14
- end
13
+ def persisted?
14
+ false
15
+ end
15
16
 
16
- def _destroy
17
- false
17
+ def _destroy
18
+ false
19
+ end
18
20
  end
19
21
  end
@@ -1,2 +1,4 @@
1
- class ObjectiveForm::PseudoRelation < Struct.new(:name, :klass_or_proc)
1
+ module ObjectiveForm
2
+ class PseudoRelation < Struct.new(:name, :klass_or_proc)
3
+ end
2
4
  end
@@ -1,3 +1,3 @@
1
1
  module ObjectiveForm
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: objective_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: