cyrax 0.6.0.beta → 0.6.0

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MzJlYTg3MDkzYjVjMzJhODVmODAwNTllMzVlM2Q2Y2Q4MDQ2NjUyNQ==
4
+ Yjc1NmExZWEyMjNhOWJhNzQ0Nzc2ZjU2Njg2NTE4YjYwNWFhNTI0Nw==
5
5
  data.tar.gz: !binary |-
6
- ODdlMjE1MGIxMzRkZTc1MmExMTFkZWUyYThlZGViM2RiNjEwYjZmZQ==
6
+ ODA0NjA2MGE1NjFkYjZkNzMzZGI4OTFjNzRiMDQ0NzFlYTM0MTRkNQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YzE2M2IzNjRmZTAzOGY2MjNlMmJhYzg0NzM2YjFhYjFhZTljNTZlYTMyMTE5
10
- YjE0OGQ2ZjE3Y2IzYWIxMTg2MTA2MjFmMzdlYjhhNGFkN2IxOGQ1YzFlNWVi
11
- YmY1ODAyM2FkNjdlODcxYTNmYjk3NjcyOGRhYWQxYjdlYjZlMjc=
9
+ ZTEyMzA0M2Y2NGJlMzIwOWM0NDZiMTYxNmU2MTg5OGE3OGUxMzc2OWVlMTZl
10
+ N2FlMTM0ZGRiYjVjYmRlY2NmNjM5ZGRmZDJlZjEzMjdhYjhlMTkzZTc1ZGEw
11
+ MGNhMmQyMzE5ZGZjMjIxNzM2MzE2NWZhOTkyMmFlNjNlYTBiN2Y=
12
12
  data.tar.gz: !binary |-
13
- NWM1NzBjNDEwM2E2NDc4ZGEyNmZhNGY3MTRiN2YyNThmYzQ3NzU1OWY5Mjg3
14
- NzE2ZTEyZWEyMDAyNWU3NDE4MzRhYjNkOTI2NDRiMDFjZTQxNjBkMjU4NmQy
15
- ZjA4ZWMzZmUwMjQ4MjJiNzgxYTY2NmZkNDYzMjhjNThkODcyNjQ=
13
+ Y2NkMmNjMWNlOGQ2N2QzYjdhOWMwZmU2NmUyOWZhZjJhM2ZkM2RkZjk1OWE2
14
+ MTVjOGY5YjBmMWYzZTcwMTZlNGMxZWFhODQ4ZTYyMDlhOGUxMWViN2ZlOGIz
15
+ NzExNzMzMjU2OWI3OTYyMDc2NTk0YjgzNmQ1YjdkNWVkYWUzZTI=
@@ -33,9 +33,14 @@ module Cyrax::Extensions
33
33
  self._repository_class = klass
34
34
  end
35
35
  if block_given?
36
+ # temporary workaround. we should not use one object hash for parent and childrens
37
+ if self._repository_options.present?
38
+ self._repository_options = self._repository_options.dup
39
+ end
40
+
36
41
  self._repository_options ||= {}
37
- self._repository_options[:finder_blocks] = {}
38
- self._repository_options[:finder_blocks][finder_name || :scope] = block
42
+ self._repository_options[:finders] = {}
43
+ self._repository_options[:finders][finder_name || :scope] = block
39
44
  end
40
45
  end
41
46
  end
@@ -107,18 +107,23 @@ module Cyrax::Extensions
107
107
 
108
108
  # DEPRECATED METHODS
109
109
  def resource_scope
110
+ ActiveSupport::Deprecation.warn "#resource_scope inside Resource deprecated. Use repository.scope instead."
110
111
  respository.scope
111
112
  end
112
113
  def build_collection
114
+ ActiveSupport::Deprecation.warn "#build_collection inside Resource deprecated. Use repository.find_all instead."
113
115
  respository.find_all
114
116
  end
115
117
  def find_resource(id)
118
+ ActiveSupport::Deprecation.warn "#find_resource inside Resource deprecated. Use repository.find instead."
116
119
  respository.find(id)
117
120
  end
118
121
  def save_resource(resource)
122
+ ActiveSupport::Deprecation.warn "#save_resource inside Resource deprecated. Use repository.save instead."
119
123
  respository.save(resource)
120
124
  end
121
125
  def delete_resource(resource)
126
+ ActiveSupport::Deprecation.warn "#delete_resource inside Resource deprecated. Use repository.delete instead."
122
127
  respository.delete(resource)
123
128
  end
124
129
  end
@@ -4,14 +4,27 @@ class Cyrax::Repository
4
4
  attr_accessor :accessor
5
5
  attr_accessor :params
6
6
  attr_accessor :resource_class
7
- attr_accessor :finder_blocks
7
+ attr_accessor :finders
8
8
 
9
9
  def initialize(options = {})
10
10
  @options = options
11
11
  @accessor = options[:as]
12
12
  @params = options[:params]
13
13
  @resource_class = options[:resource_class]
14
- @finder_blocks = options[:finder_blocks] || {}
14
+ @finders = options[:finders] || {}
15
+ end
16
+
17
+ def finder(name, *attrs)
18
+ block = finders[name]
19
+ instance_exec(*attrs, &block)
20
+ end
21
+
22
+ def finder?(name)
23
+ finders.has_key?(name)
24
+ end
25
+
26
+ def finder_or_run(name, *attrs)
27
+ finder?(name) ? finder(name, *attrs) : send("#{name}!", *attrs)
15
28
  end
16
29
 
17
30
  # Returns the resource class - e.g. Product by default.
@@ -25,11 +38,10 @@ class Cyrax::Repository
25
38
  # end
26
39
  # end
27
40
  def scope
28
- if block = finder_blocks[:scope]
29
- instance_exec(&block)
30
- else
31
- resource_class
32
- end
41
+ finder_or_run(:scope)
42
+ end
43
+ def scope!
44
+ resource_class
33
45
  end
34
46
 
35
47
  # Instantiates the resource
@@ -37,66 +49,60 @@ class Cyrax::Repository
37
49
  # @param attributes [hash] Attributes you want to add to the resource
38
50
  # @return [object] The object
39
51
  def build(id, attributes = {})
40
- if block = finder_blocks[:build]
41
- instance_exec(id, attributes, &block)
52
+ finder_or_run(:build, id, attributes)
53
+ end
54
+ def build!(id, attributes = {})
55
+ if id.present?
56
+ resource = find(id)
57
+ resource.attributes = attributes
58
+ resource
42
59
  else
43
- if id.present?
44
- resource = find(id)
45
- resource.attributes = attributes
46
- resource
47
- else
48
- scope.new(default_attributes.merge(attributes))
49
- end
60
+ scope.new(default_attributes.merge(attributes))
50
61
  end
51
62
  end
52
63
 
53
64
  # Finds and returns a multiple items within the scope from the DB
54
65
  # @return [Array] Array of objects
55
66
  def find_all
56
- if block = finder_blocks[:find_all]
57
- instance_exec(&block)
58
- else
59
- scope.is_a?(ActiveRecord::Relation) ? scope.load : scope.all
60
- end
67
+ finder_or_run(:find_all)
68
+ end
69
+ def find_all!
70
+ defined?(ActiveRecord) && scope.is_a?(ActiveRecord::Relation) ? scope.load : scope.all
61
71
  end
62
72
 
63
73
  # Finds and returns a single item from the DB
64
74
  # @param id [int] ID of item
65
75
  # @return [object] The object
66
76
  def find(id)
67
- if block = finder_blocks[:find]
68
- instance_exec(id, &block)
69
- else
70
- scope.find(id)
71
- end
77
+ finder_or_run(:find, id)
78
+ end
79
+ def find!(id)
80
+ scope.find(id)
72
81
  end
73
82
 
74
83
  # Saves a resource
75
84
  # @param resource [object] The resource to save
76
85
  def save(resource)
77
- if block = finder_blocks[:save]
78
- instance_exec(resource, &block)
79
- else
80
- resource.save
81
- end
86
+ finder_or_run(:save, resource)
87
+ end
88
+ def save!(resource)
89
+ resource.save
82
90
  end
83
91
 
84
92
  # Removes a resource
85
93
  # Calls destroy method on resource
86
94
  # @param resource [object] The resource to destroy
87
95
  def delete(resource)
88
- if block = finder_blocks[:delete]
89
- instance_exec(resource, &block)
90
- else
91
- resource.destroy
92
- end
96
+ finder_or_run(:delete, resource)
97
+ end
98
+ def delete!(resource)
99
+ resource.destroy
93
100
  end
94
101
 
95
102
  def default_attributes
96
- if block = finder_blocks[:default_attributes]
97
- instance_exec(&block)
98
- else
99
- {}
100
- end
103
+ finder_or_run(:default_attributes)
104
+ end
105
+ def default_attributes!
106
+ {}
101
107
  end
102
108
  end
@@ -1,3 +1,3 @@
1
1
  module Cyrax
2
- VERSION = "0.6.0.beta"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cyrax
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0.beta
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Droidlabs
@@ -136,9 +136,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
136
136
  version: '0'
137
137
  required_rubygems_version: !ruby/object:Gem::Requirement
138
138
  requirements:
139
- - - ! '>'
139
+ - - ! '>='
140
140
  - !ruby/object:Gem::Version
141
- version: 1.3.1
141
+ version: '0'
142
142
  requirements: []
143
143
  rubyforge_project:
144
144
  rubygems_version: 2.0.5