restly 0.0.1.beta.1 → 0.0.1.beta.2

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.
@@ -1,4 +1,7 @@
1
1
  module Restly::Associations
2
+
3
+ ATTR_MATCHER = /(?<attr>\w+)(?<setter>=)?$/
4
+
2
5
  extend ActiveSupport::Concern
3
6
  extend ActiveSupport::Autoload
4
7
 
@@ -46,7 +49,7 @@ module Restly::Associations
46
49
  end
47
50
 
48
51
  def respond_to_association?(m)
49
- !!(/(?<attr>\w+)(?<setter>=)?$/ =~ m.to_s) && associations.include?(attr.to_sym)
52
+ (matched = ATTR_MATCHER.match m) && associations.include?(matched[:attr].to_sym)
50
53
  end
51
54
 
52
55
  def respond_to?(m, include_private = false)
@@ -85,9 +88,8 @@ module Restly::Associations
85
88
  end
86
89
 
87
90
  def association_missing(m, *args)
88
- if !!(/(?<attr>\w+)(?<setter>=)?$/ =~ m.to_s) && respond_to_association?(m)
89
- attr = attr.to_sym
90
- case !!setter
91
+ if (matched = ATTR_MATCHER.match m) && associations.include?(attr = matched[:attr].to_sym)
92
+ case !!matched[:setter]
91
93
  when true
92
94
  set_association(attr, *args)
93
95
  when false
@@ -32,16 +32,16 @@ module Restly
32
32
  # Concerned Inheritance
33
33
  include Restly::ConcernedInheritance
34
34
 
35
+ # Relationships
36
+ include Restly::Associations
37
+ include Restly::EmbeddedAssociations
38
+
35
39
  # Actions & Callbacks
36
40
  extend Resource
37
41
  include Includes
38
42
  include Instance
39
43
  include Fields
40
44
 
41
- # Relationships
42
- include Restly::Associations
43
- include Restly::EmbeddedAssociations
44
-
45
45
  # Set up the Attributes
46
46
  thread_local_accessor :current_token
47
47
  class_attribute :path, instance_writer: false, instance_reader: false
@@ -39,12 +39,14 @@ module Restly::Base::Instance
39
39
 
40
40
  @readonly = options[:readonly] || false
41
41
  set_response options[:response] if options[:response]
42
- @loaded = options.has_key?(:loaded) ? options[:loaded] : true
43
42
  self.attributes = attributes if attributes
43
+ @loaded = options.has_key?(:loaded) ? options[:loaded] : true
44
44
  self.connection = options[:connection] if options[:connection].is_a?(OAuth2::AccessToken)
45
45
 
46
46
  end
47
47
 
48
+ @initialized = true
49
+
48
50
  end
49
51
  end
50
52
 
@@ -52,6 +54,10 @@ module Restly::Base::Instance
52
54
  !!@loaded
53
55
  end
54
56
 
57
+ def initialized?
58
+ !!@initialized
59
+ end
60
+
55
61
  def connection
56
62
  @connection || resource.connection
57
63
  end
@@ -1,5 +1,7 @@
1
1
  module Restly::Base::Instance::Attributes
2
2
 
3
+ ATTR_MATCHER = /(?<attr>\w+)(?<setter>=)?$/
4
+
3
5
  def update_attributes(attributes)
4
6
  self.attributes = attributes
5
7
  save
@@ -19,7 +21,7 @@ module Restly::Base::Instance::Attributes
19
21
  end
20
22
 
21
23
  def attribute(key, options={})
22
- read_attribute(key)
24
+ read_attribute(key, options)
23
25
  end
24
26
 
25
27
  def []=(key, value)
@@ -35,7 +37,7 @@ module Restly::Base::Instance::Attributes
35
37
  end
36
38
 
37
39
  def respond_to_attribute?(m)
38
- !!(/(?<attr>\w+)(?<setter>=)?$/ =~ m.to_s) && fields.include?(attr)
40
+ (matched = ATTR_MATCHER.match m) && fields.include?(matched[:attr])
39
41
  end
40
42
 
41
43
  def respond_to?(m, include_private = false)
@@ -56,8 +58,8 @@ module Restly::Base::Instance::Attributes
56
58
  private
57
59
 
58
60
  def attribute_missing(m, *args)
59
- if !!(/(?<attr>\w+)(?<setter>=)?$/ =~ m.to_s) && fields.include?(attr)
60
- case !!setter
61
+ if (matched = ATTR_MATCHER.match m) && fields.include?(attr = matched[:attr].to_sym)
62
+ case !!matched[:setter]
61
63
  when true
62
64
  write_attribute(attr, *args)
63
65
  when false
@@ -76,8 +78,8 @@ module Restly::Base::Instance::Attributes
76
78
 
77
79
  def write_attribute(attr, val)
78
80
  if fields.include?(attr)
79
- send("#{attr}_will_change!".to_sym) unless val == @attributes[attr.to_sym] || !@loaded
80
- @attributes[attr.to_sym] = Attribute.new(val)
81
+ send("#{attr}_will_change!") if val != read_attribute(attr) && loaded?
82
+ @attributes[attr.to_sym] = val
81
83
 
82
84
  else
83
85
  ActiveSupport::Notifications.instrument("missing_attribute.restly", attr: attr)
@@ -86,7 +88,12 @@ module Restly::Base::Instance::Attributes
86
88
 
87
89
  def read_attribute(attr, options={})
88
90
  options.reverse_merge!({ autoload: true })
89
- load! if (key = attr.to_sym) != :id && @attributes[key].nil? && !!options[:autoload] && !loaded? && !exists?
91
+
92
+ # Try and get the attribute if the item is not loaded
93
+ if initialized? && attr.to_sym != :id && @attributes[attr].nil? && !!options[:autoload] && !loaded? && exists?
94
+ load!
95
+ end
96
+
90
97
  @attributes[attr.to_sym]
91
98
  end
92
99
 
@@ -103,37 +110,4 @@ module Restly::Base::Instance::Attributes
103
110
  self.attributes = parsed_response(response)
104
111
  end
105
112
 
106
- class Attribute < Restly::Proxies::Base
107
-
108
- def initialize(attr)
109
- @attr = attr
110
- case @attr
111
- when String
112
- type_convert_string
113
- end
114
- super(@attr)
115
- end
116
-
117
- private
118
-
119
- def type_convert_string
120
- time = (@attr.to_time rescue nil)
121
- date = (@attr.to_date rescue nil)
122
- # int = (@attr.to_i rescue nil)
123
- # flt = (@attr.to_f rescue nil)
124
- @attr = if time.try(:iso8601) == @attr
125
- time
126
- elsif date.try(:to_s) == @attr
127
- date
128
- #elsif int.try (:to_s) == @attr
129
- # int
130
- #elsif flt.try (:to_s) == @attr
131
- # flt
132
- else
133
- @attr
134
- end
135
- end
136
-
137
- end
138
-
139
113
  end
@@ -25,7 +25,8 @@ module Restly::Base::Instance::Persistence
25
25
  end
26
26
 
27
27
  def reload!
28
- raise Restly::Error::MissingId, "Cannot reload #{resource_name}, either it hasn't been created or it is missing an ID." unless new_record?
28
+ return unless initialized?
29
+ raise Restly::Error::MissingId, "Cannot reload #{resource_name}, either it hasn't been created or it is missing an ID." unless exists?
29
30
  set_attributes_from_response connection.get(path_with_format, force: true)
30
31
  @loaded = true
31
32
  self
@@ -64,7 +64,7 @@ class Restly::Connection < OAuth2::AccessToken
64
64
  alias_method :forced_request, :request
65
65
 
66
66
  def request(verb, path, opts={}, &block)
67
- path = [base_path, path.gsub(/^\/?/, '')].join('/')
67
+ path = [base_path.gsub(/\/?$/, ''), path.gsub(/^\/?/, '')].join('/')
68
68
 
69
69
  if cache && !opts[:force]
70
70
  request_log("Restly::CacheRequest", path, verb) do
@@ -1,5 +1,7 @@
1
1
  module Restly::NestedAttributes
2
2
 
3
+ ATTR_MATCHER = /(?<attr>\w+)_attributes=$/
4
+
3
5
  extend ActiveSupport::Concern
4
6
 
5
7
  included do
@@ -65,7 +67,7 @@ module Restly::NestedAttributes
65
67
  end
66
68
 
67
69
  def nested_attribute_missing(m, *args)
68
- if !!(/(?<attr>\w+)_attributes=$/ =~ m.to_s) && (options = resource_nested_attributes_options[attr])
70
+ if !!(matched = ATTR_MATCHER.match m) && (options = resource_nested_attributes_options[(attr = matched[:attr])])
69
71
  send( "assign_nested_attributes_for_#{options[:association_type]}_resource_association", attr, *args )
70
72
  else
71
73
  raise Restly::Error::InvalidNestedAttribute, "Nested Attribute does not exist!"
@@ -1,3 +1,3 @@
1
1
  module Restly
2
- VERSION = "0.0.1.beta.1"
2
+ VERSION = "0.0.1.beta.2"
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.1
4
+ version: 0.0.1.beta.2
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors: