acts_as_array 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -2
- data/CHANGELOG.md +6 -0
- data/README.md +1 -1
- data/acts_as_array.gemspec +2 -1
- data/lib/acts_as_array.rb +27 -6
- data/spec/model.rb +1 -2
- data/spec/spec_helper.rb +3 -3
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 055e352928682be4e789b935fd3c0db23c30ca2f
|
4
|
+
data.tar.gz: 2cf1a1f51e261d44a649e2b7a86cb91ef40f864d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e5bc2fefbb4358cd6edad6b7b96b12611229c70667af934a0cdc8376edaceb9e2b5b50aef3f03679b5ad6f125eb149478b74aa8b7dcc766578ca71a121fe5b8
|
7
|
+
data.tar.gz: 5f7ba42cf59bc3efc306d2a8be18a97bd4eec48a9a84ee322d6540ee7f0d4607bb4cbc570d2263b36b9b608d101043ccf793ff3e4797fa3e2b8228618e52a4cd
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
data/acts_as_array.gemspec
CHANGED
@@ -3,12 +3,12 @@
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "acts_as_array"
|
6
|
+
s.version = '0.0.2'
|
6
7
|
s.authors = ["Naotoshi Seo"]
|
7
8
|
s.email = ["sonots@gmail.com"]
|
8
9
|
s.homepage = "https://github.com/sonots/acts_as_array"
|
9
10
|
s.summary = "Treat array fields simply"
|
10
11
|
s.description = "Treat array fields simply."
|
11
|
-
s.version = '0.0.1'
|
12
12
|
s.date = Time.now.strftime("%Y-%m-%d")
|
13
13
|
|
14
14
|
s.extra_rdoc_files = Dir["*.rdoc"]
|
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.require_paths = ["lib"]
|
19
19
|
s.rdoc_options = ["--charset=UTF-8"]
|
20
20
|
|
21
|
+
s.add_runtime_dependency 'activesupport'
|
21
22
|
s.add_development_dependency 'rspec'
|
22
23
|
s.add_development_dependency 'rake'
|
23
24
|
s.add_development_dependency 'pry'
|
data/lib/acts_as_array.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_support/core_ext'
|
2
|
+
|
1
3
|
module ActsAsArray
|
2
4
|
def self.included(klass)
|
3
5
|
klass.extend(ClassMethods)
|
@@ -6,14 +8,20 @@ module ActsAsArray
|
|
6
8
|
module ClassMethods
|
7
9
|
# acts_as_array :has_many_field => {:class => Class, :field => :name}
|
8
10
|
def acts_as_array(params = {})
|
11
|
+
params.each do |field, opts|
|
12
|
+
opts[:class] ||= field.to_s.singularize.capitalize.constantize
|
13
|
+
end
|
9
14
|
self.class_eval do
|
10
15
|
params.each do |field, opts|
|
11
|
-
#
|
16
|
+
# Setter with an array mapper
|
12
17
|
#
|
13
18
|
# Example:
|
14
19
|
#
|
15
20
|
# mails = ['a@a.com', 'b@b.com']
|
16
|
-
#
|
21
|
+
#
|
22
|
+
# instead of
|
23
|
+
#
|
24
|
+
# mails = [Mail.new(name: 'a@a.com'), Mail.new(name: 'b@b.com')]
|
17
25
|
unless method_defined?("#{field}_with_arraymap=")
|
18
26
|
define_method("#{field}_with_arraymap=") do |raw_array|
|
19
27
|
obj_array = __send__("make_#{field}", raw_array)
|
@@ -24,10 +32,15 @@ module ActsAsArray
|
|
24
32
|
alias_method("#{field}=", "#{field}_with_arraymap=")
|
25
33
|
end
|
26
34
|
|
35
|
+
# Getter with an array mapper
|
36
|
+
#
|
27
37
|
# Example:
|
28
38
|
#
|
29
|
-
#
|
30
|
-
#
|
39
|
+
# mails #=> ['a@a.com', 'b@b.com']
|
40
|
+
#
|
41
|
+
# instead of
|
42
|
+
#
|
43
|
+
# mails #=> [Mail.new(name: 'a@a.com'), Mail.new(name: 'b@b.com')]
|
31
44
|
unless method_defined?("#{field}_with_arraymap")
|
32
45
|
define_method("#{field}_with_arraymap") do
|
33
46
|
obj_array = __send__("#{field}_without_arraymap")
|
@@ -38,17 +51,25 @@ module ActsAsArray
|
|
38
51
|
alias_method("#{field}", "#{field}_with_arraymap")
|
39
52
|
end
|
40
53
|
|
54
|
+
# Get the original object array
|
55
|
+
#
|
56
|
+
# Example:
|
57
|
+
#
|
41
58
|
# obj_mails #=> [Mail.new(name: 'a@a.com'), Mail.new(name: 'b@b.com')]
|
42
59
|
define_method("obj_#{field}") do
|
43
60
|
__send__("#{field}_without_arraymap")
|
44
61
|
end
|
45
62
|
|
46
|
-
#
|
63
|
+
# Convert the single value array to object array
|
47
64
|
#
|
48
65
|
# Example:
|
49
66
|
#
|
50
67
|
# make_mails(['a@a.com', 'b@b.com'])
|
51
|
-
#
|
68
|
+
# #=> [Mail.new(name: 'a@a.com'), Mail.new(name: 'b@b.com')]
|
69
|
+
#
|
70
|
+
# Note:
|
71
|
+
#
|
72
|
+
# This method uses the `#find_or_initialize_by` method internally
|
52
73
|
define_method("make_#{field}") do |raw_array|
|
53
74
|
return nil unless raw_array
|
54
75
|
raw_array.map {|val| opts[:class].find_or_initialize_by(opts[:field] => val) }
|
data/spec/model.rb
CHANGED
@@ -25,6 +25,5 @@ class User < ActiveRecord::Base
|
|
25
25
|
include ActsAsArray
|
26
26
|
has_many :mails
|
27
27
|
has_many :phones
|
28
|
-
acts_as_array :mails => {:
|
29
|
-
:phones => {:class => Phone, :field => :name}
|
28
|
+
acts_as_array :mails => {:field => :name}, :phones => {:field => :name}
|
30
29
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -9,9 +9,9 @@ ActiveRecord::Base.establish_connection(
|
|
9
9
|
:database => 'db/sqlite.db'
|
10
10
|
)
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
require 'rake'
|
13
|
+
require_relative 'migrate_tasks'
|
14
|
+
Rake::Task['db:schema:load'].invoke
|
15
15
|
|
16
16
|
if ENV['TRAVIS']
|
17
17
|
require 'coveralls'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_array
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Naotoshi Seo
|
@@ -10,6 +10,20 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2014-06-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rspec
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|