format_alias 0.0.4 → 0.0.5

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3334f7494cf3e02c4a0082fed0e472ebb924067c
4
+ data.tar.gz: 0c85b6c7f2b0ae3263af9760b139481bde2c3436
5
+ SHA512:
6
+ metadata.gz: 8d1cc3e40ae9ffad7a77b7ef1e87faa1699b2b97956b604f404ce9e327898768f4b708c90985ffaf0686dbd763a866dc19db831da75178af4e2e5fbdd6b68b54
7
+ data.tar.gz: 15bb72c6962af0cf2393cf31305bd365a47c15d292feab87cf65bcc900c7ca72f0e4e578306436afc83c11152da36399709933def9f527712bb0f185879e614a
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.1
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # Format Alias
2
+
3
+ Provides virtual attributes to get/set certain model values in/to human
4
+ readable format.
5
+
6
+ ## Date formatting
7
+
8
+ ```ruby
9
+ class Foo
10
+ extend FormatAlias
11
+
12
+ attr_accessor :created_at
13
+ date_format_alias :created_at, '%d.%m.%Y'
14
+ end
15
+
16
+ bar = Foo.new
17
+ bar.created_at_formatted = '12.01.2001'
18
+ bar.created_at # => Sat, 01 Dec 2001
19
+ ```
20
+
21
+ Accepts options:
22
+ * `:suffix` - `formatted` by default
23
+ * `:getter_name` - name of a getter method
24
+ * `:setter_name` - name of a setter method (without '=')
25
+
26
+ ## Setting polymorphic attribute
27
+
28
+ Useful when you want to set variables from such select:
29
+
30
+ ```html
31
+ <select name="imageable">
32
+ <option value="Image:3">Image</option>
33
+ <option value="Banner:1">Banner</option>
34
+ ...
35
+ </select>
36
+ ```
37
+ ```ruby
38
+ class Foo < ActiveRecord::Base
39
+ extend FormatAlias
40
+ belongs_to :imageable
41
+ polymorphic_alias :imageable
42
+ end
43
+
44
+ bar = Foo.new
45
+ bar.imageable_formatted = "Image:3"
46
+ bar.imageable_id # => 3
47
+ bar.imageable_type # => "Image"
48
+ ```
49
+
50
+ ## Roll your own
51
+
52
+ ## Installation
53
+
54
+ Add this line to your application's Gemfile:
55
+
56
+ gem 'sidekiq-batching'
57
+
58
+ And then execute:
59
+
60
+ $ bundle
61
+
62
+ Or install it yourself as:
63
+
64
+ $ gem install sidekiq-batching
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it ( http://github.com/gzigzigzeo/format_alias/fork )
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create new Pull Request
data/format_alias.gemspec CHANGED
@@ -8,8 +8,14 @@ Gem::Specification.new do |s|
8
8
  s.authors = ["Victor Sokolov"]
9
9
  s.email = ["gzigzigzeo@gmail.com"]
10
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.}
11
+ s.summary = %q{
12
+ Provides virtual attributes to get or set model values in human
13
+ readable format.
14
+ }
15
+ s.description = %q{
16
+ Provides virtual attributes to get or set certain model values (like
17
+ dates) in human readable/localized format.
18
+ }
13
19
 
14
20
  s.rubyforge_project = "format_alias"
15
21
 
@@ -17,10 +23,9 @@ Gem::Specification.new do |s|
17
23
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
24
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
25
  s.require_paths = ["lib"]
20
-
26
+
21
27
  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')
28
+ s.add_dependency 'activesupport', '>= 3.0'
29
+
30
+ s.add_development_dependency('rspec-rails')
26
31
  end
@@ -0,0 +1,12 @@
1
+ module FormatAlias
2
+ def date_format_alias(name, format, options = {})
3
+ define_setter_alias(name, options) do |value|
4
+ time = Time.parse(value, format) rescue nil
5
+ { name => time }
6
+ end
7
+
8
+ define_getter_alias(name, options) do |value|
9
+ value.strftime(format) unless value.nil?
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ module FormatAlias
2
+ def polymorphic_alias(name, options = {})
3
+ delimiter = options.delete(:delimiter) || ':'
4
+ attributes = ["#{name}_type", "#{name}_id"]
5
+
6
+ options_with_attributes = options.merge(attributes: attributes)
7
+
8
+ define_setter_alias(name, options_with_attributes) do |value|
9
+ values = value.to_s.split(delimiter)
10
+ Hash[attributes.zip(values)]
11
+ end
12
+
13
+ define_getter_alias(name, options_with_attributes) do |*values|
14
+ values = values.compact
15
+ values.join(delimiter) if values.any?
16
+ end
17
+ end
18
+ end
@@ -1,11 +1,6 @@
1
1
  module FormatAlias
