mastar 0.9.0 → 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5f83534e4c5c000b8d817261a63b40cff34f5ae3
4
+ data.tar.gz: 09122dcbd7eb624db45a52e226a5455f3efda27b
5
+ SHA512:
6
+ metadata.gz: 1efebc6ea5765c1c1cbc93ad0de69d79ad87935fb0337aee15d829734b892217a3e4d4e292028154596065379007750c20d25a6ce39316a5bd7a753bef468bb5
7
+ data.tar.gz: 5dcc9c9bc2ad945b42db9373592c5d1fbe1d42745835e9927163c949689f430e43022cfd91a0c7e910c9727d97466745629b6a2f3e096a339f6f84b138867b66
@@ -1,3 +1,9 @@
1
+ language: ruby
1
2
  rvm:
2
3
  - 1.9.3
3
-
4
+ - 2.0.0
5
+ - 2.1.0
6
+ gemfile:
7
+ - gemfiles/activerecord_3_2_x.gemfile
8
+ - gemfiles/activerecord_4_0_x.gemfile
9
+ # - gemfiles/activerecord_4_1_x.gemfile
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # Mastar
2
2
 
3
- TODO: Write a gem description
3
+ [![Build Status](https://secure.travis-ci.org/pinzolo/mastar.png)](http://travis-ci.org/pinzolo/mastar)
4
+ [![Coverage Status](https://coveralls.io/repos/pinzolo/mastar/badge.png)](https://coveralls.io/r/pinzolo/mastar)
5
+
6
+ Add some features to master table class on ActiveRecord.
4
7
 
5
8
  ## Installation
6
9
 
@@ -18,7 +21,51 @@ Or install it yourself as:
18
21
 
19
22
  ## Usage
20
23
 
21
- TODO: Write usage instructions here
24
+ 1. Call `include Mastar` in your ActiveRecord classes.
25
+ 2. Set key column name by way of `mastar.key` method.
26
+
27
+ ## Feature
28
+
29
+ ### Ruby code
30
+
31
+ ```ruby
32
+ class Country < ActiveRecord::Base
33
+ include Mastar
34
+ mastar.key :code
35
+ end
36
+
37
+ class City < ActiveRecord::Base
38
+ belongs_to :country
39
+ end
40
+ ```
41
+ ### Countries table data
42
+
43
+ | id | name | code |
44
+ |---:|:-----|:-----|
45
+ |1 |USA |us |
46
+ |2 |Japan |jp |
47
+
48
+ 1. Direct access by key method.
49
+ * `Country.jp` returns `#<Country id: 2, name: "Japan", code: "jp", ...>`
50
+ * `Country.jp(:name)` returns `"Japan"`
51
+ * `Country.jp(:code, :name, :id)` returns `["jp", "Japan", 2]`
52
+ 2. Judge method by key. (ex. `City.first.country.jp?`)
53
+ 3. `.get` method like as `.find`, `.get` preferentially returns cached object.
54
+ * `Country.find` returns from DB.
55
+ * `Country.get` returns from inner cache when already cached.
56
+ * When not cached, `Country.get` returns from DB, and cache it.
57
+ 4. `.pairs` method for writing select element shortly in Rails.
58
+ ```ruby
59
+ # Normal
60
+ f.select :country_id, Country.all.map { |c| [c.name, c.id] }
61
+ f.collection_select :country_id, Country.all, :id, :name
62
+ # In using Mastar
63
+ f.select :country_id, Country.pairs
64
+ ```
65
+ ## Supported versions
66
+
67
+ - Ruby: 1.9.3, 2.0.0, 2.1.0
68
+ - ActiveRecord: 3.2.x, 4.0.x
22
69
 
23
70
  ## Contributing
24
71
 
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activerecord", "~>3.2.0"
4
+
5
+ gemspec :path => "../"
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activerecord", "~>4.0.0"
4
+
5
+ gemspec :path => "../"
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activerecord", "~>4.1.0"
4
+
5
+ gemspec :path => "../"
@@ -1,16 +1,14 @@
1
1
  # coding: utf-8
2
+ require "active_support/concern"
2
3
  require 'mastar/version'
3
4
  require 'mastar/configuration'
4
5
  require 'mastar/name_value_pair'
5
6
 
6
7
  module Mastar
7
- def self.included(base)
8
- class << base
9
- alias_method :find!, :find
10
- end
11
- base.extend(ClassMethods)
12
- base.__send__(:include, InstanceMethods)
13
- base.__send__(:before_save, :remove_record_cache)
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ before_save :remove_record_cache
14
12
  end
15
13
 
16
14
  module ClassMethods
@@ -21,8 +19,8 @@ module Mastar
21
19
  self.select([name, value]).map { |r| NameValuePair.new(r.__send__(name), r.__send__(value)) }
22
20
  end
23
21
 
24
- def find(id)
25
- mastar_records[id] ||= find!(id)
22
+ def get(id)
23
+ mastar_records[id] ||= find(id)
26
24
  mastar_records[id]
27
25
  end
28
26
 
@@ -88,13 +86,37 @@ module Mastar
88
86
  end
89
87
  end
90
88
 
91
- module InstanceMethods
92
- private
93
- def remove_record_cache
94
- if self.id
95
- records = self.class.__send__('mastar_records')
96
- records[self.id] = nil
89
+ def respond_to_missing?(symbol, include_private)
90
+ can_define_judge_method?(symbol)
91
+ end
92
+
93
+ def method_missing(symbol, *args)
94
+ if can_define_judge_method?(symbol)
95
+ self.class.class_eval do
96
+ define_method(symbol) do
97
+ mastar_config = self.class.__send__("mastar_config")
98
+ __send__(mastar_config.key) == symbol.to_s.chomp("?")
99
+ end
97
100
  end
101
+ __send__(symbol)
102
+ else
103
+ super
104
+ end
105
+ end
106
+
107
+ private
108
+ def can_define_judge_method?(symbol)
109
+ mastar_config = self.class.__send__("mastar_config")
110
+ return false unless mastar_config.key && symbol.to_s.end_with?("?")
111
+
112
+ name = symbol.to_s.chomp("?")
113
+ self.class.exists?(mastar_config.key => name)
114
+ end
115
+
116
+ def remove_record_cache
117
+ if self.id
118
+ records = self.class.__send__('mastar_records')
119
+ records[self.id] = nil
98
120
  end
99
121
  end
100
122
  end
@@ -1,4 +1,4 @@
1
1
  # coding: utf-8
2
2
  module Mastar
3
- VERSION = "0.9.0"
3
+ VERSION = "1.0.0"
4
4
  end
@@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
5
5
  gem.authors = ["pinzolo"]
6
6
  gem.email = ["pinzolo@gmail.com"]
7
7
  gem.description = %q{MASTer table on Active Record}
8
- gem.summary = %q{add some features to master table class on ActiveRecord}
8
+ gem.summary = %q{Add some features to master table class on ActiveRecord}
9
9
  gem.homepage = "https://github.com/pinzolo/mastar"
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
@@ -18,5 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.add_development_dependency 'rake'
19
19
  gem.add_development_dependency 'rspec'
20
20
  gem.add_development_dependency 'sqlite3'
21
- gem.add_development_dependency 'activerecord'
21
+ gem.add_development_dependency "coveralls"
22
+
23
+ gem.add_dependency 'activerecord', '>=3.2.0'
22
24
  end
@@ -8,7 +8,7 @@ def pair_map(pairs)
8
8
  end
9
9
 
10
10
  describe Mastar do
11
- describe Mastar::ClassMethods do
11
+ describe "class methods" do
12
12
  describe '.pairs' do
13
13
  context 'default' do
14
14
  context 'directly called' do
@@ -147,7 +147,7 @@ describe Mastar do
147
147
  end
148
148
  end
149
149
  end
150
- describe '.find' do
150
+ describe '.get' do
151
151
  context 'called by cached id' do
152
152
  it 'get cached record' do
153
153
  rec = Dow4.wednesday
@@ -157,31 +157,21 @@ describe Mastar do
157
157
  'iii'
158
158
  end
159
159
  end
160
- Dow4.find(4).respond_to?(:iii).should be_true
160
+ Dow4.get(4).respond_to?(:iii).should be_true
161
161
  end
162
162
  end
163
163
  context 'called by uncached id' do
164
164
  it 'get new record and cache' do
165
165
  recs = Dow4.__send__(:mastar_records)
166
166
  recs[5].should be_nil
167
- Dow4.find(5)
167
+ Dow4.get(5)
168
168
  recs = Dow4.__send__(:mastar_records)
169
169
  recs[5].should_not be_nil
170
170
  end
171
171
  end
172
172
  end
173
- describe '.find!' do
174
- it 'defined' do
175
- Dow2.respond_to?(:find!).should be_true
176
- end
177
- context 'called' do
178
- it 'get record' do
179
- Dow2.find(4).id.should eq 4
180
- end
181
- end
182
- end
183
173
  end
184
- describe Mastar::InstanceMethods do
174
+ describe "instance methods" do
185
175
  context 'record update' do
186
176
  it 'direct method record is updated automatically' do
187
177
  Dow1.friday.holiday?.should eq false
@@ -191,5 +181,30 @@ describe Mastar do
191
181
  Dow1.friday.holiday?.should eq true
192
182
  end
193
183
  end
184
+ context "judge method" do
185
+ context "method name is `code` + ?" do
186
+ it "respond to method name" do
187
+ Dow4.sunday.respond_to?(:monday?).should be_true
188
+ end
189
+ context "method name equals to code" do
190
+ it "returns true" do
191
+ Dow4.sunday.sunday?.should be_true
192
+ end
193
+ end
194
+ context "method name does not equal to code" do
195
+ it "returns false" do
196
+ Dow4.sunday.monday?.should be_false
197
+ end
198
+ end
199
+ end
200
+ context "method name is `not code` + ?" do
201
+ it "not respond to method name" do
202
+ Dow4.sundy.respond_to?(:foo?).should be_false
203
+ end
204
+ it "raise error" do
205
+ lambda { Dow4.sunday.birthday? }.should raise_error(NoMethodError)
206
+ end
207
+ end
208
+ end
194
209
  end
195
210
  end
@@ -1,4 +1,13 @@
1
1
  # coding: utf-8
2
+ require "coveralls"
3
+ require "simplecov"
4
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
5
+ SimpleCov.start do
6
+ add_filter '/active_record/'
7
+ add_filter '/spec/'
8
+ add_filter '/bundle/'
9
+ end
10
+
2
11
  base_dir = File.dirname(__FILE__)
3
12
  $:.unshift(base_dir, '/../lib')
4
13
  require 'mastar'
metadata CHANGED
@@ -1,80 +1,85 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mastar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
5
- prerelease:
4
+ version: 1.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - pinzolo
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-11-17 00:00:00.000000000 Z
11
+ date: 2014-02-06 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: sqlite3
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
- name: activerecord
56
+ name: coveralls
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - '>='
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - '>='
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 3.2.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 3.2.0
78
83
  description: MASTer table on Active Record
79
84
  email:
80
85
  - pinzolo@gmail.com
@@ -88,6 +93,9 @@ files:
88
93
  - LICENSE
89
94
  - README.md
90
95
  - Rakefile
96
+ - gemfiles/activerecord_3_2_x.gemfile
97
+ - gemfiles/activerecord_4_0_x.gemfile
98
+ - gemfiles/activerecord_4_1_x.gemfile
91
99
  - lib/mastar.rb
92
100
  - lib/mastar/configuration.rb
93
101
  - lib/mastar/name_value_pair.rb
@@ -102,28 +110,27 @@ files:
102
110
  - spec/spec_helper.rb
103
111
  homepage: https://github.com/pinzolo/mastar
104
112
  licenses: []
113
+ metadata: {}
105
114
  post_install_message:
106
115
  rdoc_options: []
107
116
  require_paths:
108
117
  - lib
109
118
  required_ruby_version: !ruby/object:Gem::Requirement
110
- none: false
111
119
  requirements:
112
- - - ! '>='
120
+ - - '>='
113
121
  - !ruby/object:Gem::Version
114
122
  version: '0'
115
123
  required_rubygems_version: !ruby/object:Gem::Requirement
116
- none: false
117
124
  requirements:
118
- - - ! '>='
125
+ - - '>='
119
126
  - !ruby/object:Gem::Version
120
127
  version: '0'
121
128
  requirements: []
122
129
  rubyforge_project:
123
- rubygems_version: 1.8.23
130
+ rubygems_version: 2.0.3
124
131
  signing_key:
125
- specification_version: 3
126
- summary: add some features to master table class on ActiveRecord
132
+ specification_version: 4
133
+ summary: Add some features to master table class on ActiveRecord
127
134
  test_files:
128
135
  - spec/fixtures/dows.yml
129
136
  - spec/mastar/configuration_spec.rb