exposable_attributes 0.0.1
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.
- data/README.rdoc +45 -0
- data/lib/exposable_attributes/version.rb +3 -0
- data/lib/exposable_attributes.rb +76 -0
- data/rails/init.rb +1 -0
- data/tasks/exposable_attributes_tasks.rake +4 -0
- data/test/exposable_attributes_test.rb +8 -0
- data/test/test_helper.rb +6 -0
- metadata +67 -0
data/README.rdoc
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
|
2
|
+
== exposable_attributes
|
3
|
+
|
4
|
+
Exposable Attributes plugin is designed to control ActiveRecord::Base to_xml and to_json methods output.
|
5
|
+
It might be useful with plugins like inherited_resources.
|
6
|
+
|
7
|
+
|
8
|
+
== Resources
|
9
|
+
|
10
|
+
Source
|
11
|
+
|
12
|
+
* git://github.com/nikolaeff/exposable_attributes
|
13
|
+
|
14
|
+
|
15
|
+
== Example
|
16
|
+
|
17
|
+
Redefine to_xml() and to_json() with def params as :only => [:id, :login, :password]
|
18
|
+
|
19
|
+
class User < ActiveRecord::Base
|
20
|
+
exposable_attributes :id, :login, :password
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
Redefine to_json method only, to_xml will be defined as usual
|
25
|
+
|
26
|
+
class User < ActiveRecord::Base
|
27
|
+
exposable_attributes :json, :only => [:id, :login]
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
Redefine to_xml only with "except" statement
|
32
|
+
|
33
|
+
class User < ActiveRecord::Base
|
34
|
+
exposable_attributes :xml, :except => [:encrypted_password, :salt, :single_access_token]
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
Redefine both to_xml and to_json
|
39
|
+
|
40
|
+
class User < ActiveRecord::Base
|
41
|
+
exposable_attributes :json, :xml, :except => [:encrypted_password, :salt, :single_access_token]
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
Copyright (c) 2010 nikolaeff, released under the MIT license
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# ExposableAttributes
|
2
|
+
module ExposableAttributes
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.send :extend, ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
DEFAULT_ACCESS_METHODS = [:xml, :json]
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
# Defines exposable attributes for AR object.
|
12
|
+
#
|
13
|
+
# == Examples
|
14
|
+
#
|
15
|
+
# class User < ActiveRecord::Base
|
16
|
+
# exposable_attributes :json, :only => [:id, :login]
|
17
|
+
# exposable_attributes :xml, :except => [:encrypted_password, :salt, :single_access_token]
|
18
|
+
# exposable_attributes :json, :xml, :except => [:encrypted_password, :salt, :single_access_token]
|
19
|
+
# end
|
20
|
+
def exposable_attributes(*attributes)
|
21
|
+
init_default_accessors unless already_initialized?
|
22
|
+
access_methods, options = parse_attributes(attributes)
|
23
|
+
access_methods.each {|method_name| define_accessor(method_name, options) }
|
24
|
+
send :include, InstanceMethods
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def init_default_accessors
|
29
|
+
DEFAULT_ACCESS_METHODS.each {|method_name| define_accessor(method_name, {}) }
|
30
|
+
set_initialization_flag
|
31
|
+
end
|
32
|
+
|
33
|
+
def set_initialization_flag
|
34
|
+
cattr_accessor :exposable_attributes_initialized
|
35
|
+
self.exposable_attributes_initialized = true
|
36
|
+
end
|
37
|
+
|
38
|
+
def already_initialized?
|
39
|
+
self.respond_to? :exposable_attributes_initialized
|
40
|
+
end
|
41
|
+
|
42
|
+
def define_accessor(accessor_name, options)
|
43
|
+
access_method_accessor = "exposable_#{accessor_name.to_s}_attributes"
|
44
|
+
self.send(:cattr_accessor, access_method_accessor)
|
45
|
+
self.send("#{access_method_accessor}=", options)
|
46
|
+
end
|
47
|
+
|
48
|
+
def parse_attributes(attributes)
|
49
|
+
access_methods = []
|
50
|
+
options = {}
|
51
|
+
|
52
|
+
if attributes.last.is_a?(Hash) then
|
53
|
+
options = attributes.pop
|
54
|
+
access_methods = attributes
|
55
|
+
else
|
56
|
+
options = { :only => attributes }
|
57
|
+
access_methods = DEFAULT_ACCESS_METHODS
|
58
|
+
end
|
59
|
+
|
60
|
+
[access_methods, options]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
module InstanceMethods
|
65
|
+
def to_xml(options = {})
|
66
|
+
super(exposable_xml_attributes.merge(options))
|
67
|
+
end
|
68
|
+
|
69
|
+
def to_json(options = {})
|
70
|
+
super(exposable_json_attributes.merge(options))
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
ActiveRecord::Base.send :include, ExposableAttributes
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'exposable_attributes'
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: exposable_attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Andrey Nikolaev
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-18 00:00:00 +04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Ruby on Rails plugin designed to control ActiveRecord::Base to_xml and to_json methods output
|
22
|
+
email: nikolaeff@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.rdoc
|
29
|
+
files:
|
30
|
+
- lib/exposable_attributes.rb
|
31
|
+
- lib/exposable_attributes/version.rb
|
32
|
+
- rails/init.rb
|
33
|
+
- tasks/exposable_attributes_tasks.rake
|
34
|
+
- README.rdoc
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/nikolaeff/exposable_attributes
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --charset=UTF-8
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
segments:
|
49
|
+
- 0
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.3.6
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Make ActiveRecord attributes exposable
|
65
|
+
test_files:
|
66
|
+
- test/exposable_attributes_test.rb
|
67
|
+
- test/test_helper.rb
|