findable 0.2.0 → 0.2.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 022bdcf5a11ab55395af5ca7af256b23afd96376
4
- data.tar.gz: 86900a51201b84491a1e361f0945ec4d36b4e064
3
+ metadata.gz: ed73de3540dfd5ad802f506f399aa52cfa748a7f
4
+ data.tar.gz: f35dc8a71965b62c822e6aa7a9070f733b043db5
5
5
  SHA512:
6
- metadata.gz: 880d6023b382295648fd89c61c4e1bba1a73bc3a6e9266b7b84840d0eee4722ef388a8e94ba9b5c166e752f74b79a36b24577b1f6ba68998e5726306a1fd385d
7
- data.tar.gz: 0670803e036bd49527ebc2d4208afb298ab5faed78eee5e2fc8052a00b340b4228efc7b6fbec58bc63d9e890aa7902b516b09afc9c52bd9208969de6437be339
6
+ metadata.gz: 8ffdcafaef3953de07e19f63d804bd25e0c69db535d964b96c4f0da81d3f4cddcdcb1bd1ddebaccc2dee5e48fb04a1a4d234526830050dfb423d5ef903b7b68e
7
+ data.tar.gz: 41d2b9c45cbd8b5902c66ca28779aa8d3bac5d453ce7912bb2578cd8180326598fb89ef77bf9f6a507233554cd74e4e8ece7a1ecd14e03527aab83720722a948
@@ -1,4 +1,5 @@
1
1
  require "json"
2
+ require "bigdecimal"
2
3
 
3
4
  require "active_support"
4
5
  require "active_support/core_ext"
@@ -1,3 +1,5 @@
1
+ require "findable/associations/utils"
2
+
1
3
  module Findable
2
4
  module Associations
3
5
  module ActiveRecordExt
@@ -19,7 +19,7 @@ module Findable
19
19
  name = args.first
20
20
  if !public_method_defined?(name) || options.present?
21
21
  define_attribute_methods name
22
- conversion = Conversion.for(options[:type])
22
+ conversion = Findable::Schema::Conversion.for(options[:type])
23
23
  define_method(name) { conversion.call(attributes[name.to_sym]) }
24
24
  column_names << name.to_sym
25
25
  end
@@ -17,10 +17,13 @@ module Findable
17
17
 
18
18
  def add_type!(type)
19
19
  return type if type.respond_to?(:call)
20
- raise ArgumentError unless private_method_defined?(type)
21
20
  types[type.to_sym] = method(type)
22
21
  end
23
22
 
23
+ def clear_types
24
+ @_types = nil
25
+ end
26
+
24
27
  private
25
28
  # Conversion methods
26
29
  def integer(value)
@@ -42,10 +45,8 @@ module Findable
42
45
  def boolean(value)
43
46
  if value.is_a?(TrueClass) || value.is_a?(FalseClass)
44
47
  value
45
- elsif value.in?(FALSE_VALUE)
46
- false
47
48
  else
48
- !!value
49
+ value.in?(FALSE_VALUE) ? false : !!value
49
50
  end
50
51
  end
51
52
 
@@ -58,7 +59,7 @@ module Findable
58
59
  if value.is_a?(Time) || value.is_a?(ActiveSupport::TimeWithZone)
59
60
  return value
60
61
  end
61
- Time.zone.parse(value)
62
+ (Time.zone || Time).parse(value)
62
63
  end
63
64
 
64
65
  def symbol(value)
@@ -1,3 +1,3 @@
1
1
  module Findable
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -11,7 +11,7 @@ describe Findable::Associations::Utils do
11
11
  it { expect(utils.model_for(model_name.plural, collection: true)).to eq(model) }
12
12
  it { expect(utils.model_for("invalid", class_name: model_name.name)).to eq(model) }
13
13
 
14
- it { expect { utils.model_for("invalid") }.to raise_error }
14
+ it { expect { utils.model_for("invalid") }.to raise_error(NameError) }
15
15
  it { expect { utils.model_for("invalid", safe: true) }.not_to raise_error }
16
16
  it { expect(utils.model_for("invalid", safe: true)).to be_nil }
17
17
  end
@@ -55,7 +55,7 @@ describe Findable::Configuration do
55
55
  it {
56
56
  expect {
57
57
  Findable.configure {|config| config.unknown = :value }
58
- }.to raise_error
58
+ }.to raise_error(NoMethodError)
59
59
  }
60
60
  end
61
61
 
@@ -0,0 +1,96 @@
1
+ require "spec_helper"
2
+
3
+ describe Findable::Schema::Conversion do
4
+ after(:each) { conversion.clear_types }
5
+ let(:conversion) { Findable::Schema::Conversion }
6
+
7
+ describe ".for" do
8
+ it { expect(conversion.for(nil)).to eq(conversion.types[:default]) }
9
+ it { expect(conversion.for(:integer)).to eq(conversion.types[:integer]) }
10
+ it { expect(conversion.for(:float)).to eq(conversion.types[:float]) }
11
+ it { expect(conversion.for(:decimal)).to eq(conversion.types[:decimal]) }
12
+ it { expect(conversion.for(:string)).to eq(conversion.types[:string]) }
13
+ it { expect(conversion.for(:boolean)).to eq(conversion.types[:boolean]) }
14
+ it { expect(conversion.for(:date)).to eq(conversion.types[:date]) }
15
+ it { expect(conversion.for(:datetime)).to eq(conversion.types[:datetime]) }
16
+ it { expect(conversion.for(:symbol)).to eq(conversion.types[:symbol]) }
17
+ it { expect(conversion.for(:inquiry)).to eq(conversion.types[:inquiry]) }
18
+ end
19
+
20
+ describe ".types" do
21
+ it { expect(conversion.types).to be_kind_of(Hash) }
22
+ it { expect(conversion.types[:default]).not_to be_nil }
23
+ end
24
+
25
+ describe ".add_type!" do
26
+ it {
27
+ expect {
28
+ conversion.add_type!(:integer)
29
+ }.to change { conversion.types.size }.by(1)
30
+ }
31
+ end
32
+
33
+ describe ".integer" do
34
+ it { expect(conversion.send(:integer, "1")).to be_kind_of(Integer) }
35
+ it { expect(conversion.send(:integer, 1)).to eq(1) }
36
+ end
37
+
38
+ describe ".float" do
39
+ it { expect(conversion.send(:float, "1")).to be_kind_of(Float) }
40
+ it { expect(conversion.send(:float, 1)).to eq(1.to_f) }
41
+ end
42
+
43
+ describe ".decimal" do
44
+ it { expect(conversion.send(:decimal, "1")).to be_kind_of(BigDecimal) }
45
+ it { expect(conversion.send(:decimal, 1)).to eq(BigDecimal(1)) }
46
+ end
47
+
48
+ describe ".string" do
49
+ it { expect(conversion.send(:string, 1)).to be_kind_of(String) }
50
+ it { expect(conversion.send(:string, 1)).to eq("1") }
51
+ end
52
+
53
+ describe ".boolean" do
54
+ it { expect(conversion.send(:boolean, true)).to be_truthy }
55
+ it { expect(conversion.send(:boolean, false)).to be_falsey }
56
+ it { expect(conversion.send(:boolean, "false")).to be_falsey }
57
+ it { expect(conversion.send(:boolean, "0")).to be_falsey }
58
+ it { expect(conversion.send(:boolean, "true")).to be_truthy }
59
+ end
60
+
61
+ describe ".date" do
62
+ let(:strdate) { "2015-07-12" }
63
+ it { expect(conversion.send(:date, strdate)).to be_kind_of(Date) }
64
+ it { expect(conversion.send(:date, strdate)).to eq(Date.parse(strdate)) }
65
+ it {
66
+ d = Date.current
67
+ expect(conversion.send(:date, d)).to eq(d)
68
+ }
69
+ end
70
+
71
+ describe ".datetime" do
72
+ let(:strtime) { "2015-07-12 22:15:48 +0900" }
73
+ it { expect(conversion.send(:datetime, strtime)).to be_kind_of(Time) }
74
+ it { expect(conversion.send(:datetime, strtime)).to eq(Time.parse(strtime)) }
75
+ it {
76
+ t = Time.now
77
+ expect(conversion.send(:datetime, t)).to eq(t)
78
+ }
79
+ it {
80
+ t = Time.current
81
+ expect(conversion.send(:datetime, t)).to eq(t)
82
+ }
83
+ end
84
+
85
+ describe ".symbol" do
86
+ let(:string) { "string" }
87
+ it { expect(conversion.send(:symbol, string)).to be_kind_of(Symbol) }
88
+ it { expect(conversion.send(:symbol, string)).to eq(string.to_sym) }
89
+ end
90
+
91
+ describe ".inquiry" do
92
+ let(:string) { "string" }
93
+ it { expect(conversion.send(:inquiry, string)).to be_kind_of(ActiveSupport::StringInquirer) }
94
+ it { expect(conversion.send(:inquiry, string)).to eq(string.inquiry) }
95
+ end
96
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: findable
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
  - i2bskn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-12 00:00:00.000000000 Z
11
+ date: 2015-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -213,6 +213,7 @@ files:
213
213
  - spec/findable/query/connection_spec.rb
214
214
  - spec/findable/query/namespace_spec.rb
215
215
  - spec/findable/query_spec.rb
216
+ - spec/findable/schema/conversion_spec.rb
216
217
  - spec/spec_helper.rb
217
218
  - spec/support/initialize_database.rb
218
219
  - spec/support/shared_contexts.rb
@@ -251,6 +252,7 @@ test_files:
251
252
  - spec/findable/query/connection_spec.rb
252
253
  - spec/findable/query/namespace_spec.rb
253
254
  - spec/findable/query_spec.rb
255
+ - spec/findable/schema/conversion_spec.rb
254
256
  - spec/spec_helper.rb
255
257
  - spec/support/initialize_database.rb
256
258
  - spec/support/shared_contexts.rb