publicize 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2010-06-11
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,8 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/publicize.rb
6
+ lib/activerecord_extensions.rb
7
+ test/test_helper.rb
8
+ test/test_publicize.rb
data/README.rdoc ADDED
@@ -0,0 +1,57 @@
1
+ = publicize
2
+
3
+ * http://github.com/#{github_username}/#{project_name}
4
+
5
+ == DESCRIPTION:
6
+
7
+ The Publicize gem extends ActiveRecord to support features commonly needed
8
+ by Rails-based web services. When creating a web service, Publicize allows
9
+ developers to specify how models will be exposed and, equally important,
10
+ what fields will be exposed.
11
+
12
+ == FEATURES/PROBLEMS:
13
+
14
+ XML generation does not currently work.
15
+
16
+ == SYNOPSIS:
17
+
18
+ Examples to be provided later.
19
+
20
+ == REQUIREMENTS:
21
+
22
+ * Allows developers to specify the name under which a model will be exposed.
23
+ * Allows developers to specify which fields will be exposed.
24
+ * Allow developers to specify alternate names for exposed fields, i.e. - the
25
+ web service may expose friendlier names than the underlying database.
26
+ * Allows developers to expose "virtual" fields for a web service, i.e. - the
27
+ output of a method can be publicized, not just database-derived model
28
+ attributes.
29
+
30
+ == INSTALL:
31
+
32
+ * gem install publicize
33
+
34
+ == LICENSE:
35
+
36
+ (The MIT License)
37
+
38
+ Copyright (c) 2010 MetroStar Systems
39
+
40
+ Permission is hereby granted, free of charge, to any person obtaining
41
+ a copy of this software and associated documentation files (the
42
+ 'Software'), to deal in the Software without restriction, including
43
+ without limitation the rights to use, copy, modify, merge, publish,
44
+ distribute, sublicense, and/or sell copies of the Software, and to
45
+ permit persons to whom the Software is furnished to do so, subject to
46
+ the following conditions:
47
+
48
+ The above copyright notice and this permission notice shall be
49
+ included in all copies or substantial portions of the Software.
50
+
51
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
52
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
53
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
54
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
55
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
56
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
57
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/publicize'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'publicize' do
14
+ self.developer 'David Keener', 'dkeener@keenertech.com'
15
+ self.rubyforge_name = self.name
16
+ # self.extra_deps = [['activesupport','>= 2.0.2']]
17
+
18
+ end
19
+
20
+ require 'newgem/tasks'
21
+ Dir['tasks/**/*.rake'].each { |t| load t }
22
+
23
+ # TODO - want other tests/tasks run by default? Add them to the list
24
+ # remove_task :default
25
+ # task :default => [:spec, :features]
@@ -0,0 +1,111 @@
1
+ require 'active_record'
2
+
3
+ module Publicize
4
+ module ActiveRecordExtensions
5
+
6
+ PublicizedField = Struct.new(:name, :label)
7
+
8
+ def self.included(base)
9
+ base.send(:extend, ClassMethods)
10
+ base.send(:include, InstanceMethods)
11
+ base.class_eval do
12
+ attr_accessor :controller
13
+ end
14
+ end
15
+
16
+ module ClassMethods
17
+
18
+ # Defines a field to be publicized. This may be a "virtual" field, i.e. -
19
+ # it may be a method rather than a database-derived class attribute.
20
+
21
+ def publicize_field(name, opts={})
22
+ publicized_fields << PublicizedField.new(name, opts[:as].nil? ? name : opts[:as])
23
+ end
24
+
25
+ # Defines whether 1) the resource is publicized, and 2) optionally under
26
+ # which name.
27
+
28
+ def publicize_model(name = self.name)
29
+ @publicized_name = name.to_s.downcase.dasherize
30
+ @publicized = true
31
+ end
32
+
33
+ # Indicates whether the model has been publicized or not.
34
+
35
+ def is_publicized?
36
+ @publicized.nil? ? false : true
37
+ end
38
+
39
+ # Gets an array of the fields specified to be publicized.
40
+
41
+ def publicized_fields
42
+ @publicized_fields ||= []
43
+ end
44
+
45
+ # Gets the name under which a model will be publicized. Defaults to the
46
+ # name of the model itself.
47
+
48
+ def publicized_name
49
+ @publicized_name || self.name.downcase.dasherize
50
+ end
51
+
52
+ # Returns the PublicizedField structure associated with the specified
53
+ # label.
54
+
55
+ def select_publicized_field(label)
56
+ publicized_fields.select {|x| x.label.to_s == label.to_s}
57
+ end
58
+
59
+ end
60
+
61
+ module InstanceMethods
62
+
63
+ def as_json(options = {})
64
+ if self.class.is_publicized?
65
+
66
+ # Based on ActiveRecord setting, do {"name" : {"field1" : "value1", ...}}
67
+ if ActiveRecord::Base.include_root_in_json == true
68
+ {self.class.publicized_name => publicize.as_json(options)}
69
+ else
70
+ publicize.as_json(options)
71
+ end
72
+
73
+ else
74
+ super(options)
75
+ end
76
+ end
77
+
78
+ def to_xml(options = {})
79
+ if self.class.is_publicized?
80
+ xml = options[:builder] ||= Builder::XmlMarkup.new(options)
81
+ xml.instruct! unless options[:skip_instruct]
82
+ xml.level_one do
83
+ xml.tag!(:test, 'content')
84
+ end
85
+ else
86
+ super(options)
87
+ end
88
+ end
89
+
90
+ # Generates an ordered hash containing publicized fields for a class
91
+ # instance. Not generally used directly by end users of the gem.
92
+
93
+ def publicize
94
+ if !self.class.is_publicized?
95
+ logger.warn("WARNING (Publicize): Attempted to publicize an unpublicized model")
96
+ return nil
97
+ end
98
+ # TODO: Can switch to a regular hash when using Ruby 1.9
99
+ hsh = ActiveSupport::OrderedHash.new
100
+ self.class.publicized_fields.each do |pf|
101
+ hsh[pf.label.nil? ? pf.name : pf.label] = ((v = eval(pf.name.to_s)).nil? ? '' : v)
102
+ end
103
+ hsh
104
+ end
105
+
106
+ end
107
+
108
+ end
109
+ end
110
+
111
+ ActiveRecord::Base.send(:include, Publicize::ActiveRecordExtensions)
data/lib/publicize.rb ADDED
@@ -0,0 +1,8 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'activerecord_extensions'
5
+
6
+ module Publicize
7
+ VERSION = '0.0.1'
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/publicize'
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestPublicize < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: publicize
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - David Keener
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-21 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rubyforge
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 2
32
+ - 0
33
+ - 4
34
+ version: 2.0.4
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: hoe
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 19
46
+ segments:
47
+ - 2
48
+ - 6
49
+ - 2
50
+ version: 2.6.2
51
+ type: :development
52
+ version_requirements: *id002
53
+ description: |-
54
+ The Publicize gem extends ActiveRecord to support features commonly needed
55
+ by Rails-based web services. When creating a web service, Publicize allows
56
+ developers to specify how models will be exposed and, equally important,
57
+ what fields will be exposed.
58
+ email:
59
+ - dkeener@keenertech.com
60
+ executables: []
61
+
62
+ extensions: []
63
+
64
+ extra_rdoc_files:
65
+ - History.txt
66
+ - Manifest.txt
67
+ files:
68
+ - History.txt
69
+ - Manifest.txt
70
+ - README.rdoc
71
+ - Rakefile
72
+ - lib/publicize.rb
73
+ - lib/activerecord_extensions.rb
74
+ - test/test_helper.rb
75
+ - test/test_publicize.rb
76
+ has_rdoc: true
77
+ homepage: http://github.com/#{github_username}/#{project_name}
78
+ licenses: []
79
+
80
+ post_install_message:
81
+ rdoc_options:
82
+ - --main
83
+ - README.rdoc
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ requirements: []
105
+
106
+ rubyforge_project: publicize
107
+ rubygems_version: 1.3.7
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: The Publicize gem extends ActiveRecord to support features commonly needed by Rails-based web services
111
+ test_files:
112
+ - test/test_helper.rb
113
+ - test/test_publicize.rb