kelredd-resourceful 0.6.7 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -113,6 +113,23 @@ module Resourceful
113
113
  end
114
114
  end
115
115
 
116
+ # Returns the value for the provided key(s). Allows searching in nested hashes
117
+ unless {}.respond_to?(:get_value)
118
+ def get_value(*keys)
119
+ val = self[keys.first] || self[keys.first.to_s]
120
+ val = self[keys.first.to_s.intern] unless val || keys.first.to_s.empty? || keys.first.kind_of?(Symbol)
121
+ val.kind_of?(Hash) && keys.length > 1 ? val.get_value?(keys[1..-1]) : val
122
+ end
123
+ end
124
+
125
+ # Determines if a value exists for the provided key(s). Allows searching in nested hashes
126
+ unless {}.respond_to?('check_value?')
127
+ def check_value?(*keys)
128
+ val = self.get_value(keys)
129
+ val && !val.empty? ? true : false
130
+ end
131
+ end
132
+
116
133
  end
117
134
  end
118
135
  end
@@ -8,6 +8,22 @@ module Resourceful
8
8
  @@agent[self.name.to_s] = block;
9
9
  end
10
10
 
11
+ @@namespaces = [""]
12
+ def self.add_namespace(ns)
13
+ @@namespaces.unshift(ns)
14
+ end
15
+ def self.get_namespaced_klass(class_name)
16
+ klass = nil
17
+ @@namespaces.each do |ns|
18
+ begin
19
+ klass = "#{ns}::#{class_name}".constantize
20
+ break;
21
+ rescue Exception => err
22
+ end
23
+ end
24
+ klass
25
+ end
26
+
11
27
  def self.get(path, opts={})
12
28
  block = opts.delete(:on_response)
13
29
  set_agent.get(path, opts, &block)
@@ -45,7 +61,7 @@ module Resourceful
45
61
 
46
62
  def update_attributes(attr_hash={})
47
63
  attr_hash.each do |k,v|
48
- self.send("#{key}=", v)
64
+ self.send("#{k}=", v)
49
65
  end
50
66
  attributes(true)
51
67
  end
@@ -170,6 +186,10 @@ module Resourceful
170
186
  @@agent[klass_name]
171
187
  end
172
188
 
189
+ def self.add_namespace(ns)
190
+ @@namespaces.unshift(ns)
191
+ end
192
+
173
193
  end
174
194
 
175
195
  end
@@ -10,11 +10,15 @@ module Resourceful
10
10
  clean_name = cleanup_name(name)
11
11
  config ||= {}
12
12
  config[:path] ||= clean_name
13
+ raise ArgumentError, "contains_one requires a :class option to be specified" unless config[:class]
14
+ class_name = config.delete(:class).to_s
13
15
  define_method(name) do
16
+ klass = self.class.get_namespaced_klass(class_name)
17
+ raise ArgumentError, "contains_one :class '#{class_name}' is not defined in any given namespaces" if klass.nil?
14
18
  instance_variable_get("@#{clean_name}") || \
