restly 0.0.1.alpha.9 → 0.0.1.alpha.10

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.
@@ -13,7 +13,7 @@ class Restly::Associations::Base
13
13
  include Modifiers
14
14
  include Conditionals
15
15
 
16
- attr_reader :name, :association_class, :namespace, :polymorphic, :options
16
+ attr_reader :name, :namespace, :polymorphic, :options
17
17
 
18
18
  def initialize(owner, name, options={})
19
19
  @name = name
@@ -21,10 +21,13 @@ class Restly::Associations::Base
21
21
  @polymorphic = options.delete(:polymorphic)
22
22
  options[:class_name] ||= name.to_s.classify
23
23
  @owner = owner
24
- @association_class = [@namespace, options.delete(:class_name)].select(&:present?).join('::').constantize
25
24
  @options = options
26
25
  end
27
26
 
27
+ def association_class
28
+ [@namespace, options(:class_name)].select(&:present?).join('::').constantize
29
+ end
30
+
28
31
  private
29
32
 
30
33
  def association_resource_name
data/lib/restly/base.rb CHANGED
@@ -6,7 +6,6 @@ module Restly
6
6
  autoload :Instance
7
7
  autoload :GenericMethods
8
8
  autoload :Includes
9
- autoload :MassAssignmentSecurity
10
9
  autoload :Fields
11
10
  autoload :EmbeddedAssociations
12
11
 
@@ -38,7 +37,6 @@ module Restly
38
37
  include Includes
39
38
  include Instance
40
39
  include Fields
41
- include MassAssignmentSecurity
42
40
 
43
41
  # Relationships
44
42
  include Restly::Associations
@@ -2,8 +2,6 @@ module Restly::Base::Fields
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  included do
5
- extend SharedMethods
6
- include SharedMethods
7
5
  extend ClassMethods
8
6
 
9
7
  class_attribute :fields
@@ -16,31 +14,16 @@ module Restly::Base::Fields
16
14
 
17
15
  end
18
16
 
19
- module SharedMethods
20
-
21
- private
22
-
23
- def set_field(attr)
24
- base = self.is_a?(Class) ? self : singleton_class
25
- unless base.send :instance_method_already_implemented?, attr
26
- base.send :define_attribute_method, attr
27
- self.fields += [attr]
28
- end
29
- end
30
-
31
- end
32
-
33
17
  module ClassMethods
34
18
 
35
19
  private
36
20
 
37
21
  def field(attr)
38
- if attr.is_a?(Hash) && attr[:from_spec]
39
- before_initialize do
40
- spec[:attributes].each{ |attr| set_field(attr) }
22
+ if attr.is_a?(Symbol) || attr.is_a?(String)
23
+ unless instance_method_already_implemented? attr
24
+ define_attribute_method attr
25
+ self.fields += [attr]
41
26
  end
42
- elsif attr.is_a?(Symbol) || attr.is_a?(String)
43
- set_field(attr)
44
27
  else
45
28
  raise Restly::Error::InvalidField, "field must be a symbol or string."
46
29
  end
@@ -63,7 +46,7 @@ module Restly::Base::Fields
63
46
  class FieldSet < Set
64
47
 
65
48
  def include?(value)
66
- super(value.to_sym)
49
+ super(value.to_sym) || super(value.to_s)
67
50
  end
68
51
 
69
52
  def <<(value)
@@ -15,6 +15,11 @@ module Restly::Base::Includes
15
15
  @client = client
16
16
  end
17
17
 
18
+ def has_specification
19
+ self.fields = Restly::Base::Resource::Specification.new(self).fields
20
+ (self._accessible_attributes ||= {})[:default] = Restly::Base::Resource::Specification.new(self).accessible_attributes
21
+ end
22
+
18
23
  def connection
19
24
  connection = @connection || Restly::Connection.tokenize(client, current_token)
20
25
  connection.cache ||= cache
@@ -60,7 +60,7 @@ module Restly::Base::Instance::Attributes
60
60
 
61
61
  def inspect
62
62
  inspection = if @attributes
