gnoso-remote_entity 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,28 @@
1
+ module RemoteEntity
2
+ module EntityRecord
3
+
4
+ def self.included(base)
5
+ base.class_eval do
6
+ alias_method :old_to_xml, :to_xml
7
+ def to_xml(options = {})
8
+ if !options[:methods]
9
+ options[:methods] = [ :remote_entity_id ]
10
+ else
11
+ options[:methods] << :remote_entity_id
12
+ end
13
+
14
+ old_to_xml(options)
15
+ end
16
+ end
17
+ end
18
+
19
+ attr_accessor :remote_entity_id
20
+ def remote_entity_id
21
+ if id
22
+ return "#{RemoteEntity.service}-#{self.class.name.underscore.gsub("/", "_")}-#{id}"
23
+ else
24
+ return nil
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,97 @@
1
+ module RemoteEntity
2
+ module EntityRecord
3
+ module Assocations
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def belongs_to_remote_entity(association_id, options = {})
11
+ if options[:class_name].nil?
12
+ options[:class_name] = association_id.to_s.camelize.to_sym
13
+ end
14
+
15
+ association_class =
16
+ Object.const_get(options[:class_name].to_sym)
17
+
18
+ define_accessible(association_id, options, association_class)
19
+ define_constructors(association_id, options, association_class)
20
+
21
+ if options[:dependent] == :destroy
22
+ define_destroy(association_id, options, association_class)
23
+ after_destroy "destroy_#{association_id}".to_sym
24
+ end
25
+ end
26
+
27
+ private
28
+ def define_accessible(association_id, options, association_class)
29
+
30
+
31
+ if !method_defined?("remote_entity_cache")
32
+ define_method("remote_entity_cache") do
33
+ @remote_entity_cache ||= {}
34
+ end
35
+ end
36
+ if !method_defined?("remote_entity_dirty_vals")
37
+ define_method("remote_entity_dirty_vals") do
38
+ @remote_entity_dirty_vals ||= {}
39
+ end
40
+ end
41
+
42
+ define_method("#{association_id.to_s}_remote_entity_object") do
43
+ service = association_class.service
44
+ resource = association_class.name.underscore
45
+
46
+ RemoteEntity.new(
47
+ self.send("#{association_id.to_s}_id").to_s)
48
+ end
49
+
50
+ define_method("#{association_id.to_s}".to_sym) do
51
+ if remote_entity_dirty_vals.has_key?(association_id)
52
+ remote_entity_dirty_vals[association_id]
53
+ elsif !self.send("#{association_id.to_s}_id").nil?
54
+ id = self.send("#{association_id.to_s}_remote_entity_object").id
55
+ remote_entity_cache[association_id] ||= association_class.find(id)
56
+ else
57
+ nil
58
+ end
59
+ end
60
+
61
+ define_method("#{association_id.to_s}=".to_sym) do |value|
62
+ remote_entity_cache[association_id] = nil
63
+
64
+ self.send("#{association_id.to_s}_id=", value.remote_entity_id)
65
+ remote_entity_dirty_vals[association_id] = value
66
+ end
67
+ end
68
+
69
+ def define_constructors(association_id, options, association_class)
70
+ define_method("build_#{association_id}") do |*params|
71
+ attributes = params.first
72
+
73
+ object = association_class.new(attributes)
74
+
75
+ self.send("#{association_id.to_s}=", object)
76
+ end
77
+
78
+ define_method("create_#{association_id}") do |*params|
79
+ attributes = params.first
80
+
81
+ object = association_class.new(attributes)
82
+ object.save
83
+
84
+ self.send("#{association_id}=", object)
85
+ end
86
+ end
87
+
88
+ def define_destroy(association_id, options, association_class)
89
+ define_method("destroy_#{association_id}") do
90
+ object = self.send("#{association_id}")
91
+ object.destroy
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,54 @@
1
+ require 'active_resource'
2
+
3
+ module RemoteEntity
4
+ class EntityResource < ActiveResource::Base
5
+
6
+ def initialize(attrs = {})
7
+ @@schema ||= []
8
+
9
+ attrs = HashWithIndifferentAccess.new(attrs)
10
+ @@schema.each do |attribute|
11
+ if !attrs.has_key?(attribute)
12
+ attrs[attribute] = nil
13
+ end
14
+ end
15
+ if !attrs.has_key?(:remote_entity_id)
16
+ attrs[:remote_entity_id] = nil
17
+ end
18
+
19
+ super(attrs)
20
+ end
21
+
22
+ # Defines methods for the following properties on every instance of this
23
+ # type that is created
24
+ def self.schema(*args)
25
+ @@schema = args
26
+ end
27
+
28
+ # Helps us set the site automatically.
29
+ def self.version=(version)
30
+ @@api_version = version
31
+ self.update_site
32
+ end
33
+
34
+ def self.version
35
+ return defined?(@@api_version) ? @@api_version : nil
36
+ end
37
+
38
+ # Helps us set the site automatically
39
+ def self.service=(service)
40
+ @@service = service
41
+ self.update_site
42
+ end
43
+
44
+ def self.service
45
+ return defined?(@@service) ? @@service : nil
46
+ end
47
+
48
+ private
49
+ def self.update_site
50
+ self.site = RemoteEntity.service_uri(self.service,
51
+ self.version)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,48 @@
1
+ module RemoteEntity
2
+ class RemoteEntity
3
+ attr_accessor :service
4
+ attr_accessor :resource
5
+ attr_accessor :id
6
+
7
+ # Constructs a new remote entity object.
8
+ def initialize(remote_entity_id)
9
+ parts = remote_entity_id.split('-', 3)
10
+ if parts.length != 3
11
+ raise "Entity id given is not a proper entity id"
12
+ end
13
+
14
+ self.service, self.resource, self.id = parts
15
+ end
16
+
17
+ # Returns the service uri for the given service at the given version
18
+ def self.service_uri(service, version)
19
+ return "#{lookup_service(service)[:uri]}/api/v#{version.to_s}/"
20
+ end
21
+
22
+ # Registers a service so that we can build urls from it. Holla!
23
+ def self.register_service(service_name, service_uri, service_api_key = "")
24
+ services[service_name.to_sym] = { :uri => service_uri,
25
+ :api_key => service_api_key }
26
+ end
27
+
28
+ def self.lookup_service(service)
29
+ result = services[service.to_sym]
30
+ raise "Service `#{service.to_s}` not found" if !result
31
+
32
+ return result
33
+ end
34
+
35
+ def self.service=(service)
36
+ @@service = service
37
+ end
38
+
39
+ def self.service
40
+ @@service ||= nil
41
+ end
42
+
43
+ private
44
+ def self.services
45
+ @@services ||= {}
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ require File.join(File.dirname(__FILE__), 'remote_entity', 'remote_entity')
2
+ require File.join(File.dirname(__FILE__), 'remote_entity', 'entity_resource')
3
+ require File.join(File.dirname(__FILE__), 'remote_entity', 'entity_record')
4
+ require File.join(File.dirname(__FILE__), 'remote_entity',
5
+ 'entity_record_associations')
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gnoso-remote_entity
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gnoso, Inc.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activeresource
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "2.0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "2.0"
34
+ version:
35
+ description: Remote Entity defines a pattern for working with REST services. It defines a versioning scheme, a remote entity identifier scheme, and adds some useful features to ActiveRecord and ActiveResource for working with those schemes.
36
+ email: alan@gnoso.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - lib/remote_entity.rb
45
+ - lib/remote_entity/entity_record.rb
46
+ - lib/remote_entity/entity_record_associations.rb
47
+ - lib/remote_entity/entity_resource.rb
48
+ - lib/remote_entity/remote_entity.rb
49
+ has_rdoc: true
50
+ homepage: http://www.gnoso.com
51
+ licenses:
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 2
75
+ summary: Remote Entity defines a pattern for working with REST services.
76
+ test_files: []
77
+