coerced_accessor 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f7145312b0c3b70a97a9cd0e3ffb444e85cb5963
4
+ data.tar.gz: d0a1ead53be0fde30753b896a12a2ec9d3285c52
5
+ SHA512:
6
+ metadata.gz: 9d91228a03b74eb7b0c512f6b4278333e4e7044632d228a0368fafdcf05768bcf81bdcbadde218128286f140d7421fa974c5a646c961397d59a4a249bd3ff1e7
7
+ data.tar.gz: f12ca4be50acb1b452163ebc80c028faa0fb5b52a68a1814e991858fb11824b49132afb1d3f3ce94ab2b8a8ed18bd7169aac0365befdb6f0d7963ebf303baec9
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in coerced_accessor.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Viktor Sokolov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,87 @@
1
+ # CoercedAccessor
2
+
3
+ Provides object attribute aliases coercing values to/from human readable format
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'coerced_accessor'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install coerced_accessor
18
+
19
+ ## Human readable dates
20
+
21
+ ```ruby
22
+ class UserForm
23
+ extend CoercedAccessor::Time
24
+
25
+ attr_accessor :birthdate
26
+ attr_accessor :meeting_at
27
+
28
+ time_coerced_accessor :birthdate, "%d.%m.%Y", time_class: Date
29
+ time_coerced_accessor :meeting_at, "%d.%m.%Y %H:%M", as: :meeting_time
30
+ end
31
+
32
+ form = UserForm.new
33
+ form.birthdate_human = '12.01.2001'
34
+ form.birthdate # => Sat, 01 Dec 2001
35
+
36
+ form.meeting_time = '18.04.1983'
37
+ form.meeting_at # => Mon, 18 Apr 1983 00:00:00 +0000
38
+ ```
39
+
40
+ ## Multiple polymorhic entities through single attrbute
41
+
42
+ ```html
43
+ <select name="imageable">
44
+ <option value="Image:3">Image</option>
45
+ <option value="Banner:1">Banner</option>
46
+ ...
47
+ </select>
48
+ ```
49
+
50
+ ```ruby
51
+ class User < ActiveRecord::Base
52
+ extend CoercedAccessor::Compound
53
+
54
+ belongs_to :imageable
55
+ compound_accessor :imageable_formatted, :imageable_id, :imageable_type
56
+ end
57
+
58
+ user = User.new
59
+ user.imageable_formatted = "Image:3"
60
+ user.imageable_id # => 3
61
+ user.imageable_type # => "Image"
62
+ ```
63
+
64
+ ## Single joined attribute through partial accessors
65
+
66
+ ```ruby
67
+ class TimeForm
68
+ extend CoercedAccessor::Compound
69
+
70
+ attr_accessor :time
71
+
72
+ compound_accessor :time, :hour, :minute, delimiter: ':'
73
+ end
74
+
75
+ tree = TimeForm.new
76
+ tree.hour = '03'
77
+ tree.minute = '50'
78
+ tree.path # => '03:50'
79
+ ```
80
+
81
+ ## Contributing
82
+
83
+ 1. Fork it ( https://github.com/gzigzigzeo/coerced_accessor/fork )
84
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
85
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
86
+ 4. Push to the branch (`git push origin my-new-feature`)
87
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'coerced_accessor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "coerced_accessor"
8
+ spec.version = CoercedAccessor::VERSION
9
+ spec.authors = ["Viktor Sokolov"]
10
+ spec.email = ["gzigzigzeo@evilmartians.com"]
11
+ spec.summary = %q{Provides object attribute aliases coercing values to/from human readable format}
12
+ spec.description = %q{Provides object attribute aliases coercing values to/from human readable format}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "simplecov"
25
+ spec.add_development_dependency "activesupport"
26
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_support/core_ext/array/extract_options'
2
+ require "coerced_accessor/version"
3
+ require "coerced_accessor/time"
4
+ require "coerced_accessor/compound"
5
+ require "coerced_accessor/split"
6
+
7
+ module CoercedAccessor
8
+ end
@@ -0,0 +1,17 @@
1
+ module CoercedAccessor
2
+ module Compound
3
+ def compound_accessor(accessor, *attributes)
4
+ options = attributes.extract_options!
5
+ delimiter = options.delete(:delimiter) || ':'
6
+
7
+ define_method accessor do
8
+ attributes.map { |a| send(a) }.join(delimiter)
9
+ end
10
+
11
+ define_method "#{accessor}=" do |value|
12
+ values = value.split(delimiter)
13
+ attributes.map { |a| send("#{a}=", values.shift) }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ module CoercedAccessor
2
+ module Split
3
+ def split_accessors(attribute, *accessors)
4
+ options = accessors.extract_options!
5
+ delimiter = options.delete(:delimiter) || ':'
6
+
7
+ accessors.each.with_index do |accessor, index|
8
+ define_method accessor do
9
+ send(attribute).to_s.split(delimiter)[index]
10
+ end
11
+
12
+ define_method "#{accessor}=" do |value|
13
+ values = accessors.map { |a| send(a) }
14
+ values[index] = value
15
+ send("#{attribute}=", values.join(delimiter))
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ module CoercedAccessor
2
+ module Time
3
+ def time_coerced_accessor(attribute, format = nil, options = {})
4
+ as = options.delete(:as) || "#{attribute}_human"
5
+ time_class = options.delete(:time_class) || ::Time
6
+
7
+ define_method("#{as}=") do |value|
8
+ time = time_class.parse(value, format) rescue nil
9
+ send("#{attribute}=", time)
10
+ end
11
+
12
+ define_method(as) do
13
+ value = send(attribute)
14
+ value.strftime(format) if value
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module CoercedAccessor
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe CoercedAccessor::Compound do
4
+ subject { CompoundMock.new }
5
+
6
+ it do
7
+ subject.subject = 'a/b'
8
+ expect(subject.subject_id).to eq('a')
9
+ expect(subject.subject_type).to eq('b')
10
+
11
+ subject.subject_id = 8
12
+ expect(subject.subject).to eq('8/b')
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe CoercedAccessor::Compound do
4
+ subject { SplitMock.new }
5
+
6
+ it do
7
+ subject.subject_id = 1
8
+ subject.subject_type = 'User'
9
+ expect(subject.subject).to eq('1:User')
10
+ end
11
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe CoercedAccessor::Time do
4
+ subject { TimeMock.new }
5
+
6
+ let(:date) { DateTime.new(2009, 1, 1) }
7
+ let(:date_with_time) { DateTime.new(2009, 1, 1, 12, 0) }
8
+
9
+ it 'converts dates' do
10
+ subject.date = date
11
+ expect(subject.date_human).to eq("20090101")
12
+
13
+ subject.date_human = nil
14
+ expect(subject.date).to be_nil
15
+
16
+ subject.date_human = "20090202"
17
+ expect(subject.date).to_not be_nil
18
+
19
+ expect(subject.date.year).to eq(2009)
20
+ expect(subject.date.month).to eq(2)
21
+ expect(subject.date.day).to eq(2)
22
+ end
23
+
24
+ it 'handles time zones' do
25
+ subject.date_with_time = date_with_time
26
+ expect(subject.date_with_time_human).to eq("20090101 12:00")
27
+ end
28
+
29
+ it ':as option works' do
30
+ subject.year = "20090202"
31
+ expect(subject.year).to eq("2009")
32
+ end
33
+ end
@@ -0,0 +1,24 @@
1
+ $LOAD_PATH << "." unless $LOAD_PATH.include?(".")
2
+
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+ require 'simplecov'
6
+ require 'time'
7
+ require 'date'
8
+
9
+ SimpleCov.start do
10
+ add_filter 'spec'
11
+ end
12
+
13
+ require 'coerced_accessor'
14
+ require 'support/time_mock'
15
+ require 'support/compound_mock'
16
+ require 'support/split_mock'
17
+
18
+ RSpec.configure do |config|
19
+ config.order = :random
20
+ config.filter_run(:focus)
21
+ config.run_all_when_everything_filtered = true
22
+ end
23
+
24
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
@@ -0,0 +1,7 @@
1
+ class CompoundMock
2
+ extend CoercedAccessor::Compound
3
+
4
+ attr_accessor :subject_id, :subject_type
5
+
6
+ compound_accessor :subject, :subject_id, :subject_type, delimiter: '/'
7
+ end
@@ -0,0 +1,7 @@
1
+ class SplitMock
2
+ extend CoercedAccessor::Split
3
+
4
+ attr_accessor :subject
5
+
6
+ split_accessors :subject, :subject_id, :subject_type
7
+ end
@@ -0,0 +1,9 @@
1
+ class TimeMock
2
+ extend CoercedAccessor::Time
3
+
4
+ attr_accessor :date, :date_with_time, :just_year
5
+
6
+ time_coerced_accessor(:date, "%Y%m%d")
7
+ time_coerced_accessor(:date_with_time, "%Y%m%d %H:%M")
8
+ time_coerced_accessor(:just_year, "%Y", as: :year)
9
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: coerced_accessor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Viktor Sokolov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Provides object attribute aliases coercing values to/from human readable
84
+ format
85
+ email:
86
+ - gzigzigzeo@evilmartians.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - coerced_accessor.gemspec
97
+ - lib/coerced_accessor.rb
98
+ - lib/coerced_accessor/compound.rb
99
+ - lib/coerced_accessor/split.rb
100
+ - lib/coerced_accessor/time.rb
101
+ - lib/coerced_accessor/version.rb
102
+ - spec/lib/compound_spec.rb
103
+ - spec/lib/split_spec.rb
104
+ - spec/lib/time_spec.rb
105
+ - spec/spec_helper.rb
106
+ - spec/support/compound_mock.rb
107
+ - spec/support/split_mock.rb
108
+ - spec/support/time_mock.rb
109
+ homepage: ''
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.2.2
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Provides object attribute aliases coercing values to/from human readable
133
+ format
134
+ test_files:
135
+ - spec/lib/compound_spec.rb
136
+ - spec/lib/split_spec.rb
137
+ - spec/lib/time_spec.rb
138
+ - spec/spec_helper.rb
139
+ - spec/support/compound_mock.rb
140
+ - spec/support/split_mock.rb
141
+ - spec/support/time_mock.rb