active_hash_fields 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "active_hash_fields"
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Roman Snitko"]
12
- s.date = "2013-10-17"
12
+ s.date = "2013-10-19"
13
13
  s.description = "Adds nested fields to an AR model stored in a serialized field as hash"
14
14
  s.email = "roman.snitko@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -37,7 +37,7 @@ Gem::Specification.new do |s|
37
37
  s.homepage = "http://github.com/snitko/active_hash_fields"
38
38
  s.licenses = ["MIT"]
39
39
  s.require_paths = ["lib"]
40
- s.rubygems_version = "1.8.25"
40
+ s.rubygems_version = "1.8.24"
41
41
  s.summary = "Adds nested fields to an AR model"
42
42
 
43
43
  if s.respond_to? :specification_version then
data/db/test.sqlite3 CHANGED
Binary file
@@ -7,8 +7,9 @@ module ActiveHashFields
7
7
  def initialize(hash, defaults={})
8
8
  hash ||= {}
9
9
  defaults.stringify_keys!; hash.stringify_keys!
10
- @hash = defaults.merge(hash)
11
- @hash.each { |k,v| @hash[k] = self.class.convert_to_boolean(v) }
10
+ @defaults = defaults
11
+ @hash = defaults.merge(hash)
12
+ @hash.each { |k,v| @hash[k] = convert_to_correct_type(k,v) }
12
13
  @hash.delete_if { |k,v| defaults[k].nil? }
13
14
  end
14
15
 
@@ -20,7 +21,7 @@ module ActiveHashFields
20
21
  elsif name.match(/=$/)
21
22
  k = name.sub(/=$/, "")
22
23
  if @hash.has_key?(k)
23
- @hash[k] = self.class.convert_to_boolean(args[0])
24
+ @hash[k] = convert_to_correct_type(k, args[0])
24
25
  end
25
26
  end
26
27
  else
@@ -29,11 +30,19 @@ module ActiveHashFields
29
30
  nil
30
31
  end
31
32
 
32
- def self.convert_to_boolean(v)
33
- if (v == "1" || v == "true" || v == true)
34
- true
35
- elsif v == "0" || v == "false" || v == false
36
- false
33
+ def convert_to_correct_type(k,v)
34
+ if @defaults[k] == false || @defaults[k] == true
35
+ if (v == "1" || v == "true" || v == true)
36
+ true
37
+ elsif v == "0" || v == "false" || v == false
38
+ false
39
+ end
40
+ elsif @defaults[k].kind_of?(Numeric)
41
+ if @defaults[k] =~ /\./
42
+ v.to_f
43
+ else
44
+ v.to_i
45
+ end
37
46
  else
38
47
  v
39
48
  end
@@ -2,9 +2,10 @@ require 'spec_helper'
2
2
 
3
3
  class Dummy < ActiveRecord::Base
4
4
  active_hash_fields :notifications,
5
- hello: false,
6
- hi: false,
5
+ hello: "",
6
+ hi: "",
7
7
  some_boolean_value: false,
8
+ some_integer_value: 0,
8
9
  answers_to_my_questions: true,
9
10
  news: false
10
11
  end
@@ -21,13 +22,20 @@ describe ActiveHashFields do
21
22
  @dummy.notifications.hi.should == "hi"
22
23
  end
23
24
 
24
- it "converts 0 and 1 strings to boolean values" do
25
+ it "converts 0 and 1 strings to boolean values for boolean fields" do
25
26
  @dummy.valid?
26
27
  @dummy.notifications.some_boolean_value.should be_true
27
28
  @dummy.notifications.some_boolean_value = "0"
28
29
  @dummy.notifications.some_boolean_value.should be_false
29
30
  end
30
31
 
32
+ it "converts number strings to integers" do
33
+ @dummy.valid?
34
+ @dummy.notifications.some_integer_value.should == 0
35
+ @dummy.notifications.some_integer_value = "1"
36
+ @dummy.notifications.some_integer_value.should == 1
37
+ end
38
+
31
39
  it "sets default values to the hash as soon as object is initialized" do
32
40
  user1 = Dummy.new(notifications: { news: false } )
33
41
  user2 = Dummy.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_hash_fields
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-17 00:00:00.000000000 Z
12
+ date: 2013-10-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -146,7 +146,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
146
146
  version: '0'
147
147
  segments:
148
148
  - 0
149
- hash: -4192656926053817055
149
+ hash: 2859436679583182602
150
150
  required_rubygems_version: !ruby/object:Gem::Requirement
151
151
  none: false
152
152
  requirements:
@@ -155,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
155
155
  version: '0'
156
156
  requirements: []
157
157
  rubyforge_project:
158
- rubygems_version: 1.8.25
158
+ rubygems_version: 1.8.24
159
159
  signing_key:
160
160
  specification_version: 3
161
161
  summary: Adds nested fields to an AR model