format_alias 0.0.4

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.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in format_alias.gemspec
4
+ gemspec
@@ -0,0 +1,44 @@
1
+ = Format Alias
2
+
3
+ = Installation
4
+
5
+ Include following line in your Gemfile:
6
+
7
+ gem 'format_alias'
8
+
9
+ = Usage
10
+
11
+ == Date formatting
12
+
13
+ Useful when you want user to input formatted dates.
14
+
15
+ class News < ActiveRecord::Base
16
+ date_format_alias :created_at, '%d/%m/%Y'
17
+ end
18
+
19
+ item = News.new
20
+ item.created_at_formatted = '12.01.2001'
21
+ item.created_at # 2001-01-12
22
+
23
+ == Setting polymorphic attribute.
24
+
25
+ Useful when you want to set variables from such select:
26
+
27
+ <select name="imageable">
28
+ <option value="Image:3">Image</option>
29
+ <option value="Banner:1">Banner</option>
30
+ ...
31
+ </select>
32
+
33
+ class News < ActiveRecord::Base
34
+ belongs_to :imageable, :polymorphic => true
35
+
36
+ polymorphic_alias :imageable
37
+ end
38
+
39
+ item = News.new
40
+ item.imageable = "Image:3"
41
+ item.imageable_id # 3
42
+ item.imageable_type # Image
43
+
44
+ = To be continued...
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core'
3
+ require 'rspec/core/rake_task'
4
+ require 'bundler'
5
+
6
+ Bundler::GemHelper.install_tasks
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "format_alias/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "format_alias"
7
+ s.version = FormatAlias::VERSION
8
+ s.authors = ["Victor Sokolov"]
9
+ s.email = ["gzigzigzeo@gmail.com"]
10
+ s.homepage = "http://github.com/gzigzigzeo/format_alias"
11
+ s.summary = %q{ActiveRecord attribute accessor aliases for date formatting etc.}
12
+ s.description = %q{ActiveRecord attribute accessor aliases for date formatting etc.}
13
+
14
+ s.rubyforge_project = "format_alias"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency 'activerecord', '>= 3.0'
22
+ s.add_dependency 'activesupport', '>= 3.0'
23
+
24
+ s.add_development_dependency('rspec-rails')
25
+ s.add_development_dependency('sqlite3-ruby')
26
+ end
@@ -0,0 +1,6 @@
1
+ require 'active_support/concern'
2
+ require "format_alias/version"
3
+ require "format_alias/base"
4
+ require "format_alias/formatters/date_format"
5
+ require "format_alias/formatters/polymorphic_alias"
6
+ require "format_alias/railtie"
@@ -0,0 +1,20 @@
1
+ require 'active_support/core_ext/array/extract_options'
2
+ require 'active_support/core_ext/time/zones'
3
+ require 'active_support/core_ext/date_time/zones'
4
+ require 'tzinfo'
5
+
6
+ module FormatAlias
7
+ module Base
8
+ extend ActiveSupport::Concern
9
+
10
+ module ClassMethods
11
+ def format_alias(attr, getter, setter, *args)
12
+ options = args.extract_options!
13
+ name = options.delete(:as) || "#{attr}_formatted"
14
+
15
+ define_method name, &getter
16
+ define_method "#{name}=", &setter
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ module FormatAlias
2
+ module Base
3
+ module ClassMethods
4
+ def date_format_alias(attr, format, *args)
5
+ format_alias(
6
+ attr,
7
+ proc { send(attr).strftime(format) unless send(attr).nil? },
8
+ proc { |value| send(:"#{attr}=", (Time.parse(value, format) rescue nil)) },
9
+ *args
10
+ )
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,35 @@
1
+ module FormatAlias
2
+ module Base
3
+ module ClassMethods
4
+ def polymorphic_alias(attr, *args)
5
+ options = args.extract_options!
6
+ polymorphic = options.delete(:polymorphic) || true
7
+ attrs = options.delete(:attrs)
8
+ delim = options.delete(:delimiter) || ':'
9
+
10
+ raise ArgumentError, "Specify :polymorphic or :attrs option for polymorphic_alias :#{attr} in #{self.name}" if attrs.blank? && polymorphic.blank?
11
+
12
+ if polymorphic
13
+ attrs = ["#{attr}_type", "#{attr}_id"]
14
+ options[:as] ||= "#{attr}_type_id"
15
+ else
16
+ options[:as] ||= "#{attr}_split"
17
+ end
18
+
19
+ format_alias(
20
+ attr,
21
+ proc {
22
+ r = attrs.map { |a| send(a) }.compact
23
+ r.present? ? r.join(delim) : nil
24
+ },
25
+ proc { |value|
26
+ values = value.to_s.split(delim)
27
+ h = Hash[attrs.zip(values)]
28
+ h.map { |a, v| send("#{a}=", v) }
29
+ },
30
+ options
31
+ )
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,11 @@
1
+ module FormatAlias
2
+ if defined?(Rails::Railtie)
3
+ class Railtie < Rails::Railtie
4
+ initializer 'format_alias.extend_active_record' do
5
+ ActiveSupport.on_load :active_record do
6
+ ActiveRecord::Base.send(:include, FormatAlias::Base)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module FormatAlias
2
+ VERSION = "0.0.4"
3
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe "FormatAlias spec" do
4
+ before(:each) do
5
+ @t = TestClass.new
6
+ end
7
+
8
+ context "date/time conversions" do
9
+ it "should correctly convert dates" do
10
+ d = DateTime.new(2009, 1, 1)
11
+ @t.d = d
12
+ @t.d_formatted.should eq("20090101")
13
+
14
+ @t.d_formatted = nil
15
+ @t.d.should be_nil
16
+
17
+ @t.d_formatted = "20090202"
18
+ @t.d.should_not be_nil
19
+ @t.d.year.should eq(2009)
20
+ @t.d.month.should eq(2)
21
+ @t.d.day.should eq(2)
22
+ end
23
+
24
+ it "should correctly convert dates with time zones" do
25
+ d = DateTime.new(2009, 1, 1, 12, 0)
26
+ @t.dt = d
27
+ @t.dt_formatted.should eq("20090101 12:00")
28
+
29
+ Time.use_zone 'Moscow' do
30
+ d = DateTime.new(2009, 1, 1, 12, 0)
31
+ @t.dt = d
32
+ @t.dt_formatted.should eq("20090101 12:00")
33
+ end
34
+ end
35
+ end
36
+
37
+ context "polymorphic conversions" do
38
+ it "should correctly convert polymorphic" do
39
+ @t.poly_type_id = "Page:3"
40
+ @t.poly_type.should == 'Page'
41
+ @t.poly_id.should == '3'
42
+
43
+ @t.poly_type_id = "Banner:4"
44
+ @t.poly_type_id.should == "Banner:4"
45
+
46
+ @t.poly_type_id = nil
47
+ @t.poly_type_id.should == nil
48
+
49
+ @t.poly_type = nil
50
+ @t.poly_id = 5
51
+ @t.poly_type_id.should == "5"
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH << "." unless $LOAD_PATH.include?(".")
2
+
3
+ begin
4
+ require "bundler"
5
+ Bundler.setup
6
+ rescue Bundler::GemNotFound
7
+ raise RuntimeError, "Bundler couldn't find some gems." +
8
+ "Did you run `bundle install`?"
9
+ end
10
+
11
+ Bundler.require
12
+
13
+ require 'support/test_class'
14
+
15
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
@@ -0,0 +1,13 @@
1
+ class TestClass
2
+ include FormatAlias::Base
3
+
4
+ attr_accessor :d, :dt
5
+ attr_accessor :poly_id, :poly_type
6
+ attr_accessor :value1, :value2, :value3
7
+
8
+ date_format_alias(:d, "%Y%m%d")
9
+ date_format_alias(:dt, "%Y%m%d %H:%M")
10
+
11
+ polymorphic_alias(:poly, :polymorphic => true)
12
+ polymorphic_alias(:values, :attrs => [:value1, :value2, :value3])
13
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: format_alias
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 4
10
+ version: 0.0.4
11
+ platform: ruby
12
+ authors:
13
+ - Victor Sokolov
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-29 00:00:00 +04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activerecord
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 3
31
+ - 0
32
+ version: "3.0"
33
+ prerelease: false
34
+ type: :runtime
35
+ requirement: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: activesupport
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 7
44
+ segments:
45
+ - 3
46
+ - 0
47
+ version: "3.0"
48
+ prerelease: false
49
+ type: :runtime
50
+ requirement: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: rspec-rails
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ prerelease: false
63
+ type: :development
64
+ requirement: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: sqlite3-ruby
67
+ version_requirements: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ prerelease: false
77
+ type: :development
78
+ requirement: *id004
79
+ description: ActiveRecord attribute accessor aliases for date formatting etc.
80
+ email:
81
+ - gzigzigzeo@gmail.com
82
+ executables: []
83
+
84
+ extensions: []
85
+
86
+ extra_rdoc_files: []
87
+
88
+ files:
89
+ - .gitignore
90
+ - Gemfile
91
+ - README.rdoc
92
+ - Rakefile
93
+ - format_alias.gemspec
94
+ - lib/format_alias.rb
95
+ - lib/format_alias/base.rb
96
+ - lib/format_alias/formatters/date_format.rb
97
+ - lib/format_alias/formatters/polymorphic_alias.rb
98
+ - lib/format_alias/railtie.rb
99
+ - lib/format_alias/version.rb
100
+ - spec/format_alias/format_alias_spec.rb
101
+ - spec/spec_helper.rb
102
+ - spec/support/test_class.rb
103
+ has_rdoc: true
104
+ homepage: http://github.com/gzigzigzeo/format_alias
105
+ licenses: []
106
+
107
+ post_install_message:
108
+ rdoc_options: []
109
+
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 3
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: 3
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ requirements: []
131
+
132
+ rubyforge_project: format_alias
133
+ rubygems_version: 1.6.2
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: ActiveRecord attribute accessor aliases for date formatting etc.
137
+ test_files:
138
+ - spec/format_alias/format_alias_spec.rb
139
+ - spec/spec_helper.rb
140
+ - spec/support/test_class.rb