ar_doc_store 0.1.0 → 0.1.1

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: 7e522200ea6aeccb2f865c7642e7ce75ac64224a
4
- data.tar.gz: 76c9d6f63fa544eb7bd38fc1e857db5c99f590aa
3
+ metadata.gz: c4a712bbb6d8bdecd00a0266ffa78914695441c3
4
+ data.tar.gz: 7f05b2322e14a906d047b4762c85014126c9989c
5
5
  SHA512:
6
- metadata.gz: f247dfe4caaac8e32e33bb2c77fde2e7667e46c3a08e54f1fd6b4fd28a9685d445fb935c20b0964044658330b91f14bec70a3ed1823e651e2768252d6bcf4359
7
- data.tar.gz: 5218202cab8de9cec3b81990a95087e9b3d3498837cf283528d8a792458448e837b366a14e2ec3a4ce3b84aa03c2a9160d8ae9e5d43575f906df8a3090fa20d4
6
+ metadata.gz: e8d6cd07419016a93663a11d2134ba2b1f2d60ff35013759031e807fde872f03204410dc7574a19f570ee8d8ccd5b974d1864c189d4e7efc1523ee878c324595
7
+ data.tar.gz: b235a93f0323b82e4e2ac226e903729d162fec7cea3a91be892bd1850bd11f7f80a9b0ee05240d6303a8a6037bedb77b4709d54284f670c7b554f2e433e9e432
@@ -31,18 +31,19 @@ module ArDocStore
31
31
  def create_reader_for(assn_name, class_name)
32
32
  add_method assn_name.to_sym, -> {
33
33
  ivar = "@#{assn_name}"
34
- existing = instance_variable_get(ivar)
35
- return existing if existing
36
- my_class_name = class_name.constantize
37
- items = read_store_attribute(:data, assn_name)
38
- if items.present? && items.first.respond_to?(:keys)
39
- items = ArDocStore::EmbeddedCollection.new items.map { |item| my_class_name.new(item) }
34
+ instance_variable_get(ivar) || begin
35
+ my_class_name = class_name.constantize
36
+ items = read_store_attribute(:data, assn_name)
37
+ if items.is_a?(Array) || items.is_a?(ArDocStore::EmbeddedCollection)
38
+ items = ArDocStore::EmbeddedCollection.new items.map { |item| my_class_name.new(item) }
39
+ else
40
+ items ||= ArDocStore::EmbeddedCollection.new
41
+ end
42
+ instance_variable_set ivar, (items)
43
+ items.parent = self
44
+ items.map {|item| item.parent = self }
45
+ items
40
46
  end
41
- items ||= ArDocStore::EmbeddedCollection.new
42
- instance_variable_set ivar, (items)
43
- items.parent = self
44
- items.map {|item| item.parent = self }
45
- items
46
47
  }
47
48
  end
48
49
  def create_writer_for(assn_name, class_name)
@@ -51,6 +51,8 @@ module ArDocStore
51
51
  self
52
52
  end
53
53
 
54
+ # TODO: This doesn't work very well for embeds_many because the parent needs to have its setter triggered
55
+ # before the embedded model will actually get saved.
54
56
  def save
55
57
  parent && parent.save
56
58
  end
@@ -76,10 +78,6 @@ module ArDocStore
76
78
  @attributes[key] = value
77
79
  end
78
80
 
79
- def data_will_change!
80
- true
81
- end
82
-
83
81
  def to_param
84
82
  id
85
83
  end
@@ -88,6 +86,7 @@ module ArDocStore
88
86
 
89
87
  module ClassMethods
90
88
 
89
+ #:nodoc:
91
90
  def store_accessor(store, key)
92
91
  self.virtual_attributes ||= HashWithIndifferentAccess.new
93
92
  virtual_attributes[key] ||= true
@@ -43,9 +43,20 @@ module ArDocStore
43
43
  def attribute_name_from_foreign_key(name)
44
44
  is_store_accessor_method?(name) ? name : name.match(/\'(\w+)\'/)[0].gsub("'", '')
45
45
  end
46
-
47
-
48
-
46
+
47
+ def clean_attribute_names_for_arel(attribute_names)
48
+ attribute_names.reject{|attribute_name| is_stored_attribute?(attribute_name)}
49
+ end
50
+
51
+ # Overridden otherwise insert and update queries get fooled into thinking that stored attributes are real columns.
52
+ def arel_attributes_with_values_for_create(attribute_names) #:nodoc:
53
+ super clean_attribute_names_for_arel(attribute_names)
54
+ end
55
+
56
+ # Overridden otherwise insert and update queries get fooled into thinking that stored attributes are real columns.
57
+ def arel_attributes_with_values_for_update(attribute_names) #:nodoc:
58
+ super clean_attribute_names_for_arel(attribute_names)
59
+ end
49
60
  end
50
61
 
51
62
  module ClassMethods
@@ -61,6 +72,7 @@ module ArDocStore
61
72
  define_virtual_attribute_method name
62
73
  end
63
74
 
75
+ #:nodoc:
64
76
  def add_ransacker(key, predicate = nil)
65
77
  return unless respond_to?(:ransacker)
66
78
  ransacker key do
@@ -72,6 +84,7 @@ module ArDocStore
72
84
  end
73
85
  end
74
86
 
87
+ #:nodoc:
75
88
  def store_attributes(typecast_method, predicate=nil, attributes=[], default_value=nil)
76
89
  attributes = [attributes] unless attributes.respond_to?(:each)
77
90
  attributes.each do |key|
@@ -85,6 +98,7 @@ module ArDocStore
85
98
  end
86
99
  end
87
100
 
101
+ #:nodoc:
88
102
  def store_attribute_from_symbol(typecast_method, key, default_value)
89
103
  define_method key.to_sym, -> {
90
104
  value = read_store_attribute(:data, key)
@@ -92,12 +106,10 @@ module ArDocStore
92
106
  value.public_send(typecast_method)
93
107
  elsif default_value
94
108
  write_default_store_attribute(key, default_value)
95
- # write_store_attribute(:data, key, default_value)
96
109
  default_value
97
110
  end
98
111
  }
99
112
  define_method "#{key}=".to_sym, -> (value) {
100
- # data_will_change! if @initalized
101
113
  if value == '' || value.nil?
102
114
  write_store_attribute :data, key, nil
103
115
  else
@@ -106,6 +118,9 @@ module ArDocStore
106
118
  }
107
119
  end
108
120
 
121
+ # TODO: add default value support.
122
+ # Default ought to be a hash of attributes,
123
+ # also accept a proc that receives the newly initialized object
109
124
  def store_attribute_from_class(class_name, key)
110
125
  define_method key.to_sym, -> {
111
126
  ivar = "@#{key}"
@@ -147,32 +162,36 @@ module ArDocStore
147
162
  attribute_method_matchers_cache.clear
148
163
  end
149
164
 
150
-
151
-
165
+ # Allows you to define several string attributes at once. Deprecated.
152
166
  def string_attributes(*args)
153
167
  args.each do |arg|
154
168
  attribute arg, as: :string
155
169
  end
156
170
  end
157
171
 
172
+ # Allows you to define several float attributes at once. Deprecated.
158
173
  def float_attributes(*args)
159
174
  args.each do |arg|
160
175
  attribute arg, as: :float
161
176
  end
162
177
  end
163
178
 
179
+ # Allows you to define several integer attributes at once. Deprecated.
164
180
  def integer_attributes(*args)
165
181
  args.each do |arg|
166
182
  attribute arg, as: :integer
167
183
  end
168
184
  end
169
185
 
186
+ # Allows you to define several boolean attributes at once. Deprecated.
170
187
  def boolean_attributes(*args)
171
188
  args.each do |arg|
172
189
  attribute arg, as: :boolean
173
190
  end
174
191
  end
175
-
192
+
193
+ # Shorthand for attribute :name, as: :enumeration, values: %w{a b c}
194
+ # Deprecated.
176
195
  def enumerates(field, *args)
177
196
  options = args.extract_options!
178
197
  options[:as] = :enumeration
@@ -1,3 +1,3 @@
1
1
  module ArDocStore
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_doc_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Furber
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-12 00:00:00.000000000 Z
11
+ date: 2015-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord