restly 0.0.1.beta.5 → 0.0.1.beta.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,14 +26,16 @@ module Restly::Associations::Base::Loaders
26
26
  def load_instance(parent, association_class = self.association_class)
27
27
  raise Restly::Error::AssociationError, "Not an instance" if collection?
28
28
  return nil if embedded?
29
- instance = if parent.attributes.has_key? "#{name}_id"
30
- foreign_key = parent.attributes["#{name}_id"]
31
- return nil unless foreign_key
32
- association_class.find(foreign_key)
29
+ foreign_key = options[:foreign_key] || "#{name}_id"
30
+ instance = if parent.attributes.has_key? foreign_key
31
+ id = parent.attributes[foreign_key]
32
+ return nil unless id
33
+ association_class.find(id)
33
34
  else
34
- association_class.instance_from_response association_class.connection.get(association_class.path)
35
+ association_class.instance_from_response association_class.connection.get(association_class.path_with_format)
35
36
  end
36
37
  Restly::Proxies::Associations::Instance.new(instance, parent)
37
38
  end
38
39
 
39
- end
40
+
41
+ end
@@ -66,6 +66,7 @@ module Restly
66
66
  # Set Defaults on Inheritance
67
67
  inherited do
68
68
  field :id
69
+ find_by :id
69
70
  self.resource_name = name.gsub(/.*::/,'').underscore if name.present?
70
71
  self.path = resource_name.pluralize
71
72
  self.params = params.dup
@@ -46,7 +46,7 @@ module Restly::Base::Fields
46
46
  end
47
47
 
48
48
  def include?(value)
49
- super(value.to_sym) || super(value.to_s)
49
+ super(value.try :to_sym) || super(value.try :to_s)
50
50
  end
51
51
 
52
52
  def <<(value)
@@ -2,7 +2,7 @@ module Restly::Base::Includes
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  included do
5
- class_attribute :current_specification, instance_writer: false
5
+ class_attribute :finder, :current_specification, instance_writer: false
6
6
  end
7
7
 
8
8
  module ClassMethods
@@ -24,6 +24,11 @@ module Restly::Base::Includes
24
24
  @client
25
25
  end
26
26
 
27
+ def find_by(field)
28
+ raise Restly::Error::InvalidField, "#{field} was not a valid field." unless field.in? fields
29
+ self.finder = field
30
+ end
31
+
27
32
  def has_specification
28
33
  self.current_specification = Restly::Base::Resource::Specification.new(self)
29
34
 
@@ -71,11 +71,12 @@ module Restly::Base::Instance
71
71
  end
72
72
 
73
73
  def path
74
- return @path if @path
75
- if response && response.response.env[:url]
76
- response.response.env[:url].path.gsub(/\.\w+$/,'')
77
- elsif respond_to?(:id) && id
78
- [self.class.path, id].join('/')
74
+ if @path
75
+ @path
76
+ elsif path_from_response
77
+ path_from_response
78
+ elsif send self.finder
79
+ [self.class.path, send self.finder].join('/')
79
80
  else
80
81
  self.class.path
81
82
  end
@@ -99,6 +100,11 @@ module Restly::Base::Instance
99
100
  end
100
101
  end
101
102
 
103
+ def path_from_response
104
+ return nil unless response
105
+ response.response.env[:url].try(:path).try(:gsub, /\.\w+$/,'')
106
+ end
107
+
102
108
  def parsed_response(response=self.response)
103
109
  return {} unless response
104
110
  parsed = response.parsed || {}
@@ -1,7 +1,7 @@
1
1
  module Restly::Base::Instance::Persistence
2
2
 
3
3
  def exists?
4
- return false unless id
4
+ return false unless id || path_from_response
5
5
 
6
6
  begin
7
7
  @response = connection.get(path, force: true) unless @response.status.to_i < 400
@@ -2,11 +2,10 @@ module Restly::Base::Resource::Finders
2
2
 
3
3
  Collection = Restly::Collection
4
4
 
5
- def find(id, *args)
5
+ def find(finder, *args)
6
+ finder = nil unless self.finder.in? fields
6
7
  options = args.extract_options!
7
-
8
- #params[pagination_options[:params][:page]] = options[:page] if pagination
9
- instance_from_response connection.get(path_with_format(id), params: params)
8
+ instance_from_response connection.get(path_with_format(finder), params: params)
10
9
  end
11
10
 
12
11
  def all
@@ -45,7 +45,7 @@ module Restly::NestedAttributes
45
45
 
46
46
  if attributes_collection.is_a? Hash
47
47
  keys = attributes_collection.keys
48
- attributes_collection = if keys.include?('id') || keys.include?(:id)
48
+ attributes_collection = if keys.include?(self.finder.try(:to_s)) || keys.include?(self.finder.try(:to_sym))
49
49
  Array.wrap(attributes_collection)
50
50
  else
51
51
  attributes_collection.values
@@ -57,9 +57,9 @@ module Restly::NestedAttributes
57
57
 
58
58
  attributes_collection.each do |attributes|
59
59
  attributes = attributes.with_indifferent_access
60
- if attributes[:id].blank?
61
- send(association_name) << association.build(self, attributes.except(:id))
62
- elsif (existing_record = existing_records.find{ |record| record.id.to_s == attributes['id'].to_s })
60
+ if attributes[self.finder].blank?
61
+ send(association_name) << association.build(self, attributes.except(self.finder))
62
+ elsif (existing_record = existing_records.find{ |record| record.id.to_s == attributes[self.finder].to_s })
63
63
  existing_record.attributes = attributes
64
64
  end
65
65
  end
@@ -1,3 +1,3 @@
1
1
  module Restly
2
- VERSION = "0.0.1.beta.5"
2
+ VERSION = "0.0.1.beta.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.beta.5
4
+ version: 0.0.1.beta.6
5
5
  prerelease: 6
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-11-26 00:00:00.000000000 Z
12
+ date: 2012-12-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: oauth2