63
- fields.collect { |name|
63
+ fields.map { |name|
64
64
  "#{name}: #{attribute_for_inspect(name)}"
65
65
  }.compact.join(", ")
66
66
  else
@@ -3,6 +3,7 @@ module Restly::Base::Resource
3
3
 
4
4
  autoload :Finders
5
5
  autoload :BatchActions
6
+ autoload :Specification
6
7
 
7
8
  include Restly::Base::GenericMethods
8
9
  include Finders
@@ -10,17 +11,6 @@ module Restly::Base::Resource
10
11
 
11
12
  delegate :first, :last, to: :all
12
13
 
13
- # OPTIONS FOR /:path
14
- # Fetches the spec of a remote resource
15
- def spec(path=self.path)
16
- begin
17
- parsed_response = authorize(client_token).connection.request(:options, path, params: params).parsed
18
- (parsed_response || {}).with_indifferent_access
19
- rescue OAuth2::Error
20
- raise Restly::Error::InvalidSpec, "Unable to load the specification for #{self.class}"
21
- end
22
- end
23
-
24
14
  def resource
25
15
  self
26
16
  end
@@ -0,0 +1,103 @@
1
+ class Restly::Base::Resource::Specification < HashWithIndifferentAccess
2
+
3
+ attr_reader :model, :fields, :accessible_attributes
4
+
5
+ def initialize(model)
6
+ @model = model
7
+ @fields = Fields.new(self)
8
+ @accessible_attributes = AccessibleAttributes.new(self)
9
+ end
10
+
11
+ def [](key)
12
+ reload! if super.nil?
13
+ super
14
+ end
15
+
16
+ def reload!
17
+ parsed_response = authorize(client_token).connection.request(:options, path).parsed
18
+ self.replace parsed_response if parsed_response.present?
19
+ rescue OAuth2::Error
20
+ false
21
+ end
22
+
23
+ def method_missing(method, *args, &block)
24
+ return model.send(method, *args, &block) if model.respond_to?(method)
25
+ super
26
+ end
27
+
28
+ def respond_to_missing?(method, include_private=false)
29
+ model.respond_to?(method)
30
+ end
31
+
32
+ module ReloadableSet
33
+
34
+ def include?(field)
35
+ reload! unless super
36
+ super
37
+ end
38
+
39
+ def inspect
40
+ reload! if empty?
41
+ super
42
+ end
43
+
44
+ def each(*args, &block)
45
+ reload! if empty?
46
+ super
47
+ end
48
+
49
+ def map(*args, &block)
50
+ reload! if empty?
51
+ super
52
+ end
53
+
54
+ def map!(*args, &block)
55
+ reload! if empty?
56
+ super
57
+ end
58
+
59
+ def reduce(*args, &block)
60
+ reload! if empty?
61
+ super
62
+ end
63
+
64
+ def reload!
65
+ replace []
66
+ self
67
+ end
68
+
69
+ end
70
+
71
+ class Fields < Restly::Base::Fields::FieldSet
72
+ include ReloadableSet
73
+
74
+ attr_reader :spec
75
+
76
+ def initialize(spec)
77
+ @spec = spec
78
+ super([])
79
+ end
80
+
81
+ def reload!
82
+ replace spec[:attributes]
83
+ end
84
+
85
+ end
86
+
87
+ class AccessibleAttributes < ActiveModel::MassAssignmentSecurity::WhiteList
88
+ include ReloadableSet
89
+
90
+ attr_reader :spec
91
+
92
+ def initialize(spec)
93
+ @spec = spec
94
+ super([])
95
+ end
96
+
97
+ def reload!
98
+ replace spec[:actions].map { |action| action['parameters'] }.flatten if spec[:actions].present?
99
+ end
100
+
101
+ end
102
+
103
+ end
data/lib/restly/error.rb CHANGED
@@ -15,6 +15,7 @@ module Restly::Error
15
15
  InvalidConnection
16
16
  MissingId
17
17
  InvalidSpec
18
+ InvalidField
18
19
  AssociationError
19
20
  }
20
21
 
@@ -1,3 +1,3 @@
1
1
  module Restly
2
- VERSION = "0.0.1.alpha.9"
2
+ VERSION = "0.0.1.alpha.10"
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.alpha.9
4
+ version: 0.0.1.alpha.10
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-08 00:00:00.000000000 Z
12
+ date: 2012-11-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: oauth2
@@ -193,10 +193,10 @@ files:
193
193
  - lib/restly/base/instance/attributes.rb
194
194
  - lib/restly/base/instance/persistence.rb
195
195
  - lib/restly/base/instance/write_callbacks.rb
196
- - lib/restly/base/mass_assignment_security.rb
197
196
  - lib/restly/base/resource.rb
198
197
  - lib/restly/base/resource/batch_actions.rb
199
198
  - lib/restly/base/resource/finders.rb
199
+ - lib/restly/base/resource/specification.rb
200
200
  - lib/restly/client.rb
201
201
  - lib/restly/collection.rb
202
202
  - lib/restly/collection/pagination.rb
@@ -1,19 +0,0 @@
1
- module Restly::Base::MassAssignmentSecurity
2
- extend ActiveSupport::Concern
3
-
4
- module ClassMethods
5
-
6
- def attr_accessible(*args)
7
- options = args.dup.extract_options!
8
- if options[:from_spec]
9
- before_initialize do
10
- self._accessible_attributes = spec[:actions].map { |action| action['parameters'] }.flatten
11
- end
12
- else
13
- super(*args)
14
- end
15
- end
16
-
17
- end
18
-
19
- end