path_utilities 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 376c2224a5ecd56519af4e2fa97fc6ba87d1853b
4
- data.tar.gz: a70641cb2463673f3df64b8a87a3bdfa980e85fd
3
+ metadata.gz: ce9538e9406d3bbe17a3583cac61be64b3ba9b3a
4
+ data.tar.gz: a7c6db1ae49369e99934909a22b6ecb4bcf2dc0e
5
5
  SHA512:
6
- metadata.gz: 65ed40c98c4bc0492145d16adaf9e5d71eeaaa1381c027fbb7294ab8fd3f70317c7add39243d2888b739c07b69d3935278574529e301bd6c849e99e99929b797
7
- data.tar.gz: dcd8705b8baea65b5b17456d4ef320506925ab7b5179986db6a00d3baae829cf484548ed253da67bd4820f37cd01fa2a50949660ef8aadbe37d8c205c29e8114
6
+ metadata.gz: 7ab291b0776b78fb4671987775d07d5f4f3ce6f8e1c086d762e25353b8d27d529a1e187226cb1b48cbeafbcdcba5b668cd0f84d996bf31cc16e7a3e701f343ec
7
+ data.tar.gz: d2b45f59d99e107189082573cad11f5a8ee149d97d190108fc1e657d81e432ac1cbad3183af5e5aaa2c6911cfbcb1756e442584bd3035c1667834a2f63336eed
@@ -13,11 +13,14 @@ module PathUtilities
13
13
 
14
14
  included do
15
15
  include Virtus.model
16
- extend ActiveModel::Naming
17
16
  include ActiveModel::Conversion
18
17
  include ActiveModel::Validations
18
+ include ActiveSupport::Configurable
19
19
  include TrackingChanges
20
20
 
21
+ config_accessor :model_name
22
+ config_accessor(:fields) { {} }
23
+ config_accessor(:models) { [] }
21
24
  attr_reader :models_mapping
22
25
 
23
26
  delegate :id, :persisted?, :new_record?, to: :main_record
@@ -28,88 +31,93 @@ module PathUtilities
28
31
  init_sync
29
32
  end
30
33
 
31
- def validate_mapping!
32
- self.class.models.each do |model|
33
- next if models_mapping.keys.include?(model)
34
- fail "#{model.to_s.camelize} not mapped on initialization"
35
- end
36
- end
37
-
38
34
  def validate(params)
39
35
  params = HashWithIndifferentAccess.new(params)
36
+
40
37
  self.class.fields.keys.each do |field|
41
38
  any_changes = params[field] &&
42
39
  params[field] != instance_model_for(field).send(field)
43
40
  next unless any_changes
44
41
  send("#{field}=", params[field])
45
42
  end
46
-
47
43
  valid?
48
44
  end
49
45
 
50
- def save
51
- if valid?
52
- sync
53
- persist!
54
- true
55
- else
56
- false
57
- end
46
+ def self.setup_model_name(name)
47
+ self.model_name = ActiveModel::Name.new(self, nil, name.to_s)
58
48
  end
49
+ end
59
50
 
60
- def sync
61
- self.class.fields.keys.each do |field|
62
- form_field_value = send(field)
63
- instance_model_for(field).send("#{field}=", form_field_value)
64
- end
51
+ def validate_mapping!
52
+ self.class.models.each do |model|
53
+ next if models_mapping.keys.include?(model)
54
+ fail "#{model.to_s.camelize} not mapped on initialization"
65
55
  end
56
+ end
66
57
 
67
- def instance_model_for(field)
68
- model = self.class.fields[field].options[:klass].name.underscore.to_sym
69
- models_mapping[model]
58
+ def save
59
+ if valid?
60
+ sync
61
+ persist!
62
+ true
63
+ else
64
+ false
70
65
  end
66
+ end
71
67
 
72
- def init_sync
73
- self.class.fields.keys.each do |field|
74
- model_value = instance_model_for(field).send("#{field}")
75
- send("#{field}=", model_value)
76
- end
68
+ def sync
69
+ self.class.fields.keys.each do |field|
70
+ form_field_value = send(field)
71
+ instance_model_for(field).send("#{field}=", form_field_value)
77
72
  end
73
+ end
78
74
 
79
- def main_record
80
- models_mapping[self.class.model_name.name.to_sym]
81
- end
75
+ def instance_model_for(field)
76
+ model = self.class.fields[field].options[:klass].name.underscore.to_sym
77
+ models_mapping[model]
78
+ end
82
79
 
83
- def persist!
84
- models_mapping.values.all?(&:save)
80
+ def init_sync
81
+ self.class.fields.keys.each do |field|
82
+ model_value = instance_model_for(field).send("#{field}")
83
+ send("#{field}=", model_value)
85
84
  end
86
- private :persist!
87
85
  end
88
86
 
87
+ def main_record
88
+ self.class.model_name || fail('setup_model_name not set in form class')
89
+ models_mapping[self.class.model_name.name.to_sym]
90
+ end
91
+
92
+ def persist!
93
+ models_mapping.values.all?(&:save)
94
+ end
95
+ private :persist!
96
+
89
97
  class_methods do
90
98
  def properties(attributes, model)
91
- @@attributes ||= {}
92
99
  add_model(model)
93
100
  attributes.each do |att|
94
101
  already_define_attribute_warn(att, model) do
95
102
  attribute att, String
96
103
  end
97
104
 
98
- @@attributes[att] = model.to_s.camelize
99
- .safe_constantize.fields[att.to_s]
105
+ set_attribute(att, model.to_s.camelize
106
+ .safe_constantize.fields[att.to_s])
100
107
  end
101
108
  end
102
109
 
103
- def setup_model_name(name)
104
- @@model_name = ActiveModel::Name.new(self, nil, name.to_s)
105
- end
106
-
107
- def model_name
108
- @@model_name || fail('setup_model_name not set in form class')
110
+ def add_model(name)
111
+ return if self.models.include?(name.to_sym)
112
+ existing_models = models
113
+ existing_models << name.to_sym
114
+ self.models = existing_models
109
115
  end
110
116
 
111
- def fields
112
- @@attributes
117
+ def set_attribute(name, value)
118
+ existing_fields = self.fields
119
+ existing_fields[name] = value
120
+ self.fields = existing_fields
113
121
  end
114
122
 
115
123
  def already_define_attribute_warn(attr, new_model)
@@ -124,16 +132,6 @@ module PathUtilities
124
132
  end
125
133
  end
126
134
 
127
- def models
128
- @@models ||= []
129
- end
130
-
131
- def add_model(name)
132
- @@models ||= []
133
- return if @@models.include?(name.to_sym)
134
- @@models << name.to_sym
135
- end
136
-
137
135
  def validates_uniqueness_of(attribute, options = {})
138
136
  options = { case_sensitive: false, attributes: [attribute],
139
137
  model: model_for(attribute) }
@@ -1,3 +1,3 @@
1
1
  module PathUtilities
2
- VERSION = "0.2.3"
2
+ VERSION = '0.2.4'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: path_utilities
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricard Forniol Agustí
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-15 00:00:00.000000000 Z
11
+ date: 2015-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug