active_store_accessor 0.2.0 → 0.2.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: 06ba4627f521236634c1de9d03b20e672c5477aa
4
- data.tar.gz: 139b01fbfc855b5959ef426844cd634fd1342f1a
3
+ metadata.gz: 3d80e0a2c9f3bf1d6fde04802b05c5d01f89f179
4
+ data.tar.gz: 92665c56d16926a3b909c0d45650f0a6032ffe26
5
5
  SHA512:
6
- metadata.gz: 061f082b0c32e47db4489b6d07045b3d6557a57df737cb7373afa6046ff1693aa894ac8e52ad311810434508f26a76a9c6b8be24826b75255e076ad091889e67
7
- data.tar.gz: 8e9952ba446c200649fea03e94a5ee4a6b96039de92f1e69b1843e92192a8667a443d709b65558822b48690c77a6bf7b0a0c738e0fde15642c36da592215931c
6
+ metadata.gz: b91de091930c91921061b8dd70713b3c568bb6e4c8c29009fa191f49ceac194782e27e2c3132cbff2ac542b97b6e563da4d54e73a5bd7e6d94772a567f29ed87
7
+ data.tar.gz: 82ea8763e58340fe731de04f2db72a2f6ed6bab7e1b1bfbba98199e89b9fee93a7e58639943bf8279f29734b2aeb2368951947b242690ecbd644583b9ac4f17d
data/.travis.yml CHANGED
@@ -1,6 +1,7 @@
1
1
  env:
2
2
  - "AR_VERSION=4.0"
3
3
  - "AR_VERSION=4.1"
4
+ - "AR_VERSION=4.2"
4
5
  rvm:
5
6
  - 1.9.3
6
7
  - 2.0.0
@@ -8,4 +9,4 @@ rvm:
8
9
  addons:
9
10
  postgresql: "9.3"
10
11
  before_script:
11
- - psql -c 'create database asa_test;' -U rails
12
+ - psql -c 'create database asa_test;' -U rails -d postgres
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ source 'https://rubygems.org'
5
5
  ar_version = ENV["AR_VERSION"]
6
6
  ar_version = case ar_version
7
7
  when "4.0" then "~> 4.0.0"
8
+ when "4.2" then "~> 4.2.0.beta4"
8
9
  else
9
10
  "~> 4.1.0"
10
11
  end
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "active_store_accessor"
7
- spec.version = "0.2.0"
7
+ spec.version = "0.2.1"
8
8
  spec.authors = ["Sergey Pchelincev"]
9
9
  spec.email = ["mail@sergeyp.me"]
10
10
  spec.summary = %q{Get more from ActiveRecord::Store}
@@ -6,7 +6,10 @@ module ActiveStoreAccessor
6
6
  end
7
7
 
8
8
  def active_store_accessor(column_name, attrs)
9
- if columns.detect { |column| column.name == column_name.to_s }.text?
9
+ column = columns.detect { |column| column.name == column_name.to_s }
10
+ if !column
11
+ raise "[active_store_accessor] The column '#{column_name}' does not exist in the model #{name}."
12
+ elsif column.type == :text
10
13
  serialize(column_name) unless serialized_attributes.include?(column_name.to_s)
11
14
  end
12
15
 
@@ -16,22 +19,31 @@ module ActiveStoreAccessor
16
19
  options = { type: options.to_s } unless options.is_a?(Hash)
17
20
  type = options.fetch(:type) { raise ArgumentError, "please specify type of `#{ attr_name }` attribute" }.to_s
18
21
 
19
- # time for activerecord is only a hours and minutes without date part
20
- # but for most rubist and rails developer it should contains a date too
21
- type = :datetime if type == :time
22
-
23
- args = [attr_name.to_s, options[:default], type]
24
- active_store_attributes[attr_name] = ActiveRecord::ConnectionAdapters::Column.new(*args)
22
+ config = if connection.respond_to?(:lookup_cast_type)
23
+ column = connection.lookup_cast_type(type)
24
+ [column.method(:type_cast_from_database), column.method(:type_cast_for_database)]
25
+ else
26
+ # time for activerecord is only a hours and minutes without date part
27
+ # but for most rubist and rails developer it should contains a date too
28
+ type = 'datetime' if type == 'time'
29
+ args = [attr_name.to_s, options[:default], type]
30
+ column = ActiveRecord::ConnectionAdapters::Column.new(*args)
31
+ [column.method(:type_cast), column.method(:type_cast_for_write)]
32
+ end
33
+
34
+ config << options[:default]
35
+ active_store_attributes[attr_name] = config
25
36
 
26
37
  _active_store_accessor_module.module_eval <<-RUBY
27
38
  def #{ attr_name }
28
- column = self.class.active_store_attributes[:#{ attr_name }]
29
- value = column.type_cast(super)
30
- value.nil? ? column.default : value
39
+ getter, _, default = self.class.active_store_attributes[:#{ attr_name }]
40
+ value = getter.call(super)
41
+ value.nil? ? default : value
31
42
  end
32
43
 
33
44
  def #{ attr_name }=(value)
34
- super self.class.active_store_attributes[:#{ attr_name }].type_cast_for_write(value)
45
+ _, setter, _ = self.class.active_store_attributes[:#{ attr_name }]
46
+ super setter.call(value)
35
47
  end
36
48
  RUBY
37
49
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_store_accessor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Pchelincev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-10 00:00:00.000000000 Z
11
+ date: 2014-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord