looksy 0.0.5 → 0.0.6

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.
@@ -33,6 +33,14 @@ module Looksy
33
33
  lookup.all
34
34
  end
35
35
 
36
+ def fetch_first
37
+ lookup.find_first
38
+ end
39
+
40
+ def fetch_last
41
+ lookup.find_last
42
+ end
43
+
36
44
  def fetch_by_id(id)
37
45
  lookup.find_by_id(id)
38
46
  end
@@ -1,17 +1,25 @@
1
1
  module Looksy
2
- class DynamicFindMatch < Struct.new(:finder, :attributes)
2
+ class DynamicFindMatch < Struct.new(:finder, :extractor, :attributes)
3
3
  def self.match(method)
4
4
  finder = :find
5
+ extractor = nil
5
6
 
6
7
  case method.to_s
7
- when /^find_(all_)?by_([_a-zA-Z]\w*)$/
8
- finder = :select if $1 == 'all_'
8
+ when /^find_(last_|all_)?by_([_a-zA-Z]\w*)$/
9
+ case $1
10
+ when 'last_'
11
+ finder = :select
12
+ extractor = :last
13
+ when 'all_'
14
+ finder = :select
15
+ end
16
+
9
17
  attributes = $2
10
18
  else
11
19
  return nil
12
20
  end
13
21
 
14
- new(finder, attributes.split('_and_'))
22
+ new(finder, extractor, attributes.split('_and_'))
15
23
  end
16
24
  end
17
25
  end
@@ -9,6 +9,14 @@ module Looksy
9
9
  @cache.fetch(@klass.cache_key, @klass.cache_options) { @klass.all }
10
10
  end
11
11
 
12
+ def find_first
13
+ all.first
14
+ end
15
+
16
+ def find_last
17
+ all.last
18
+ end
19
+
12
20
  def find_by_id(id)
13
21
  all.find { |record| record.id == id.to_i }
14
22
  end
@@ -21,13 +29,15 @@ module Looksy
21
29
  args[match.attributes.index(attribute)]
22
30
  end
23
31
 
24
- all.send(match.finder) do |record|
32
+ result = all.send(match.finder) do |record|
25
33
  record_attributes = extract_attributes(match.attributes) do |attribute|
26
34
  record.send(attribute)
27
35
  end
28
36
 
29
37
  record_attributes == attributes
30
38
  end
39
+
40
+ match.extractor ? result.send(match.extractor) : result
31
41
  else
32
42
  super
33
43
  end
@@ -1,3 +1,3 @@
1
1
  module Looksy
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -69,6 +69,18 @@ describe Looksy::Cacheable do
69
69
  end
70
70
  end
71
71
 
72
+ describe '.fetch_first' do
73
+ it 'returns the first record' do
74
+ klass.fetch_first.should eql(klass.first)
75
+ end
76
+ end
77
+
78
+ describe '.fetch_last' do
79
+ it 'returns the last record' do
80
+ klass.fetch_last.should eql(klass.last)
81
+ end
82
+ end
83
+
72
84
  describe '.fetch_by_id' do
73
85
  let(:record) { klass.last }
74
86
 
@@ -38,6 +38,19 @@ describe Looksy::DynamicFindMatch do
38
38
  match.finder.should eql(:select)
39
39
  end
40
40
  end
41
+
42
+ context 'when method finds last record' do
43
+ let(:method) { :find_last_by_this }
44
+ let(:match) { Looksy::DynamicFindMatch.match(method) }
45
+
46
+ it 'returns the correct extractor' do
47
+ match.extractor.should eql(:last)
48
+ end
49
+
50
+ it 'returns the correct finder' do
51
+ match.finder.should eql(:select)
52
+ end
53
+ end
41
54
  end
42
55
 
43
56
  context 'when method does not match' do
@@ -12,6 +12,18 @@ describe Looksy::Lookup do
12
12
  end
13
13
  end
14
14
 
15
+ describe '#find_first' do
16
+ it 'retrieves the first record' do
17
+ lookup.find_first.should eql(klass.first)
18
+ end
19
+ end
20
+
21
+ describe '#find_last' do
22
+ it 'retrieves the last record' do
23
+ lookup.find_last.should eql(klass.last)
24
+ end
25
+ end
26
+
15
27
  describe '#find_by_id' do
16
28
  let(:record) { lookup.find_by_id(2) }
17
29
 
@@ -46,7 +58,7 @@ describe Looksy::Lookup do
46
58
  context 'when finding by single attribute' do
47
59
  let(:record) { lookup.find_by_name('Artin') }
48
60
 
49
- it 'returns the matching receord' do
61
+ it 'returns the matching record' do
50
62
  record.name.should eql('Artin')
51
63
  end
52
64
  end
@@ -60,5 +72,14 @@ describe Looksy::Lookup do
60
72
  end
61
73
  end
62
74
  end
75
+
76
+ context 'when finding last matching record' do
77
+ let(:match) { Record.all.select { |r| r.name == 'Artin'}.last }
78
+ let(:record) { lookup.find_last_by_name('Artin') }
79
+
80
+ it 'returns the matching record' do
81
+ record.should eql(match)
82
+ end
83
+ end
63
84
  end
64
85
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: looksy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &257341200 !ruby/object:Gem::Requirement
17
+ requirement: &90706100 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *257341200
25
+ version_requirements: *90706100
26
26
  description: Add a caching layer to your ActiveRecord models that represent look up
27
27
  tables
28
28
  email: