arrest 0.0.89 → 0.0.90

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,7 @@
1
1
  require 'logger'
2
2
 
3
3
  require "arrest/version"
4
+ require "arrest/default_class_loader"
4
5
  require 'arrest/helper/logger'
5
6
 
6
7
  require "arrest/utils/class_utils.rb"
@@ -16,7 +16,8 @@ module Arrest
16
16
  include HasAttributes
17
17
  include HasMany
18
18
  include HasView
19
- attribute :id, String
19
+
20
+ attribute :id, :String
20
21
 
21
22
  attr_accessor :context
22
23
 
@@ -9,17 +9,28 @@ module Arrest
9
9
 
10
10
 
11
11
  class Attribute
12
- attr_accessor :name, :actions, :clazz, :json_name, :dirty
12
+ attr_accessor :name, :actions, :json_name, :dirty
13
13
 
14
- def initialize(name, clazz, actions = nil)
14
+ def initialize(name, class_name, actions = nil)
15
15
  @name = name.to_sym
16
16
  @actions = actions || [:create, :retrieve, :update, :delete]
17
- @clazz = clazz
18
- @dirty_sensitive = @clazz.ancestors.include?(Dirty)
17
+ @class_name = class_name.to_sym
19
18
  @dirty = false
20
19
  @json_name = Source.json_key_converter.key_to_json(name).to_sym
21
20
  end
22
21
 
22
+ def clazz
23
+ @clazz ||= Arrest::Source.class_loader.load(@class_name)
24
+ end
25
+
26
+ def clazz=(c)
27
+ @clazz = c
28
+ end
29
+
30
+ def dirty_sensitive?
31
+ @dirty_sensitive ||= clazz.ancestors.include?(Dirty)
32
+ end
33
+
23
34
  def read_only?
24
35
  @actions == [:retrieve]
25
36
  end
@@ -29,7 +40,7 @@ module Arrest
29
40
  end
30
41
 
31
42
  def dirty?
32
- if @dirty_sensitive
43
+ if dirty_sensitive?
33
44
  @dirty
34
45
  else
35
46
  true # treat as 'always' dirty
@@ -39,13 +50,13 @@ module Arrest
39
50
  def from_hash(parent, value)
40
51
  return if value == nil
41
52
 
42
- if @clazz.respond_to?(:convert)
43
- return @clazz.convert(value)
53
+ if self.clazz.respond_to?(:convert)
54
+ return self.clazz.convert(value)
44
55
  end
45
56
 
46
- converter = CONVERTER[@clazz]
57
+ converter = CONVERTER[self.clazz]
47
58
  if converter == nil
48
- puts "No converter for: #{@clazz.name}"
59
+ puts "No converter for: #{self.clazz.name}"
49
60
  converter = IdentConv
50
61
  end
51
62
  converter.convert value
@@ -55,13 +66,13 @@ module Arrest
55
66
  def to_hash value
56
67
  return nil unless value != nil
57
68
 
58
- if @clazz.respond_to?(:mk_json)
59
- return @clazz.mk_json(value)
69
+ if self.clazz.respond_to?(:mk_json)
70
+ return self.clazz.mk_json(value)
60
71
  end
61
72
 
62
- converter = CONVERTER[@clazz]
73
+ converter = CONVERTER[self.clazz]
63
74
  if converter == nil
64
- puts "No converter for: #{@clazz.name}"
75
+ puts "No converter for: #{@self.clazz.name}"
65
76
  converter = IdentConv
66
77
  end
67
78
  converter.mk_json value
@@ -21,7 +21,7 @@ module Arrest
21
21
  if polymorphic
22
22
  add_attribute(PolymorphicAttribute.new(field_name.to_sym, actions))
23
23
  else
24
- add_attribute(BelongsToAttribute.new(field_name.to_sym, actions, String, foreign_key, class_name))
24
+ add_attribute(BelongsToAttribute.new(field_name.to_sym, actions, :String, foreign_key, class_name))
25
25
  end
26
26
  end
27
27
 
@@ -60,7 +60,7 @@ module Arrest
60
60
  clazz = self.class.json_type_to_class(val.type)
61
61
  id = val.id
62
62
  else
63
- clazz = Arrest::Source.mod.const_get(class_name)
63
+ clazz = Arrest::Source.class_loader.load(class_name)
64
64
  id = val
65
65
  end
66
66
  clazz.find(self.context, id)
@@ -1,13 +1,15 @@
1
1
  module Arrest
2
2
  class BelongsToAttribute < Attribute
3
3
  attr_accessor :foreign_key
4
- def initialize(name, actions, field_class, foreign_key, target_class_name)
5
- super(name, field_class, actions)
4
+
5
+ def initialize(name, actions, field_class_name, foreign_key, target_class_name)
6
+ super(name, field_class_name, actions)
6
7
  @foreign_key = foreign_key
7
8
  @target_class_name = target_class_name
8
9
  end
10
+
9
11
  def target_class