15
19
  instance_variable_set("@#{clean_name}", \
16
20
  if (c = child(config))
17
- config[:class].constantize.new(c) rescue c
21
+ klass.new(c) rescue c
18
22
  else
19
23
  c
20
24
  end
@@ -26,12 +30,16 @@ module Resourceful
26
30
  clean_name = cleanup_name(name)
27
31
  config ||= {}
28
32
  config[:path] ||= clean_name
33
+ raise ArgumentError, "contains_many requires a :class option to be specified" unless config[:class]
34
+ class_name = config.delete(:class).to_s
29
35
  define_method(name) do
36
+ klass = self.class.get_namespaced_klass(class_name)
37
+ raise ArgumentError, "contains_many :class '#{class_name}' is not defined in any given namespaces" if klass.nil?
30
38
  instance_variable_get("@#{clean_name}") || \
31
39
  instance_variable_set("@#{clean_name}", \
32
40
  if ((c = child(config)) && c.respond_to?(:collect))
33
41
  c.collect do |item|
34
- config[:class].constantize.new(item) rescue item
42
+ klass.new(item) rescue item
35
43
  end
36
44
  else
37
45
  c
@@ -14,10 +14,12 @@ module Resourceful
14
14
  clean_name = cleanup_name(name)
15
15
  config ||= {}
16
16
  raise ArgumentError, "has_many requires a :class option to be specified" unless config[:class]
17
- klass = config.delete(:class)
17
+ class_name = config.delete(:class).to_s
18
18
  force = config.delete(:force) || true
19
19
  define_method(name) do
20
- unless klass.to_s.constantize.respond_to?(:find)
20
+ klass = self.class.get_namespaced_klass(class_name)
21
+ raise ArgumentError, "has_many :class '#{class_name}' is not defined in any given namespaces" if klass.nil?
22
+ unless klass.respond_to?(:find)
21
23
  raise NotImplementedError, "has_many expects #{klass} to be findable (ie mixin the Findable helper)"
22
24
  end
23
25
  fk = config.delete(:foreign_key) || "#{self.class.name.downcase.to_s.gsub(/^.*::/, '')}_id"
@@ -25,7 +27,7 @@ module Resourceful
25
27
  config[fk] = self.send(fk_method)
26
28
  instance_variable_get("@#{clean_name}") || \
27
29
  instance_variable_set("@#{clean_name}", \
28
- klass.to_s.constantize.find(:all, config, force)
30
+ klass.find(:all, config, force)
29
31
  )
30
32
  end
31
33
  end
@@ -33,15 +35,17 @@ module Resourceful
33
35
  def belongs_to(name, config={})
34
36
  clean_name = cleanup_name(name)
35
37
  config ||= {}
36
- raise ArgumentError, "has_many requires a :class option to be specified" unless config[:class]
37
- klass = config.delete(:class)
38
+ raise ArgumentError, "belongs_to requires a :class option to be specified" unless config[:class]
39
+ class_name = config.delete(:class).to_s
38
40
  foreign_key = config.delete(:foreign_key) || "#{clean_name}_id"
39
41
  force = config.delete(:force) || true
40
42
  define_method(name) do
43
+ klass = self.class.get_namespaced_klass(class_name)
44
+ raise ArgumentError, "belongs_to :class '#{class_name}' is not defined in any given namespaces" if klass.nil?
41
45
  unless self.respond_to?(foreign_key)
42
46
  raise ArgumentError, "belongs_to requires a '#{foreign_key}' method defined to return the foreign_key"
43
47
  end
44
- unless klass.to_s.constantize.respond_to?(:find)
48
+ unless klass.respond_to?(:find)
45
49
  raise NotImplementedError, "has_many expects #{klass} to be findable (ie mixin the Findable helper)"
46
50
  end
47
51
  fk = self.send(foreign_key)
@@ -50,7 +54,7 @@ module Resourceful
50
54
  else
51
55
  instance_variable_get("@#{clean_name}") || \
52
56
  instance_variable_set("@#{clean_name}", \
53
- klass.to_s.constantize.find(fk, config, force)
57
+ klass.find(fk, config, force)
54
58
  )
55
59
  end
56
60
  end
@@ -9,11 +9,11 @@ module Resourceful
9
9
  opts = findable_default_opts.merge(opts) if respond_to?(:findable_default_opts)
10
10
  case id
11
11
  when :all
12
- self.get_collection("#{findable_index}.#{format}", opts, force)
12
+ self.get_collection("#{findable_index}.#{format}", opts, findable_search, force)
13
13
  when :first
14
- self.get_collection("#{findable_index}.#{format}", opts, force).first
14
+ self.get_collection("#{findable_index}.#{format}", opts, findable_search, force).first
15
15
  else
16
- self.get("#{findable_index}/#{id}.#{format}", opts, force)
16
+ self.get("#{findable_index}/#{id}.#{format}", opts, findable_search, force)
17
17
  end
18
18
  end
19
19
 
@@ -28,6 +28,11 @@ module Resourceful
28
28
  raise NotImplementedError, "Findable expects a public class method 'findable_index'"
29
29
  end
30
30
 
31
+ def findable_search
32
+ # defaults to non namespaced downcased name of the class
33
+ self.name.downcase.to_s.gsub(/^.*::/, '')
34
+ end
35
+
31
36
  end
32
37
 
33
38
  module InstanceMethods
@@ -7,24 +7,27 @@ module Resourceful
7
7
 
8
8
  attr_reader :json
9
9
 
10
- def self.get(path, params, force=false, &block)
10
+ def self.get(path, params, search=nil, force=false, &block)
11
11
  opts = {
12
12
  :format => 'json',
13
13
  :params => params || {},
14
14
  :force => force,
15
15
  :on_response => block
16
16
  }
17
- new(super(path, opts))
17
+ result = super(path, opts)
18
+ hsh_keys = search.kind_of?(String) ? search.split(/\s+/) : nil
19
+ new(!hsh_keys.nil? && !hsh_keys.empty? && result.respond_to?(:get_value) ? result.get_value(hsh_keys) : result)
18
20
  end
19
- def self.get_collection(path, params, force=false, &block)
21
+ def self.get_collection(path, params, search=nil, force=false, &block)
20
22
  opts = {
21
23
  :format => 'json',
22
24
  :params => params || {},
23
25
  :force => force,
24
26
  :on_response => block
25
27
  }
28
+ hsh_keys = search.kind_of?(String) ? search.split(/\s+/) : nil
26
29
  super(path, opts) do |data|
27
- data
30
+ data.collect{|item| !hsh_keys.nil? && !hsh_keys.empty? && item.respond_to?(:get_value) ? item.get_value(hsh_keys) : item}
28
31
  end
29
32
  end
30
33
 
@@ -2,8 +2,8 @@ module Resourceful
2
2
  module Version
3
3
 
4
4
  MAJOR = 0
5
- MINOR = 6
6
- TINY = 7
5
+ MINOR = 7
6
+ TINY = 0
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kelredd-resourceful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.7
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelly Redding
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-04 00:00:00 -07:00
12
+ date: 2009-09-10 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency