fmt_alias 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/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ spec/tmp/*/**
2
+ pkg
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --backtrace
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fmt_alias (0.0.1)
5
+ activesupport (>= 3.0.0)
6
+ tzinfo
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activesupport (3.0.5)
12
+ diff-lcs (1.1.2)
13
+ rspec (2.1.0)
14
+ rspec-core (~> 2.1.0)
15
+ rspec-expectations (~> 2.1.0)
16
+ rspec-mocks (~> 2.1.0)
17
+ rspec-core (2.1.0)
18
+ rspec-expectations (2.1.0)
19
+ diff-lcs (~> 1.1.2)
20
+ rspec-mocks (2.1.0)
21
+ tzinfo (0.3.25)
22
+
23
+ PLATFORMS
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ fmt_alias!
28
+ rspec
data/README.rdoc ADDED
@@ -0,0 +1,23 @@
1
+ = Fmt Alias
2
+
3
+ Simply create virtual fields to humanize real field values.
4
+
5
+ = Installation
6
+
7
+ Add the following line to your Gemfile:
8
+
9
+ gem 'fmt_alias'
10
+
11
+ = Example
12
+
13
+ Let the News model have created_at column:
14
+
15
+ class News < ActiveRecord::Base
16
+ date_fmt_alias(:d, "%Y%m%d")
17
+ date_fmt_alias(:dt, "%Y%m%d %H:%M")
18
+ end
19
+
20
+ news = News.new
21
+ news.d_fmt = "20090101"
22
+ news.d == Date.new(2009,1,1) # true
23
+ news.d_fmt # 20090101
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+ require 'arel_date_scopes'
6
+ require 'bundler'
7
+
8
+ Bundler::GemHelper.install_tasks
9
+ RSpec::Core::RakeTask.new(:spec)
10
+ task :default => :spec
data/fmt_alias.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ $:.push File.expand_path("../lib", __FILE__)
4
+ require "fmt_alias/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{fmt_alias}
8
+ s.version = FmtAlias::VERSION
9
+
10
+ s.authors = ["Victor Sokolov"]
11
+ s.description = %q{Simply create virtual fields to humanize real field values}
12
+ s.email = %q{gzigzigzeo@gmail.com}
13
+ s.homepage = %q{http://github.com/gzigzigzeo/fmt_alias}
14
+ s.rdoc_options = ["--charset=UTF-8"]
15
+ s.summary = %q{Simply create virtual fields to humanize real field values}
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency('activesupport', '>= 3.0.0')
23
+ s.add_dependency('tzinfo')
24
+ s.add_development_dependency('rspec')
25
+ end
@@ -0,0 +1,31 @@
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 FmtAlias
7
+ module Aliases
8
+ def self.included(base)
9
+ base.send(:extend, ClassMethods)
10
+ end
11
+
12
+ module ClassMethods
13
+ def define_alias_for(attr, getter, setter, *args)
14
+ options = args.extract_options!
15
+ name = options.delete(:name) || "#{attr}_fmt"
16
+
17
+ define_method name, &getter
18
+ define_method "#{name}=", &setter
19
+ end
20
+
21
+ def date_fmt_alias(attr, fmt, *args)
22
+ define_alias_for(
23
+ attr,
24
+ proc { send(attr).strftime(fmt) unless send(attr).nil? },
25
+ proc { |value| send(:"#{attr}=", (Time.parse(value, fmt) rescue nil)) },
26
+ *args
27
+ )
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,9 @@
1
+ module FmtAlias
2
+ class Railtie < Rails::Railtie
3
+ initializer 'fmt_alias.insert_into_active_record' do
4
+ ActiveSupport.on_load :active_record do
5
+ ActiveRecord::Base.send(:include, FmtAlias::Aliases)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module FmtAlias
2
+ VERSION = '0.0.1'
3
+ end
data/lib/fmt_alias.rb ADDED
@@ -0,0 +1,5 @@
1
+ module FmtAlias
2
+ require 'fmt_alias/aliases'
3
+ require 'fmt_alias/version'
4
+ require 'fmt_alias/railtie' if defined?(Rails)
5
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Fmt_alias 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_fmt.should eq("20090101")
13
+
14
+ @t.d_fmt = nil
15
+ @t.d.should be_nil
16
+
17
+ @t.d_fmt = "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_fmt.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_fmt.should eq("20090101 12:00")
33
+ end
34
+ end
35
+ end
36
+
37
+ context "integer conversions" do
38
+
39
+ end
40
+ end
@@ -0,0 +1,21 @@
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
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
14
+
15
+ class TestClass
16
+ include FmtAlias::Aliases
17
+ attr_accessor :d, :dt
18
+
19
+ date_fmt_alias(:d, "%Y%m%d")
20
+ date_fmt_alias(:dt, "%Y%m%d %H:%M")
21
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fmt_alias
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Victor Sokolov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-30 00:00:00 +04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activesupport
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 3.0.0
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: tzinfo
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rspec
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id003
49
+ description: Simply create virtual fields to humanize real field values
50
+ email: gzigzigzeo@gmail.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - .gitignore
59
+ - .rspec
60
+ - Gemfile
61
+ - Gemfile.lock
62
+ - README.rdoc
63
+ - Rakefile
64
+ - fmt_alias.gemspec
65
+ - lib/fmt_alias.rb
66
+ - lib/fmt_alias/aliases.rb
67
+ - lib/fmt_alias/railtie.rb
68
+ - lib/fmt_alias/version.rb
69
+ - spec/fmt_alias/fmt_alias_spec.rb
70
+ - spec/spec_helper.rb
71
+ has_rdoc: true
72
+ homepage: http://github.com/gzigzigzeo/fmt_alias
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options:
77
+ - --charset=UTF-8
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: "0"
92
+ requirements: []
93
+
94
+ rubyforge_project:
95
+ rubygems_version: 1.5.3
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Simply create virtual fields to humanize real field values
99
+ test_files:
100
+ - spec/fmt_alias/fmt_alias_spec.rb
101
+ - spec/spec_helper.rb