2
2
  if defined?(Rails::Railtie)
3
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
4
  end
10
5
  end
11
- end
6
+ end
@@ -1,3 +1,3 @@
1
1
  module FormatAlias
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/format_alias.rb CHANGED
@@ -1,6 +1,41 @@
1
+ require 'time'
1
2
  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"
3
+ require 'active_support/core_ext/object/blank'
4
+ require 'format_alias/version'
5
+ require 'format_alias/formatters/date'
6
+ require 'format_alias/formatters/polymorphic'
7
+ require 'format_alias/railtie'
8
+
9
+ module FormatAlias
10
+ private
11
+ def define_setter_alias(name, options, &setter)
12
+ define_method setter_name(name, options) do |value|
13
+ setter.call(value).each { |name, value| public_send("#{name}=", value) }
14
+ end
15
+ end
16
+
17
+ def define_getter_alias(name, options, &getter)
18
+ define_method getter_name(name, options) do
19
+ attributes = options[:attributes] || [name]
20
+ getter.call(*attributes.map { |name| public_send(name) })
21
+ end
22
+ end
23
+
24
+ def alias_name(name, options, type)
25
+ if options[:suffix].present?
26
+ "#{name}_#{options[:suffix]}"
27
+ elsif options[:"#{type}_name"].present?
28
+ "#{options[:"#{type}_name"]}"
29
+ else
30
+ "#{name}_formatted"
31
+ end
32
+ end
33
+
34
+ def setter_name(name, options)
35
+ "#{alias_name(name, options, :setter)}="
36
+ end
37
+
38
+ def getter_name(name, options)
39
+ alias_name(name, options, :getter)
40
+ end
41
+ end
@@ -6,49 +6,53 @@ describe "FormatAlias spec" do
6
6
  end
7
7
 
8
8
  context "date/time conversions" do
9
- it "should correctly convert dates" do
9
+ it "correctly converts dates" do
10
10
  d = DateTime.new(2009, 1, 1)
11
11
  @t.d = d
12
- @t.d_formatted.should eq("20090101")
13
-
12
+ expect(@t.d_formatted).to eq("20090101")
13
+
14
14
  @t.d_formatted = nil
15
- @t.d.should be_nil
16
-
15
+ expect(@t.d).to be_nil
16
+
17
17
  @t.d_formatted = "20090202"
18
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)
19
+ expect(@t.d.year).to eq(2009)
20
+ expect(@t.d.month).to eq(2)
21
+ expect(@t.d.day).to eq(2)
22
22
  end
23
-
24
- it "should correctly convert dates with time zones" do
23
+
24
+ it "correctly converts dates with time zones" do
25
25
  d = DateTime.new(2009, 1, 1, 12, 0)
26
26
  @t.dt = d
27
27
  @t.dt_formatted.should eq("20090101 12:00")
28
+ end
28
29
 
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
30
+ it "works with suffix option" do
31
+ @t.dy_informatted = "20090202"
32
+ expect(@t.dy_informatted).to eq("2009")
33
+ end
34
+
35
+ it "works with getter/setter names option" do
36
+ @t.b = "20090202"
37
+ expect(@t.a).to eq("2009")
34
38
  end
35
39
  end
36
-
40
+
37
41
  context "polymorphic conversions" do
38
42
  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
43
+ @t.poly_formatted = "Page:3"
44
+ expect(@t.poly_type).to eq('Page')
45
+ expect(@t.poly_id).to eq('3')
46
+
47
+ @t.poly_formatted = "Banner:4"
48
+ expect(@t.poly_formatted).to eq("Banner:4")
49
+
50
+ @t.poly_formatted = nil
51
+ expect(@t.poly_formatted).to eq(nil)
48
52
 
49
53
  @t.poly_type = nil
50
54
  @t.poly_id = 5
51
- @t.poly_type_id.should == "5"
55
+ expect(@t.poly_formatted).to eq("5")
52
56
  end
53
57
  end
54
58
  end
data/spec/spec_helper.rb CHANGED
@@ -1,15 +1,3 @@
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
-
1
+ require 'bundler/setup'
2
+ require 'format_alias'
13
3
  require 'support/test_class'
14
-
15
- $: << File.join(File.dirname(__FILE__), '..', 'lib')
@@ -1,13 +1,15 @@
1
1
  class TestClass
2
- include FormatAlias::Base
3
-
4
- attr_accessor :d, :dt
2
+ extend FormatAlias
3
+
4
+ attr_accessor :d, :dt, :dy, :dg
5
5
  attr_accessor :poly_id, :poly_type
6
6
  attr_accessor :value1, :value2, :value3
7
-
7
+
8
8
  date_format_alias(:d, "%Y%m%d")
9
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])
10
+ date_format_alias(:dy, "%Y", suffix: 'informatted')
11
+ date_format_alias(:dg, "%Y", getter_name: 'a', setter_name: 'b')
12
+
13
+ polymorphic_alias(:poly)
14
+ # polymorphic_alias(:values, attrs: [:value1, :value2, :value3])
13
15
  end
metadata CHANGED
@@ -1,140 +1,104 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
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
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Victor Sokolov
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2011-06-29 00:00:00 +04:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
11
+ date: 2014-05-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
22
14
  name: activerecord
23
- version_requirements: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
26
17
  - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 7
29
- segments:
30
- - 3
31
- - 0
32
- version: "3.0"
33
- prerelease: false
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
34
20
  type: :runtime
35
- requirement: *id001
36
- - !ruby/object:Gem::Dependency
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
37
28
  name: activesupport
38
- version_requirements: &id002 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
41
31
  - - ">="
42
- - !ruby/object:Gem::Version
43
- hash: 7
44
- segments:
45
- - 3
46
- - 0
47
- version: "3.0"
48
- prerelease: false
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
49
34
  type: :runtime
50
- requirement: *id002
51
- - !ruby/object:Gem::Dependency
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
52
42
  name: rspec-rails
53
- version_requirements: &id003 !ruby/object:Gem::Requirement
54
- none: false
55
- requirements:
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
56
45
  - - ">="
57
- - !ruby/object:Gem::Version
58
- hash: 3
59
- segments:
60
- - 0
61
- version: "0"
62
- prerelease: false
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
63
48
  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
49
  prerelease: false
77
- type: :development
78
- requirement: *id004
79
- description: ActiveRecord attribute accessor aliases for date formatting etc.
80
- email:
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: "\n Provides virtual attributes to get or set certain model values
56
+ (like\n dates) in human readable/localized format.\n "
57
+ email:
81
58
  - gzigzigzeo@gmail.com
82
59
  executables: []
83
-
84
60
  extensions: []
85
-
86
61
  extra_rdoc_files: []
87
-
88
- files:
89
- - .gitignore
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
90
65
  - Gemfile
91
- - README.rdoc
66
+ - README.md
92
67
  - Rakefile
93
68
  - format_alias.gemspec
94
69
  - 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
70
+ - lib/format_alias/formatters/date.rb
71
+ - lib/format_alias/formatters/polymorphic.rb
98
72
  - lib/format_alias/railtie.rb
99
73
  - lib/format_alias/version.rb
100
74
  - spec/format_alias/format_alias_spec.rb
101
75
  - spec/spec_helper.rb
102
76
  - spec/support/test_class.rb
103
- has_rdoc: true
104
77
  homepage: http://github.com/gzigzigzeo/format_alias
105
78
  licenses: []
106
-
79
+ metadata: {}
107
80
  post_install_message:
108
81
  rdoc_options: []
109
-
110
- require_paths:
82
+ require_paths:
111
83
  - lib
112
- required_ruby_version: !ruby/object:Gem::Requirement
113
- none: false
114
- requirements:
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
115
86
  - - ">="
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:
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
124
91
  - - ">="
125
- - !ruby/object:Gem::Version
126
- hash: 3
127
- segments:
128
- - 0
129
- version: "0"
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
130
94
  requirements: []
131
-
132
95
  rubyforge_project: format_alias
133
- rubygems_version: 1.6.2
96
+ rubygems_version: 2.2.2
134
97
  signing_key:
135
- specification_version: 3
136
- summary: ActiveRecord attribute accessor aliases for date formatting etc.
137
- test_files:
98
+ specification_version: 4
99
+ summary: Provides virtual attributes to get or set model values in human readable
100
+ format.
101
+ test_files:
138
102
  - spec/format_alias/format_alias_spec.rb
139
103
  - spec/spec_helper.rb
140
104
  - spec/support/test_class.rb
data/README.rdoc DELETED
@@ -1,44 +0,0 @@
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...
@@ -1,20 +0,0 @@
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
@@ -1,14 +0,0 @@
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
@@ -1,35 +0,0 @@
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