passive_resource 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -5,15 +5,19 @@ Examples:
5
5
 
6
6
  used with a hash or hash like object
7
7
  > require 'passive_resource'
8
- => true
8
+ => true
9
+
9
10
  > class NewClass < PassiveResource::Base
10
11
  > def whole_name; "#{first_name} #{last_name}" end
11
12
  > end
12
- => nil
13
+ => nil
14
+
13
15
  > instance = NewClass.new(:first_name => 'grady', :last_name => 'griffin')
14
- => {"first_name"=>"grady", "last_name"=>"griffin"}
16
+ => {"first_name"=>"grady", "last_name"=>"griffin"}
17
+
15
18
  > instance.first_name
16
19
  => "grady"
20
+
17
21
  > instance.whole_name
18
22
  => "grady griffin"
19
23
 
@@ -23,24 +27,54 @@ Used as with an api returning json
23
27
  > class FacebookProfile < PassiveResource::Base
24
28
  > end
25
29
  => nil
30
+
26
31
  > facebook = FacebookProfile.new('https://graph.facebook.com/19292868552')
27
- => {"id"=>"19292868552", "name"=>"Facebook Platform", "picture"=>"http://profile.ak.fbcdn.net/hprofile-ak-ash2/276791_19292868552_1958181823_s.jpg", "link"=>"http://www.facebook.com/platform", "likes"=>3923792, "category"=>"Product/service", "website"=>"http://developers.facebook.com", "username"=>"platform", "founded"=>"2007", "company_overview"=>"Facebook Platform enables anyone to build social apps on Facebook and the web.", "mission"=>"To make the web more open and social.", "about"=>"We're building the social web. Get the latest here: developers.facebook.com ", "talking_about_count"=>74536}
32
+ => {"id"=>"19292868552", "name"=>"Facebook Platform", "picture"=>"http://profile.ak.fbcdn.net/hprofile-ak-ash2/276791_19292868552_1958181823_s.jpg", "link"=>"http://www.facebook.com/platform", "likes"=>3923792, "category"=>"Product/service", "website"=>"http://developers.facebook.com", "username"=>"platform", "founded"=>"2007", "company_overview"=>"Facebook Platform enables anyone to build social apps on Facebook and the web.", "mission"=>"To make the web more open and social.", "about"=>"We're building the social web. Get the latest here: developers.facebook.com ", "talking_about_count"=>74536}
33
+
28
34
  > facebook.talking_about_count
29
- => 74536
35
+ => 74536
36
+
30
37
  > facebook.founded
31
38
  => "2007"
32
39
 
33
40
  Works with code that was previously using hashes
34
41
 
35
42
  > facebook['talking_about_count']
36
- => 74536
43
+ => 74536
44
+
37
45
  > facebook[:founded]
38
- => "2007"
46
+ => "2007"
39
47
 
40
48
  Objects are nested automatically
41
49
  > instance = NewClass.new(:locations => [{:city => 'Orlando', :state => "FL"}, {:city => 'Greenwood', :state => "SC"}])
42
- => {"locations"=>[{"city"=>"Orlando", "state"=>"FL"}, {"city"=>"Greenwood", "state"=>"SC"}]}
50
+ => {"locations"=>[{"city"=>"Orlando", "state"=>"FL"}, {"city"=>"Greenwood", "state"=>"SC"}]}
51
+
43
52
  > instance.locations
44
- => [{"city"=>"Orlando", "state"=>"FL"}, {"city"=>"Greenwood", "state"=>"SC"}]
53
+ => [{"city"=>"Orlando", "state"=>"FL"}, {"city"=>"Greenwood", "state"=>"SC"}]
54
+
45
55
  > instance.locations.first.city
46
- => "Orlando"
56
+ => "Orlando"
57
+
58
+ the many method can be use for collections
59
+
60
+ > array = NewClass.many([{:city => 'Orlando', :state => "FL"}, {:city => 'Greenwood', :state => "SC"}])
61
+ => [{"city"=>"Orlando", "state"=>"FL"}, {"city"=>"Greenwood", "state"=>"SC"}]
62
+
63
+ > array.first.city
64
+ => "Orlando"
65
+
66
+ The key/value interface can be used to add methods
67
+ > empty = NewClass.new({})
68
+ => {}
69
+
70
+ > empty.some_method
71
+ NoMethodError: undefined method `some_method' for {}:NewClass
72
+ from /usr/local/rvm/gems/ruby-1.9.3-p0/gems/passive_resource-0.0.1/lib/passive_resource/base.rb:55:in `method_missing'
73
+ from (irb):6
74
+ from /usr/local/rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'
75
+
76
+ > empty['some_method'] = 56
77
+ => 56
78
+
79
+ > empty.some_method
80
+ => 56
@@ -27,13 +27,21 @@ module PassiveResource
27
27
  @seedling.has_key?(accessor) or HASH_METHODS.include?(method)
28
28
  end
29
29
 
30
- def [](key)
30
+ def [](*args)
31
+ args = args.flatten
32
+ if args.empty?
33
+ raise ArgumentError, "wrong number of arguments"
34
+ end
35
+ key = args.shift
31
36
  if self.class.nestable?(@seedling[key])
32
37
  @seedling[key] = self.class.new(@seedling[key])
33
38
  elsif self.class.collection?(@seedling[key])
34
39
  @seedling[key] = self.class.many(@seedling[key])
40
+ elsif self.class.callable?(args)
41
+ @seedling[key].call(args)
42
+ else
43
+ @seedling[key]
35
44
  end
36
- @seedling[key]
37
45
  end
38
46
 
39
47
  def self.many(ary)
@@ -68,5 +76,9 @@ module PassiveResource
68
76
  def self.collection?(obj)
69
77
  ['Array', 'WillPaginate::Collection'].include?(obj.class.to_s)
70
78
  end
79
+
80
+ def self.callable?(obj)
81
+ ['Proc'].include?(obj.class.to_s)
82
+ end
71
83
  end
72
84
  end
@@ -1,3 +1,3 @@
1
1
  module PassiveResource
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passive_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-01-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70256019607440 !ruby/object:Gem::Requirement
16
+ requirement: &70093083268480 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>'
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '2.3'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70256019607440
24
+ version_requirements: *70093083268480
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rest-client
27
- requirement: &70256019606840 !ruby/object:Gem::Requirement
27
+ requirement: &70093083267760 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70256019606840
35
+ version_requirements: *70093083267760
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70256020759600 !ruby/object:Gem::Requirement
38
+ requirement: &70093083266960 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70256020759600
46
+ version_requirements: *70093083266960
47
47
  description: Creates Ruby objects from JSON requested from apis or hashes
48
48
  email:
49
49
  - gradyg@izea.com