comma_separated_storage 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
+ .#*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in comma_separated_storage.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 conanite
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.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # CommaSeparatedStorage
2
+
3
+ Given an object with a string attribute containing a comma-separated list of items,
4
+ this gem makes it easier to deal with the list even though it is stored as a string
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'comma_separated_storage'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install comma_separated_storage
19
+
20
+ ## Usage
21
+
22
+ class Widget
23
+ extend CommaSeparatedStorage
24
+ attr_accessor :languages
25
+ comma_separated_storage :languages, :interrogate => :speaks?
26
+ end
27
+
28
+ widget = Widget.new
29
+ widget.languages = "de,jp,it"
30
+
31
+ widget.language_list # => ["de", "jp", "it"]
32
+ widget.default_language # => "de"
33
+
34
+ widget.speaks? "de" # => "de"
35
+ widget.speaks? "en" # => false
36
+
37
+ list = []
38
+ widget.each_language { |x|
39
+ list << x
40
+ }
41
+ list # => ["de", "jp", "it"]
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- coding: utf-8; mode: ruby -*-
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'comma_separated_storage/version'
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = "comma_separated_storage"
9
+ gem.version = CommaSeparatedStorage::VERSION
10
+ gem.authors = ["conanite"]
11
+ gem.email = ["conan@conandalton.net"]
12
+ gem.description = %q{Create utility methods to access an attribute as a list but store it as a comma-separated string}
13
+ gem.summary = %q{Given an object with a string attribute containing a comma-separated list of items,
14
+ this gem makes it easier to deal with the list even though it is stored as a string}
15
+
16
+ gem.homepage = "https://github.com/conanite/polyglot"
17
+
18
+ gem.add_development_dependency 'rspec', '~> 2.9'
19
+
20
+ gem.files = `git ls-files`.split($/)
21
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
22
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
23
+ gem.require_paths = ["lib"]
24
+ end
@@ -0,0 +1,36 @@
1
+ require "comma_separated_storage/version"
2
+
3
+ module CommaSeparatedStorage
4
+ def comma_separated_storage attribute, options={ }
5
+ name = attribute.to_s
6
+ singular = options[:singular] || (name.respond_to?(:singularize) ? name.singularize : name.sub(/s$/, ''))
7
+ interrogator = options[:interrogate] || :"has_#{singular}?"
8
+
9
+ line = __LINE__ + 1
10
+ code = %{
11
+ def #{singular}_list= array
12
+ self.#{attribute}= array.join(',')
13
+ end
14
+
15
+ def #{singular}_list
16
+ (self.#{attribute} || "").split(/,/)
17
+ end
18
+
19
+ def #{interrogator} item
20
+ #{singular}_list.include?(item.to_s) ? item : false
21
+ end
22
+
23
+ def multi_#{singular}; #{singular}_list.length > 1; end
24
+ def each_#{singular}; #{singular}_list.each { |item| yield item }; end
25
+ def default_#{singular}; #{singular}_list.first.to_sym; end
26
+
27
+ def single_#{singular}
28
+ list = #{singular}_list
29
+ return list[0] if list.size == 1
30
+ end
31
+ }
32
+
33
+ class_eval code, __FILE__, line
34
+ end
35
+ end
36
+
@@ -0,0 +1,3 @@
1
+ module CommaSeparatedStorage
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,128 @@
1
+ require "spec_helper"
2
+
3
+ describe CommaSeparatedStorage do
4
+ class SingularizingString < String
5
+ def singularize; self; end
6
+ def to_s; self; end
7
+ end
8
+
9
+ class Widget
10
+ extend CommaSeparatedStorage
11
+ attr_accessor :languages, :doors, :sheep
12
+ comma_separated_storage :languages, :interrogate => :speaks?
13
+ comma_separated_storage :doors
14
+ comma_separated_storage SingularizingString.new("sheep")
15
+ end
16
+
17
+ describe "languages" do
18
+ let(:widget) { Widget.new }
19
+
20
+ it "should return the list of languages available for the widget" do
21
+ widget.languages = "fr,en,de"
22
+ widget.language_list.should == %w{ fr en de }
23
+ widget.default_language.should == :fr
24
+ widget.speaks?("fr").should == "fr"
25
+ widget.speaks?("en").should == "en"
26
+ widget.speaks?("de").should == "de"
27
+ widget.speaks?("ru").should == false
28
+ widget.speaks?("it").should == false
29
+ widget.speaks?("jp").should == false
30
+ end
31
+
32
+ it "should return default language as a sym" do
33
+ widget.languages = "it,fr,en,de"
34
+ widget.default_language.should == :it
35
+ widget.speaks?("ru").should == false
36
+ widget.speaks?("it").should == "it"
37
+ end
38
+
39
+ it "should return default language if it is the only one" do
40
+ widget.languages = "it"
41
+ widget.single_language.should == "it"
42
+ end
43
+
44
+ it "should return nil if it has several languages" do
45
+ widget.languages = "it,fr,en,ie"
46
+ widget.single_language.should == nil
47
+ end
48
+ end
49
+
50
+ describe "language_list" do
51
+ let(:widget) { Widget.new }
52
+
53
+ it "should set the #languages attribute" do
54
+ widget.language_list = %w{ en fr ru jp }
55
+ widget.languages.should == "en,fr,ru,jp"
56
+ end
57
+
58
+ it "should read the #languages attribute" do
59
+ widget.languages = "en,fr,ru,jp"
60
+ widget.language_list.should == %w{ en fr ru jp }
61
+ end
62
+
63
+ it "doesn't die when #languages is nil" do
64
+ widget.language_list.should == []
65
+ end
66
+
67
+ it "doesn't die when #languages is empty" do
68
+ widget.languages = ""
69
+ widget.language_list.should == []
70
+ end
71
+ end
72
+
73
+ describe "each_lang" do
74
+ let(:widget) { Widget.new }
75
+
76
+ it "should set the #languages attribute" do
77
+ widget.language_list = %w{ en fr ru jp }
78
+ list = []
79
+ widget.each_language { |x|
80
+ list << x
81
+ }
82
+ list.should == %w{ en fr ru jp }
83
+ end
84
+ end
85
+
86
+ describe "doors" do
87
+ let(:widget) { Widget.new }
88
+ it "should generate :has_door?" do
89
+ widget.doors = "tall,short,medium"
90
+ widget.has_door?("tall").should == "tall"
91
+ widget.has_door?("small").should == false
92
+ end
93
+
94
+ it "should generate :multi_door" do
95
+ widget.doors = "tall,short,medium"
96
+ widget.multi_door == true
97
+ widget.single_door == nil
98
+ end
99
+
100
+ it "should generate :multi_door" do
101
+ widget.doors = "short"
102
+ widget.multi_door == false
103
+ widget.single_door == "short"
104
+ end
105
+ end
106
+
107
+ describe "with :singularize" do
108
+ let(:widget) { Widget.new }
109
+ it "should generate :has_sheep?" do
110
+ widget.sheep = "Tim,Tom,Ben,Jim"
111
+ widget.sheep_list.should == %w{ Tim Tom Ben Jim }
112
+ widget.has_sheep?("Tim").should == "Tim"
113
+ widget.has_sheep?("Ken").should == false
114
+ end
115
+
116
+ it "should generate :multi_sheep" do
117
+ widget.sheep = "Tim,Tom,Ben"
118
+ widget.multi_sheep == true
119
+ widget.single_sheep == nil
120
+ end
121
+
122
+ it "should generate :multi_sheep" do
123
+ widget.sheep = "Jim"
124
+ widget.multi_sheep == false
125
+ widget.single_sheep == "Jim"
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,19 @@
1
+ require "comma_separated_storage"
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # Require this file using `require "spec_helper"` to ensure that it is only
6
+ # loaded once.
7
+ #
8
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: comma_separated_storage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - conanite
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.9'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.9'
30
+ description: Create utility methods to access an attribute as a list but store it
31
+ as a comma-separated string
32
+ email:
33
+ - conan@conandalton.net
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - .rspec
40
+ - Gemfile
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - comma_separated_storage.gemspec
45
+ - lib/comma_separated_storage.rb
46
+ - lib/comma_separated_storage/version.rb
47
+ - spec/comma_separated_storage_spec.rb
48
+ - spec/spec_helper.rb
49
+ homepage: https://github.com/conanite/polyglot
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.24
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Given an object with a string attribute containing a comma-separated list
73
+ of items, this gem makes it easier to deal with the list even though it is stored
74
+ as a string
75
+ test_files:
76
+ - spec/comma_separated_storage_spec.rb
77
+ - spec/spec_helper.rb