frozen_record 0.13.0 → 0.14.0

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: a2caf0ce8ea29240d70604b68d7e1edeaa8b331c
4
- data.tar.gz: 4ccec1e2b69e9a882d48cbbad9df73c9732b5fae
3
+ metadata.gz: aca3d3a4084fea57cbcbc396e95d35ee24750b44
4
+ data.tar.gz: 5fe3827d178da90300e05c49062e1c1259bc366d
5
5
  SHA512:
6
- metadata.gz: 62e573bedb84600e916de3784e764eeb9400555c8f11118dc27787ef7f642f15e927a6e304e96e118eb8c1f3317c57613c1bd395fc6330d2b59ad36d014c653c
7
- data.tar.gz: c225c4584ee248a86cd88e5f888618745d6275fdd27582a47fbedf405095d521c16a7d8484c6866b1c4ce2e618d921a772b0d45335253f4b50d3bc4c1a2d385a
6
+ metadata.gz: 6afd96b57e571297ebd78bc218c0cfe8e06249be474231a50a3f573ad74f2ee8236bc289265362d67067769e7313acd7633ff721bf1349b868d23f917c94c2bc
7
+ data.tar.gz: cfd9d2c57b1238c8df69d311a738cc31d90138523b451f7cabc6bcf7a0c54f553740d47fbf10c92a9f80065e2661c9618d00f97db6975d259262dbcceb2bd84e
@@ -15,19 +15,28 @@ module FrozenRecord
15
15
  end
16
16
 
17
17
  FIND_BY_PATTERN = /\Afind_by_(\w+)(!?)/
18
- FALSY_VALUES = [false, nil, 0, ''].to_set
18
+ FALSY_VALUES = [false, nil, 0, -''].to_set
19
19
 
20
20
  class_attribute :base_path
21
21
 
22
22
  class_attribute :primary_key
23
- self.primary_key = :id
23
+
24
+ class << self
25
+ alias_method :original_primary_key=, :primary_key=
26
+
27
+ def primary_key=(primary_key)
28
+ self.original_primary_key = -primary_key.to_s
29
+ end
30
+ end
31
+
32
+ self.primary_key = 'id'
24
33
 
25
34
  class_attribute :backend
26
35
  self.backend = FrozenRecord::Backends::Yaml
27
36
 
28
37
  class_attribute :auto_reloading
29
38
 
30
- attribute_method_suffix '?'
39
+ attribute_method_suffix -'?'
31
40
 
32
41
  class ThreadSafeStorage
33
42
 
@@ -72,7 +81,7 @@ module FrozenRecord
72
81
  end
73
82
 
74
83
  def respond_to_missing?(name, *)
75
- if name =~ FIND_BY_PATTERN
84
+ if name.to_s =~ FIND_BY_PATTERN
76
85
  load_records # ensure attribute methods are defined
77
86
  return true if $1.split('_and_').all? { |attr| instance_method_already_implemented?(attr) }
78
87
  end
@@ -93,10 +102,7 @@ module FrozenRecord
93
102
  @records ||= begin
94
103
  records = backend.load(file_path)
95
104
  define_attribute_methods(list_attributes(records))
96
- records.map do |attributes|
97
- attributes.symbolize_keys!
98
- new(attributes)
99
- end.freeze
105
+ records.map(&method(:new)).freeze
100
106
  end
101
107
  end
102
108
 
@@ -113,14 +119,14 @@ module FrozenRecord
113
119
  end
114
120
 
115
121
  def method_missing(name, *args)
116
- if name =~ FIND_BY_PATTERN
122
+ if name.to_s =~ FIND_BY_PATTERN
117
123
  return dynamic_match($1, args, $2.present?)
118
124
  end
119
125
  super
120
126
  end
121
127
 
122
128
  def dynamic_match(expression, values, bang)
123
- results = where(expression.split('_and_').zip(values))
129
+ results = where(expression.split('_and_'.freeze).zip(values))
124
130
  bang ? results.first! : results.first
125
131
  end
126
132
 
@@ -128,7 +134,7 @@ module FrozenRecord
128
134
  attributes = Set.new
129
135
  records.each do |record|
130
136
  record.keys.each do |key|
131
- attributes.add(key.to_sym)
137
+ attributes.add(key.to_s)
132
138
  end
133
139
  end
134
140
  attributes.to_a
@@ -137,20 +143,19 @@ module FrozenRecord
137
143
  end
138
144
 
139
145
  def initialize(attrs = {})
140
- @attributes = attrs
146
+ @attributes = attrs.stringify_keys.freeze
141
147
  end
142
148
 
143
149
  def attributes
144
- # We have to return a hash with string keys for backward compatibity reasons
145
- @attributes.stringify_keys
150
+ @attributes.dup
146
151
  end
147
152
 
148
153
  def id
149
- self[primary_key]
154
+ self[primary_key.to_s]
150
155
  end
151
156
 
152
157
  def [](attr)
153
- @attributes[attr.to_sym]
158
+ @attributes[attr.to_s]
154
159
  end
155
160
  alias_method :attribute, :[]
156
161
 
@@ -169,8 +174,7 @@ module FrozenRecord
169
174
  private
170
175
 
171
176
  def attribute?(attribute_name)
172
- value = self[attribute_name]
173
- FALSY_VALUES.exclude?(value) && value.present?
177
+ FALSY_VALUES.exclude?(self[attribute_name]) && self[attribute_name].present?
174
178
  end
175
179
 
176
180
  end
@@ -62,7 +62,7 @@ module FrozenRecord
62
62
  def pluck(*attributes)
63
63
  case attributes.length
64
64
  when 1
65
- to_a.map(&attributes.first)
65
+ to_a.map(&attributes.first.to_sym)
66
66
  when 0
67
67
  raise NotImplementedError, '`.pluck` without arguments is not supported yet'
68
68
  else
@@ -1,3 +1,3 @@
1
1
  module FrozenRecord
2
- VERSION = '0.13.0'
2
+ VERSION = '0.14.0'
3
3
  end
@@ -13,6 +13,24 @@ describe FrozenRecord::Base do
13
13
 
14
14
  end
15
15
 
16
+ describe '.primary_key' do
17
+
18
+ around do |example|
19
+ previous_primary_key = Country.primary_key
20
+ begin
21
+ example.run
22
+ ensure
23
+ Country.primary_key = previous_primary_key
24
+ end
25
+ end
26
+
27
+ it 'is coerced to string' do
28
+ Country.primary_key = :foobar
29
+ expect(Country.primary_key).to be == 'foobar'
30
+ end
31
+
32
+ end
33
+
16
34
  describe '.auto_reloading' do
17
35
 
18
36
  context 'when enabled' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frozen_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean Boussier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-26 00:00:00.000000000 Z
11
+ date: 2019-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel