unveil 0.1.2 → 0.1.3

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.
@@ -1,5 +1,5 @@
1
1
  Array.class_eval do
2
- def unveil(options = {})
3
- map {|obj| obj.unveil(options)}
2
+ def unveil(*args)
3
+ map {|obj| obj.unveil(*args)}
4
4
  end
5
5
  end
@@ -0,0 +1,4 @@
1
+ module Unveil
2
+ # Raised when a scope that doesn't exist is called
3
+ class UndefinedScopeError < StandardError; end
4
+ end
data/lib/unveil.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'unveil/error'
1
2
  require 'unveil/core_ext'
2
3
 
3
4
  module Unveil
@@ -6,17 +7,26 @@ module Unveil
6
7
  end
7
8
 
8
9
  module ClassMethods
9
- def unveil(options = {})
10
- cattr_accessor :unveil_options
11
- self.unveil_options = options
12
- send :include, InstanceMethods
10
+ def unveil(*args)
11
+ name = args[0].is_a?(Symbol) ? args.shift : :default
12
+ self.unveil_scopes[name] = args[0] || {}
13
+ end
14
+
15
+ def unveil_scopes
16
+ @@unveil_scopes ||= {:default => {}}
13
17
  end
14
18
  end
15
19
 
16
- module InstanceMethods
17
- def unveil(options = {})
18
- serializable_hash(self.class.unveil_options.merge(options))
20
+ def unveil(*args)
21
+ name = args[0].is_a?(Symbol) ? args.shift : :default
22
+ options = args[0] || {}
23
+
24
+ unless self.class.unveil_scopes[name]
25
+ raise Unveil::UndefinedScopeError,
26
+ ":#{name} hasn't been defined. Try adding `unveil :#{name}` to #{self.class.name}."
19
27
  end
28
+
29
+ return serializable_hash(self.class.unveil_scopes[name].merge(options))
20
30
  end
21
31
  end
22
32
 
@@ -0,0 +1,9 @@
1
+ john:
2
+ first_name: John
3
+ last_name: Smith
4
+ email: johnsmith@example.com
5
+
6
+ jane:
7
+ first_name: Jane
8
+ last_name: Smith
9
+ email: janesmith@example.com
data/test/schema.rb ADDED
@@ -0,0 +1,7 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :users, :force => true do |t|
3
+ t.string :first_name
4
+ t.string :last_name
5
+ t.string :email
6
+ end
7
+ end
data/test/test_helper.rb CHANGED
@@ -1,3 +1,21 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
+ require 'active_record'
4
+ require 'active_record/fixtures'
3
5
  require 'active_support'
6
+
7
+ def load_schema
8
+ # Connect to the database
9
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
10
+
11
+ # Load the schema
12
+ load(File.dirname(__FILE__) + '/schema.rb')
13
+
14
+ # Create the fixtures
15
+ fixtures = ActiveRecord::Schema.tables
16
+ fixtures.delete('schema_migrations')
17
+ Fixtures.create_fixtures(File.dirname(__FILE__) + '/fixtures', fixtures)
18
+
19
+ # Require Unveil
20
+ require File.dirname(__FILE__) + '/../init'
21
+ end
data/test/unveil_test.rb CHANGED
@@ -1,8 +1,55 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class UnveilTest < ActiveSupport::TestCase
4
- # Replace this with your real tests.
5
- test "the truth" do
6
- assert true
4
+ load_schema
5
+
6
+ class User < ActiveRecord::Base
7
+ unveil :limited, :except => :email
8
+ end
9
+
10
+ test 'should define unveil in objects of ActiveRecord::Base' do
11
+ assert User.first.unveil
12
+ end
13
+
14
+ test 'should use the default scope when no scope is specified' do
15
+ default_user = { 'id' => 1, 'first_name' => 'John', 'last_name' => 'Smith', 'email' => 'johnsmith@example.com' }
16
+
17
+ user = User.first
18
+
19
+ assert_equal user.unveil(:default), user.unveil
20
+ assert_equal user.unveil, default_user
21
+ end
22
+
23
+ test 'should use a named scope when specified' do
24
+ limited_user = { 'id' => 1, 'first_name' => 'John', 'last_name' => 'Smith' }
25
+
26
+ user = User.first
27
+
28
+ assert_not_equal user.unveil, user.unveil(:limited)
29
+ assert_equal user.unveil(:limited), limited_user
30
+ end
31
+
32
+ test 'should call unveil for each object in an array' do
33
+ users = [
34
+ { 'id' => 1, 'first_name' => 'John', 'last_name' => 'Smith', 'email' => 'johnsmith@example.com' },
35
+ { 'id' => 2, 'first_name' => 'Jane', 'last_name' => 'Smith', 'email' => 'janesmith@example.com' }
36
+ ]
37
+
38
+ assert_equal User.all.unveil, users
39
+ end
40
+
41
+ test 'should use a named scope for each object in an array when specified' do
42
+ default_users = [
43
+ { 'id' => 1, 'first_name' => 'John', 'last_name' => 'Smith', 'email' => 'johnsmith@example.com' },
44
+ { 'id' => 2, 'first_name' => 'Jane', 'last_name' => 'Smith', 'email' => 'janesmith@example.com' }
45
+ ]
46
+
47
+ limited_users = [
48
+ { 'id' => 1, 'first_name' => 'John', 'last_name' => 'Smith' },
49
+ { 'id' => 2, 'first_name' => 'Jane', 'last_name' => 'Smith' }
50
+ ]
51
+
52
+ assert_equal User.all.unveil, default_users
53
+ assert_equal User.all.unveil(:limited), limited_users
7
54
  end
8
55
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Shaun Chapman
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-13 00:00:00 -05:00
17
+ date: 2011-02-15 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -29,7 +29,10 @@ extra_rdoc_files: []
29
29
  files:
30
30
  - Rakefile
31
31
  - lib/unveil/core_ext.rb
32
+ - lib/unveil/error.rb
32
33
  - lib/unveil.rb
34
+ - test/fixtures/users.yml
35
+ - test/schema.rb
33
36
  - test/test_helper.rb
34
37
  - test/unveil_test.rb
35
38
  has_rdoc: true