fallback 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ require 'spec/rake/spectask'
2
+ Spec::Rake::SpecTask.new {|t| t.spec_opts = ['--color']}
3
+ task :default => :spec
4
+
5
+ begin
6
+ require 'jeweler'
7
+ project_name = 'fallback'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = project_name
10
+ gem.summary = "Fallback when original is not present or somethings not right."
11
+ gem.email = "grosser.michael@gmail.com"
12
+ gem.homepage = "http://github.com/grosser/#{project_name}"
13
+ gem.authors = ["Michael Grosser"]
14
+ end
15
+
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
19
+ end
@@ -0,0 +1,34 @@
1
+ Fallback when original is not present or somethings not right.
2
+ For normal Object's and ActiveRecord's.
3
+
4
+ class User < ActiveRecord::Base
5
+ include Fallback
6
+
7
+ # use description if detailed description is not available
8
+ fallback :detailed_description => :descripion
9
+
10
+ has_one :shop
11
+
12
+ # use shop.name if user.name is blank
13
+ fallback :name, :to => :shop
14
+
15
+ # use shop.title if user.name is blank
16
+ fallback :name => :title, :to => :shop
17
+
18
+ # use shop.title if lambda is true (also works with :unless)
19
+ fallback :title, :to => :shop, :if => lambda{|user| user.title.size < 10 }
20
+
21
+ # use shop.title if user.title_to_short? is true
22
+ fallback :title, :to => :shop, :if => :title_to_short?
23
+ end
24
+
25
+ Install
26
+ =======
27
+ Gem ` sudo gem install fallback `
28
+ Or as Rails plugin: ` script/plugin install git://github.com/grosser/fallback.git `
29
+
30
+ Author
31
+ ======
32
+ [Michael Grosser](http://pragmatig.wordpress.com)
33
+ grosser.michael@gmail.com
34
+ Hereby placed under public domain, do what you want, just do not hold me accountable...
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,45 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{fallback}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Grosser"]
12
+ s.date = %q{2010-05-04}
13
+ s.email = %q{grosser.michael@gmail.com}
14
+ s.files = [
15
+ "Rakefile",
16
+ "Readme.md",
17
+ "VERSION",
18
+ "fallback.gemspec",
19
+ "lib/fallback.rb",
20
+ "spec/database.rb",
21
+ "spec/fallback_spec.rb",
22
+ "spec/spec_helper.rb"
23
+ ]
24
+ s.homepage = %q{http://github.com/grosser/fallback}
25
+ s.rdoc_options = ["--charset=UTF-8"]
26
+ s.require_paths = ["lib"]
27
+ s.rubygems_version = %q{1.3.6}
28
+ s.summary = %q{Fallback when original is not present or somethings not right.}
29
+ s.test_files = [
30
+ "spec/fallback_spec.rb",
31
+ "spec/database.rb",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+
35
+ if s.respond_to? :specification_version then
36
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
37
+ s.specification_version = 3
38
+
39
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
40
+ else
41
+ end
42
+ else
43
+ end
44
+ end
45
+
@@ -0,0 +1,52 @@
1
+ module Fallback
2
+ VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
3
+
4
+ def self.included(base)
5
+ base.send(:extend, ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def fallback(*args)
10
+ options = args.pop if args.last.is_a?(Hash) # extract_options!
11
+
12
+ methods = options.reject{|k,v| [:if, :unless, :to].include?(k) }.to_a
13
+ methods += args
14
+
15
+ methods.each do |method, delegate_method|
16
+ define_fallback(method, delegate_method, options)
17
+ end
18
+ end
19
+
20
+ def define_fallback(method, delegate_method, options)
21
+ delegate_method ||= method
22
+ # build attribute methods for AR or alias will not work
23
+ define_attribute_methods if respond_to?(:define_attribute_methods)
24
+
25
+ define_method "#{method}_with_fallback" do
26
+ current = send("#{method}_without_fallback")
27
+
28
+ conditions = options.select{|k,v| [:if, :unless].include?(k)}
29
+ do_delegate = if conditions.empty?
30
+ current.to_s.strip.empty? # present?
31
+ else
32
+ # {:if => :a, :unless => :a} --> [true, false].all? --> false
33
+ conditions.map do |condition, value|
34
+ result = !!(value.respond_to?(:call) ? value.call(self) : send(value))
35
+ condition == :unless ? result : !result
36
+ end.all?
37
+ end
38
+
39
+ if do_delegate
40
+ delegate = (options[:to] ? send(options[:to]) : self )
41
+ delegate.send(delegate_method)
42
+ else
43
+ current
44
+ end
45
+ end
46
+
47
+ # alias_method_chain method, :fallback
48
+ alias_method "#{method}_without_fallback", method
49
+ alias_method method, "#{method}_with_fallback"
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,23 @@
1
+ require 'active_record'
2
+
3
+ # connect
4
+ ActiveRecord::Base.establish_connection(
5
+ :adapter => "sqlite3",
6
+ :database => ":memory:"
7
+ )
8
+
9
+ # create tables
10
+ ActiveRecord::Schema.define(:version => 1) do
11
+ create_table :users do |t|
12
+ t.string :name
13
+ t.string :title
14
+ t.string :description
15
+ t.text :detailed_description
16
+ end
17
+
18
+ create_table :shops do |t|
19
+ t.string :name
20
+ t.string :title
21
+ t.string :description
22
+ end
23
+ end
@@ -0,0 +1,115 @@
1
+ require 'spec/spec_helper'
2
+
3
+ class Shop < ActiveRecord::Base
4
+ end
5
+
6
+ class User < ActiveRecord::Base
7
+ include Fallback
8
+ has_one :shop
9
+ fallback :name, :to => :shop
10
+ end
11
+
12
+ class User2 < ActiveRecord::Base
13
+ include Fallback
14
+ set_table_name :users
15
+ has_one :shop
16
+ fallback :name => :title, :to => :shop
17
+ end
18
+
19
+ class User3 < ActiveRecord::Base
20
+ include Fallback
21
+ set_table_name :users
22
+ fallback :description => :detailed_description
23
+ end
24
+
25
+ class User4 < ActiveRecord::Base
26
+ include Fallback
27
+ set_table_name :users
28
+ fallback :name => :description, :if => lambda{|u| u.xxx}
29
+ end
30
+
31
+
32
+ describe Fallback do
33
+ describe 'without delegation' do
34
+ it "uses current if current is present" do
35
+ user = User3.new(:description => 'DESC', :detailed_description => 'OOOPS')
36
+ user.description.should == 'DESC'
37
+ end
38
+
39
+ it "calls method if current is blank" do
40
+ user = User3.new(:description => ' ', :detailed_description => 'DESC')
41
+ user.description.should == 'DESC'
42
+ end
43
+
44
+ it "calls method if current is nil" do
45
+ user = User3.new(:description => nil, :detailed_description => 'DESC')
46
+ user.description.should == 'DESC'
47
+ end
48
+ end
49
+
50
+ describe 'simple delegation' do
51
+ it "uses current if current is present" do
52
+ user = User.new(:name => 'NAME', :shop => Shop.new(:name => 'OOPS'))
53
+ user.name.should == 'NAME'
54
+ end
55
+
56
+ it "calls method if current is blank" do
57
+ user = User.new(:name => ' ', :shop => Shop.new(:name => 'NAME'))
58
+ user.name.should == 'NAME'
59
+ end
60
+
61
+ it "calls method if current is nil" do
62
+ user = User.new(:name => nil, :shop => Shop.new(:name => 'NAME'))
63
+ user.name.should == 'NAME'
64
+ end
65
+ end
66
+
67
+ describe 'delegation with rewrite' do
68
+ it "uses current if current is present" do
69
+ user = User2.new(:name => 'NAME', :shop => Shop.new(:title => 'OOPS'))
70
+ user.name.should == 'NAME'
71
+ end
72
+
73
+ it "calls method if current is blank" do
74
+ user = User2.new(:name => ' ', :shop => Shop.new(:title => 'TITLE'))
75
+ user.name.should == 'TITLE'
76
+ end
77
+
78
+ it "calls method if current is nil" do
79
+ user = User2.new(:name => nil, :shop => Shop.new(:title => 'TITLE'))
80
+ user.name.should == 'TITLE'
81
+ end
82
+ end
83
+
84
+ describe 'with lambda' do
85
+ before do
86
+ @user = User4.new(:name => 'N', :description => 'D')
87
+ end
88
+
89
+ it "asks lambda" do
90
+ @user.should_receive(:xxx).and_return true
91
+ @user.name
92
+ end
93
+
94
+ it "returns original if lambda returns false" do
95
+ @user.stub!(:xxx).and_return false
96
+ @user.name.should == 'D'
97
+ end
98
+
99
+ it "delegates if lambda returns true" do
100
+ @user.stub!(:xxx).and_return true
101
+ @user.name.should == 'N'
102
+ end
103
+ end
104
+
105
+ describe "without fallback" do
106
+ it "can call original value" do
107
+ user = User.new(:name => nil, :shop => Shop.new(:name => 'NAME'))
108
+ user.name_without_fallback.should == nil
109
+ end
110
+ end
111
+
112
+ it "has a VERSION" do
113
+ Fallback::VERSION.should =~ /^\d+\.\d+\.\d+$/
114
+ end
115
+ end
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+ $LOAD_PATH << 'lib'
4
+ require 'fallback'
5
+ require 'spec/database'
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fallback
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Michael Grosser
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-04 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: grosser.michael@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - Rakefile
31
+ - Readme.md
32
+ - VERSION
33
+ - fallback.gemspec
34
+ - lib/fallback.rb
35
+ - spec/database.rb
36
+ - spec/fallback_spec.rb
37
+ - spec/spec_helper.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/grosser/fallback
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.6
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Fallback when original is not present or somethings not right.
68
+ test_files:
69
+ - spec/fallback_spec.rb
70
+ - spec/database.rb
71
+ - spec/spec_helper.rb