active_fedora_finders 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,25 +25,12 @@ module ActiveFedora
25
25
  fk
26
26
  end
27
27
 
28
- included do
29
- class << self
30
- alias_method :active_fedora_find, :find
31
- end
32
- end
33
-
34
28
  module ClassMethods
35
- def find(args)
36
- if args.is_a? String
37
- return active_fedora_find(args)
38
- else
39
- return find_by_conditions(nil, args)
40
- end
41
- end
42
29
 
43
30
  # modeled after ActiveRecord::FinderMethods.find_by_attributes
44
31
  def find_by_attributes(match, attribute_names, *args)
45
32
  conditions = Hash[attribute_names.map {|a| [a, args[attribute_names.index(a)]]}]
46
- result = find_by_conditions(match, conditions)
33
+ result = fcrepo_find(match, conditions)
47
34
  if match.bang? && result.blank?
48
35
  raise ActiveRecord::RecordNotFound, "Couldn't find #{self.name} with #{conditions.to_a.collect {|p| p.join(' = ')}.join(', ')}"
49
36
  else
@@ -52,7 +39,7 @@ module ActiveFedora
52
39
  end
53
40
  end
54
41
 
55
- def find_by_conditions(match, args)
42
+ def fcrepo_find(match, args)
56
43
  parms = args.dup
57
44
  maxResults = (match.nil? or match.finder == :first) ? 1 : 25 # find_all and find_last not yet supported
58
45
  query = ""
@@ -84,20 +71,24 @@ module ActiveFedora
84
71
  def process_results(results)
85
72
  results = Nokogiri::XML.parse(results)
86
73
  results = results.xpath('/f:result/f:resultList/f:objectFields/f:pid',{'f'=>"http://www.fedora.info/definitions/1/0/types/"})
87
- results.collect { |result| active_fedora_find(result.text) }
74
+ results.collect { |result| find_one(result.text) }
88
75
  end
89
76
 
90
77
  # this method is patterned after an analog in ActiveRecord::DynamicMatchers
91
78
  def all_attributes_exists?(attribute_names)
79
+ attribute_names.reduce(true) {|result, att| ALL_FIELDS.include? att or SUPPORTED_ALTS.include? att}
80
+ end
81
+
82
+ def normalize_attribute_names!(attribute_names)
92
83
  field_keys = attribute_names.map {|val| FIELD_KEYS[val] or val}
93
84
  attribute_names.replace field_keys
94
- attribute_names.reduce(true) {|result, att| ALL_FIELDS.include? att or SUPPORTED_ALTS.include? att}
95
85
  end
96
86
 
97
87
  # adapted from ActiveRecord::DynamicMatchers
98
88
  def method_missing(method_id, *arguments, &block)
99
89
  if match = (ActiveRecord::DynamicFinderMatch.match(method_id) || ActiveRecord::DynamicScopeMatch.match(method_id))
100
90
  attribute_names = match.attribute_names
91
+ normalize_attribute_names!(attribute_names)
101
92
  super unless all_attributes_exists?(attribute_names)
102
93
  if !(match.is_a?(ActiveRecord::DynamicFinderMatch) && match.instantiator? && arguments.first.is_a?(Hash)) && arguments.size < attribute_names.size
103
94
  method_trace = "#{__FILE__}:#{__LINE__}:in `#{method_id}'"
@@ -1,5 +1,5 @@
1
1
  module ActiveFedora
2
2
  module Finders
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
data/spec/finders_spec.rb CHANGED
@@ -11,21 +11,21 @@ describe ActiveFedora::Finders do
11
11
  describe "finder methods" do
12
12
  describe "default find method" do
13
13
  it "should call the normal find method when passed a string" do
14
- TestBase.expects(:active_fedora_find).returns TestBase.new
14
+ TestBase.expects(:find_one).returns TestBase.new
15
15
  TestBase.find(FCREPO_ID)
16
16
  end
17
17
  end
18
18
  describe "dynamic finder methods" do
19
19
  it "should call .find_by_conditions with correct attributes" do
20
- TestBase.expects(:find_by_conditions).with(is_a(ActiveRecord::DynamicFinderMatch), :identifier => FCREPO_ID).returns(TestBase.new)
20
+ TestBase.expects(:fcrepo_find).with(is_a(ActiveRecord::DynamicFinderMatch), :identifier => FCREPO_ID).returns(TestBase.new)
21
21
  TestBase.find_by_identifier(FCREPO_ID)
22
- TestBase.expects(:find_by_conditions).with(is_a(ActiveRecord::DynamicFinderMatch), :cDate => FCREPO_DATE, :identifier => FCREPO_ID).returns(TestBase.new)
22
+ TestBase.expects(:fcrepo_find).with(is_a(ActiveRecord::DynamicFinderMatch), :cDate => FCREPO_DATE, :identifier => FCREPO_ID).returns(TestBase.new)
23
23
  TestBase.find_by_create_date_and_identifier(FCREPO_DATE, FCREPO_ID)
24
24
  end
25
25
  it "should return an ActiveFedora::Base when there is a single result" do
26
26
  stubfedora = mock("Fedora")
27
27
  stubfedora.expects(:connection).returns(mock("Connection", :find_objects =>fixture('find_one.xml')))
28
- TestBase.expects(:active_fedora_find).with(FCREPO_ID).returns TestBase.new(:pid=>FCREPO_ID)
28
+ TestBase.expects(:find_one).with(FCREPO_ID).returns TestBase.new(:pid=>FCREPO_ID)
29
29
  ActiveFedora::Base.fedora_connection = [stubfedora]
30
30
  TestBase.find_by_identifier(FCREPO_ID).should be_a TestBase
31
31
  end
@@ -33,8 +33,8 @@ describe ActiveFedora::Finders do
33
33
  stubfedora = mock("Fedora")
34
34
  stubfedora.expects(:connection).returns(mock("Connection", :find_objects =>fixture('find_multiple.xml')))
35
35
  ActiveFedora::Base.fedora_connection = [stubfedora]
36
- TestBase.expects(:active_fedora_find).with(FCREPO_ID).returns TestBase.new(:pid=>FCREPO_ID)
37
- TestBase.expects(:active_fedora_find).with("demo:1").returns TestBase.new(:pid=>"demo:1")
36
+ TestBase.expects(:find_one).with(FCREPO_ID).returns TestBase.new(:pid=>FCREPO_ID)
37
+ TestBase.expects(:find_one).with("demo:1").returns TestBase.new(:pid=>"demo:1")
38
38
  TestBase.find_all_by_source("test").should be_a Array
39
39
  end
40
40
  it "should throw an error when no results and a bang" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_fedora_finders
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: 2012-06-25 00:00:00.000000000 Z
12
+ date: 2012-06-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: active-fedora