10
- @target_class ||= Arrest::Source.mod.const_get(@target_class_name)
12
+ @target_class ||= Arrest::Source.class_loader.load(@target_class_name)
11
13
  end
12
14
  end
13
15
  end
@@ -82,6 +82,7 @@ module Arrest
82
82
  def attributes=(attribute_hash = {})
83
83
  fields = self.class.all_fields
84
84
  field_names = fields.map(&:name)
85
+
85
86
  attribute_hash.each_pair do |k, v|
86
87
  matching_fields = fields.find_all{|f| f.name.to_s == k.to_s}
87
88
  field = matching_fields.first
@@ -142,13 +143,13 @@ module Arrest
142
143
  @fields = []
143
144
  end
144
145
 
145
- def attribute(name, clazz, attribs = {})
146
+ def attribute(name, class_name, attribs = {})
146
147
  if !!attribs[:read_only] && !attribs[:actions]
147
148
  actions = [:retrieve]
148
149
  else
149
150
  actions = attribs[:actions]
150
151
  end
151
- add_attribute Attribute.new(name, clazz, actions)
152
+ add_attribute Attribute.new(name, class_name, actions)
152
153
  end
153
154
 
154
155
  def attributes(args)
@@ -182,12 +183,12 @@ module Arrest
182
183
  self.ancestors.select{|a| a.include?(HasAttributes)}.map(&:fields).flatten.reject{|f| f == nil}
183
184
  end
184
185
 
185
- def nested name, clazz, options = {}
186
- add_attribute NestedAttribute.new(name, clazz, options[:actions])
186
+ def nested name, class_name, options = {}
187
+ add_attribute NestedAttribute.new(name, class_name, options[:actions])
187
188
  end
188
189
 
189
- def nested_array name, clazz, options = {}
190
- add_attribute Arrest::NestedCollection.new(name, clazz, options[:actions])
190
+ def nested_array name, class_name, options = {}
191
+ add_attribute Arrest::NestedCollection.new(name, class_name, options[:actions])
191
192
  end
192
193
  end
193
194
 
@@ -1,12 +1,13 @@
1
1
  module Arrest
2
- class NestedAttribute < Attribute
3
- def initialize name, clazz, options
4
- super name, clazz, options
2
+ class NestedAttribute < Attribute
3
+
4
+ def initialize name, class_name, options
5
+ super name, class_name, options
5
6
  end
6
7
 
7
8
  def from_hash(parent, value)
8
9
  return nil unless value != nil
9
- @clazz.new(parent, value)
10
+ self.clazz.new(parent, value)
10
11
  end
11
12
 
12
13
  def to_hash val
@@ -1,15 +1,15 @@
1
1
  module Arrest
2
2
 
3
3
  class NestedCollection < Attribute
4
- def initialize name, clazz, options
5
- super name, clazz, options
4
+ def initialize name, class_name, options
5
+ super name, class_name, options
6
6
  end
7
7
 
8
8
  def from_hash(parent, value)
9
9
  return nil unless value != nil
10
10
  raise "Expected an array but got #{value.class.name}" unless value.is_a?(Array)
11
11
  value.map do |v|
12
- @clazz.new(parent.context, v)
12
+ self.clazz.new(parent.context, v)
13
13
  end
14
14
  end
15
15
 
@@ -2,8 +2,8 @@ module Arrest
2
2
  class Ref
3
3
  include HasAttributes
4
4
 
5
- attribute :id, String
6
- attribute :type, String
5
+ attribute :id, :String
6
+ attribute :type, :String
7
7
 
8
8
  def self.mk_json(value)
9
9
  self.to_hash.to_json
@@ -15,13 +15,14 @@ module Arrest
15
15
  end
16
16
 
17
17
  class PolymorphicAttribute < NestedAttribute
18
+
18
19
  def initialize name, actions
19
- super name, Ref, actions
20
+ super name, :'Ref', actions
20
21
  end
21
22
 
22
23
  def from_hash(parent, value)
23
24
  return nil unless value != nil
24
- @clazz.new(value)
25
+ self.clazz.new(value)
25
26
  end
26
27
  end
27
28
  end
@@ -0,0 +1,40 @@
1
+ module Arrest
2
+
3
+ ##
4
+ # Classloader that specifies the deferred class loading strategy for the user. It is registered as default class loader
5
+ # in the Arrest::Source.
6
+ # The default implementation tries to load the class specified by the given symbol parameter in the following order:
7
+ # 1) from the given module utilising Arrest (in our case SGDB)
8
+ # 2) from Arrest itself (e.g. :Ref)
9
+ # 3) from the Kernel (for all basic types - String, Integer etc)
10
+ class DefaultClassLoader
11
+
12
+ def load(sym)
13
+ # Using const_get is effectively a hack - it uses the fact that class names are also constants to allow you to get hold of them.
14
+ # Better use eval if possible
15
+
16
+ clazz =
17
+ begin
18
+ eval("#{Source.mod.to_s}::#{sym}") unless Source.mod == Kernel
19
+ rescue NameError
20
+ end
21
+
22
+
23
+ clazz ||=
24
+ begin
25
+ eval("Arrest::#{sym}")
26
+ rescue NameError
27
+ end
28
+
29
+ clazz ||=
30
+ begin
31
+ Kernel.const_get(sym)
32
+ rescue NameError
33
+ end
34
+
35
+ raise "Class #{sym} could not be loaded! Tried module if given, with fallback Arrest and Kernel" unless clazz
36
+ clazz
37
+ end
38
+ end
39
+
40
+ end
@@ -22,7 +22,7 @@ module Arrest
22
22
  r = self.class.source().get(self.context, "#{self.resource_path}/#{id}/#{method_name}")
