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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 120a75d054520211cc914c9fd78ad4f1462fb23a
4
- data.tar.gz: 8477ac317dacdd580e5d36ff6eea21c8b544ec90
3
+ metadata.gz: 055e352928682be4e789b935fd3c0db23c30ca2f
4
+ data.tar.gz: 2cf1a1f51e261d44a649e2b7a86cb91ef40f864d
5
5
  SHA512:
6
- metadata.gz: a29c33a7618a5e1533e6b58a801ecda5e5cbe0289be94574e6a715eb2b1bb1f9172179f6b8b84be3c46bac64544b9ea7c5fd2866a7f67a1a9721cc7a149c6086
7
- data.tar.gz: fa00f4b6bc2a105237c77d1b5c441e8973699eb423ba1e1fbede0eecf5e44a2cf2f50cd67187c629b492b878da2808cee1d22065b28a2743de83b24c6f03f1f4
6
+ metadata.gz: 1e5bc2fefbb4358cd6edad6b7b96b12611229c70667af934a0cdc8376edaceb9e2b5b50aef3f03679b5ad6f125eb149478b74aa8b7dcc766578ca71a121fe5b8
7
+ data.tar.gz: 5f7ba42cf59bc3efc306d2a8be18a97bd4eec48a9a84ee322d6540ee7f0d4607bb4cbc570d2263b36b9b608d101043ccf793ff3e4797fa3e2b8228618e52a4cd
@@ -1,8 +1,11 @@
1
1
  rvm:
2
- - 1.9.3
3
2
  - 2.0.0
4
- - 2.1.0
3
+ - 2.1
5
4
  - rbx-2
6
5
  - jruby-19mode
7
6
  gemfile:
8
7
  - Gemfile
8
+ matrix:
9
+ allow_failures:
10
+ - rvm: rbx-2
11
+ - rvm: jruby-19mode
@@ -1,3 +1,9 @@
1
+ ## 0.0.2 (2014/06/04)
2
+
3
+ Enhancements:
4
+
5
+ * Automatically configure the :class name
6
+
1
7
  ## 0.0.1
2
8
 
3
9
  First version
data/README.md CHANGED
@@ -78,7 +78,7 @@ require 'acts_as_array'
78
78
  class User < ActiveRecord::Base
79
79
  has_many :mails
80
80
  include ActsAsArray
81
- acts_as_array :mails => {:class => Mail, :field => :address}
81
+ acts_as_array :mails => {:field => :address}
82
82
  end
83
83
  ```
84
84
 
@@ -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'
@@ -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
- # xxxx = raw_array #=> obj_array
16
+ # Setter with an array mapper
12
17
  #
13
18
  # Example:
14
19
  #
15
20
  # mails = ['a@a.com', 'b@b.com']
16
- # #=> [Mail.new(name: 'a@a.com'), Mail.new(name: 'b@b.com')]
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
- # mails_without_arraymap #=> [Mail.new(name: 'a@a.com'), Mail.new(name: 'b@b.com')]
30
- # mails | mails_with_arraymap #=> ['a@a.com', 'b@b.com']
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
- # make_xxxx(raw_array) #=> obj_array
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
- # #=> [Mail.new(name: 'a@a.com'), Mail.new(name: 'b@b.com')]
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) }
@@ -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 => {:class => Mail, :field => :name},
29
- :phones => {:class => Phone, :field => :name}
28
+ acts_as_array :mails => {:field => :name}, :phones => {:field => :name}
30
29
  end
@@ -9,9 +9,9 @@ ActiveRecord::Base.establish_connection(
9
9
  :database => 'db/sqlite.db'
10
10
  )
11
11
 
12
- #require 'rake'
13
- #require_relative 'migrate_tasks'
14
- #Rake::Task['db:schema:load'].invoke
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.1
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