23
23
  r = self.class.body_root(r)
24
24
 
25
- Arrest::Source.mod.const_get(clazz).new(self.context, r)
25
+ Arrest::Source.class_loader.load.new(self.context, r)
26
26
  end
27
27
  end
28
28
  end
@@ -155,7 +155,7 @@ module Arrest
155
155
  end
156
156
 
157
157
  def resolved_class
158
- @clazz ||= Source.mod.const_get(@clazz_name)
158
+ @clazz ||= Source.class_loader.load(@clazz_name)
159
159
  end
160
160
 
161
161
  def define_filters
@@ -12,6 +12,7 @@ module Arrest
12
12
  attr_reader :source
13
13
  attr_reader :mod
14
14
  attr_reader :header_decorator
15
+ attr_accessor :class_loader
15
16
  attr_accessor :json_key_converter
16
17
  attr_accessor :skip_validations
17
18
  attr_accessor :error_handler
@@ -38,6 +39,12 @@ module Arrest
38
39
  end
39
40
  end
40
41
 
42
+
43
+ def load_class_from_symbol(sym)
44
+ self.class_loader.load(sym)
45
+ end
46
+
47
+
41
48
  def header_decorator=(hd=nil)
42
49
  Arrest::debug "Setting headerd to #{hd}"
43
50
  if hd == nil
@@ -50,7 +57,9 @@ module Arrest
50
57
  end
51
58
  end
52
59
  end
60
+
53
61
  Source.mod = nil
62
+ Source.class_loader = DefaultClassLoader.new
54
63
  Source.header_decorator = Handlers::HeaderDecorator
55
64
  Source.debug = false
56
65
  Source.json_key_converter = Handlers::IdentityJSONKeyConverter
@@ -1,3 +1,3 @@
1
1
  module Arrest
2
- VERSION = "0.0.89"
2
+ VERSION = "0.0.90"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.89
4
+ version: 0.0.90
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-17 00:00:00.000000000Z
12
+ date: 2012-10-25 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &10475620 !ruby/object:Gem::Requirement
16
+ requirement: &16628380 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *10475620
24
+ version_requirements: *16628380
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: faraday
27
- requirement: &10473340 !ruby/object:Gem::Requirement
27
+ requirement: &16627880 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.7.5
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *10473340
35
+ version_requirements: *16627880
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activemodel
38
- requirement: &10472360 !ruby/object:Gem::Requirement
38
+ requirement: &16627380 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '3'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *10472360
46
+ version_requirements: *16627380
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &10471280 !ruby/object:Gem::Requirement
49
+ requirement: &16626920 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.0.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *10471280
57
+ version_requirements: *16626920
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &10470600 !ruby/object:Gem::Requirement
60
+ requirement: &16626540 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *10470600
68
+ version_requirements: *16626540
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rdoc
71
- requirement: &10469500 !ruby/object:Gem::Requirement
71
+ requirement: &16626080 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *10469500
79
+ version_requirements: *16626080
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &10468060 !ruby/object:Gem::Requirement
82
+ requirement: &16625580 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '2'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *10468060
90
+ version_requirements: *16625580
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rr
93
- requirement: &10467000 !ruby/object:Gem::Requirement
93
+ requirement: &16625160 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *10467000
101
+ version_requirements: *16625160
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: simplecov
104
- requirement: &10465960 !ruby/object:Gem::Requirement
104
+ requirement: &16624700 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *10465960
112
+ version_requirements: *16624700
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: rack
115
- requirement: &10454880 !ruby/object:Gem::Requirement
115
+ requirement: &16645840 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,7 +120,7 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *10454880
123
+ version_requirements: *16645840
124
124
  description: Consume a rest API in a AR like fashion
125
125
  email:
126
126
  - axel.tetzlaff@fortytools.com
@@ -144,6 +144,7 @@ files:
144
144
  - lib/arrest/attributes/nested_attribute.rb
145
145
  - lib/arrest/attributes/nested_collection.rb
146
146
  - lib/arrest/attributes/polymorphic_attribute.rb
147
+ - lib/arrest/default_class_loader.rb
147
148
  - lib/arrest/exceptions.rb
148
149
  - lib/arrest/handler.rb
149
150
  - lib/arrest/helper/filter.rb