mastar 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +12 -0
- data/lib/mastar.rb +100 -0
- data/lib/mastar/configuration.rb +27 -0
- data/lib/mastar/name_value_pair.rb +13 -0
- data/lib/mastar/version.rb +4 -0
- data/mastar.gemspec +22 -0
- data/spec/fixtures/dows.yml +35 -0
- data/spec/mastar/configuration_spec.rb +111 -0
- data/spec/mastar/name_value_pair_spec.rb +29 -0
- data/spec/mastar_spec.rb +195 -0
- data/spec/models.rb +24 -0
- data/spec/schema.rb +10 -0
- data/spec/spec_helper.rb +23 -0
- metadata +134 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 pinzolo
|
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,29 @@
|
|
1
|
+
# Mastar
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'mastar'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install mastar
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
|
4
|
+
task :default => [:spec]
|
5
|
+
begin
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
8
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
9
|
+
spec.rspec_opts = ['-cfs']
|
10
|
+
end
|
11
|
+
rescue LoadError => e
|
12
|
+
end
|
data/lib/mastar.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'mastar/version'
|
3
|
+
require 'mastar/configuration'
|
4
|
+
require 'mastar/name_value_pair'
|
5
|
+
|
6
|
+
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)
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def pairs(options = {})
|
18
|
+
opts = safe_options(options)
|
19
|
+
name = extract_option_value(opts, :name, mastar_config.name)
|
20
|
+
value = extract_option_value(opts, :value, mastar_config.value)
|
21
|
+
self.select([name, value]).map { |r| NameValuePair.new(r.__send__(name), r.__send__(value)) }
|
22
|
+
end
|
23
|
+
|
24
|
+
def find(id)
|
25
|
+
mastar_records[id] ||= find!(id)
|
26
|
+
mastar_records[id]
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def mastar(options = {})
|
31
|
+
opts = safe_options(options)
|
32
|
+
unless opts.empty?
|
33
|
+
mastar_config.name(extract_option_value(opts, :name))
|
34
|
+
mastar_config.value(extract_option_value(opts, :value))
|
35
|
+
mastar_config.key(extract_option_value(opts, :key))
|
36
|
+
end
|
37
|
+
mastar_config
|
38
|
+
end
|
39
|
+
|
40
|
+
def mastar_config
|
41
|
+
@mastar_config ||= Mastar::Configuration.new
|
42
|
+
end
|
43
|
+
|
44
|
+
def mastar_records
|
45
|
+
@mastar_records ||= {}
|
46
|
+
end
|
47
|
+
|
48
|
+
def safe_options(options)
|
49
|
+
options.is_a?(Hash) ? options : {}
|
50
|
+
end
|
51
|
+
|
52
|
+
def extract_option_value(options, key, default_value = nil)
|
53
|
+
val = options[key] || options[key.to_s]
|
54
|
+
val = val || default_value if default_value
|
55
|
+
val.to_sym
|
56
|
+
end
|
57
|
+
|
58
|
+
def method_missing(name, *args)
|
59
|
+
if mastar_config.key
|
60
|
+
define_direct_method(name)
|
61
|
+
respond_to?(name) ? __send__(name, *args) : nil
|
62
|
+
else
|
63
|
+
super
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def define_direct_method(name)
|
68
|
+
rec = where(mastar_config.key => name.to_s).first
|
69
|
+
return unless rec
|
70
|
+
|
71
|
+
mastar_records[rec.id] = rec
|
72
|
+
klass = class << self; self end
|
73
|
+
klass.class_eval do
|
74
|
+
eval("instance_variable_set('@#{name}_id', #{rec.id})")
|
75
|
+
define_method(name) do |*args|
|
76
|
+
record_id = klass.instance_variable_get("@#{name}_id")
|
77
|
+
mastar_records[record_id] ||= find(record_id)
|
78
|
+
record = mastar_records[record_id]
|
79
|
+
if args.nil? || args.empty? || record.nil?
|
80
|
+
record
|
81
|
+
elsif args.length == 1
|
82
|
+
record.__send__(args.first)
|
83
|
+
else
|
84
|
+
args.map { |arg| record.__send__(arg) }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
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
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
module Mastar
|
3
|
+
class Configuration
|
4
|
+
def initialize(options = {})
|
5
|
+
opts = options.is_a?(Hash) ? options : {}
|
6
|
+
[:name, :value, :key].each do |k|
|
7
|
+
v = opts[k] || opts[k.to_s]
|
8
|
+
__send__(k, v.to_sym) if v
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def name(name = nil)
|
13
|
+
@name = name if name
|
14
|
+
@name || :name
|
15
|
+
end
|
16
|
+
|
17
|
+
def value(value = nil)
|
18
|
+
@value = value if value
|
19
|
+
@value || :id
|
20
|
+
end
|
21
|
+
|
22
|
+
def key(key = nil)
|
23
|
+
@key = key if key
|
24
|
+
@key
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/mastar.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/mastar/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["pinzolo"]
|
6
|
+
gem.email = ["pinzolo@gmail.com"]
|
7
|
+
gem.description = %q{MASTer table on Active Record}
|
8
|
+
gem.summary = %q{add some features to master table class on ActiveRecord}
|
9
|
+
gem.homepage = "https://github.com/pinzolo/mastar"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "mastar"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Mastar::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rake'
|
19
|
+
gem.add_development_dependency 'rspec'
|
20
|
+
gem.add_development_dependency 'sqlite3'
|
21
|
+
gem.add_development_dependency 'activerecord'
|
22
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
sunday:
|
2
|
+
id: 1
|
3
|
+
name: sunday
|
4
|
+
short_name: Sun.
|
5
|
+
holiday: true
|
6
|
+
monday:
|
7
|
+
id: 2
|
8
|
+
name: monday
|
9
|
+
short_name: Mon.
|
10
|
+
holiday: false
|
11
|
+
tuesday:
|
12
|
+
id: 3
|
13
|
+
name: tuesday
|
14
|
+
short_name: Tue.
|
15
|
+
holiday: false
|
16
|
+
wednesday:
|
17
|
+
id: 4
|
18
|
+
name: wednesday
|
19
|
+
short_name: Wed.
|
20
|
+
holiday: false
|
21
|
+
thursday:
|
22
|
+
id: 5
|
23
|
+
name: thursday
|
24
|
+
short_name: Thu.
|
25
|
+
holiday: false
|
26
|
+
friday:
|
27
|
+
id: 6
|
28
|
+
name: friday
|
29
|
+
short_name: Fri.
|
30
|
+
holiday: false
|
31
|
+
saturday:
|
32
|
+
id: 7
|
33
|
+
name: saturday
|
34
|
+
short_name: Sat.
|
35
|
+
holiday: true
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
describe Mastar::Configuration do
|
4
|
+
context 'initialized without parameter' do# {{{
|
5
|
+
before do
|
6
|
+
@config = Mastar::Configuration.new
|
7
|
+
end
|
8
|
+
describe '#name' do
|
9
|
+
it 'is :name' do
|
10
|
+
@config.name.should be :name
|
11
|
+
end
|
12
|
+
end
|
13
|
+
describe '#value' do
|
14
|
+
it 'is :id' do
|
15
|
+
@config.value.should be :id
|
16
|
+
end
|
17
|
+
end
|
18
|
+
describe '#key' do
|
19
|
+
it 'is nil' do
|
20
|
+
@config.key.should be nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end# }}}
|
24
|
+
|
25
|
+
context 'initialized with Hash parameters (name: title, value: value, key: uid)' do
|
26
|
+
context 'key: symbol, value: symbol' do# {{{
|
27
|
+
before do
|
28
|
+
@config = Mastar::Configuration.new(:name => :title, :value => :value, :key => :uid)
|
29
|
+
end
|
30
|
+
describe '#name' do
|
31
|
+
it 'is :title' do
|
32
|
+
puts @config.name
|
33
|
+
@config.name.should be :title
|
34
|
+
end
|
35
|
+
end
|
36
|
+
describe '#value' do
|
37
|
+
it 'is :value' do
|
38
|
+
@config.value.should be :value
|
39
|
+
end
|
40
|
+
end
|
41
|
+
describe '#key' do
|
42
|
+
it 'is :uid' do
|
43
|
+
@config.key.should be :uid
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end# }}}
|
47
|
+
|
48
|
+
context 'key: symbol, value: string' do# {{{
|
49
|
+
before do
|
50
|
+
@config = Mastar::Configuration.new(:name => 'title', :value => 'value', :key => 'uid')
|
51
|
+
end
|
52
|
+
describe '#name' do
|
53
|
+
it 'is :title' do
|
54
|
+
@config.name.should be :title
|
55
|
+
end
|
56
|
+
end
|
57
|
+
describe '#value' do
|
58
|
+
it 'is :value' do
|
59
|
+
@config.value.should be :value
|
60
|
+
end
|
61
|
+
end
|
62
|
+
describe '#key' do
|
63
|
+
it 'is :uid' do
|
64
|
+
@config.key.should be :uid
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end# }}}
|
68
|
+
|
69
|
+
context 'key: string, value: string' do# {{{
|
70
|
+
before do
|
71
|
+
@config = Mastar::Configuration.new('name' => 'title', 'value' => 'value', 'key' => 'uid')
|
72
|
+
end
|
73
|
+
describe '#name' do
|
74
|
+
it 'is :title' do
|
75
|
+
@config.name.should be :title
|
76
|
+
end
|
77
|
+
end
|
78
|
+
describe '#value' do
|
79
|
+
it 'is :value' do
|
80
|
+
@config.value.should be :value
|
81
|
+
end
|
82
|
+
end
|
83
|
+
describe '#key' do
|
84
|
+
it 'is :uid' do
|
85
|
+
@config.key.should be :uid
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end# }}}
|
89
|
+
end
|
90
|
+
|
91
|
+
context 'initialized with parameters (not Hash)' do# {{{
|
92
|
+
before do
|
93
|
+
@config = Mastar::Configuration.new(['title', 'value', 'uid'])
|
94
|
+
end
|
95
|
+
describe '#name' do
|
96
|
+
it 'is :name' do
|
97
|
+
@config.name.should be :name
|
98
|
+
end
|
99
|
+
end
|
100
|
+
describe '#value' do
|
101
|
+
it 'is :id' do
|
102
|
+
@config.value.should be :id
|
103
|
+
end
|
104
|
+
end
|
105
|
+
describe '#key' do
|
106
|
+
it 'is nil' do
|
107
|
+
@config.key.should be nil
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end# }}}
|
111
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
describe Mastar::NameValuePair do
|
4
|
+
context 'initialized with name and value' do
|
5
|
+
before do
|
6
|
+
@pair = Mastar::NameValuePair.new('label', 1)
|
7
|
+
end
|
8
|
+
describe '#name' do
|
9
|
+
it 'is label' do
|
10
|
+
@pair.name.should eq 'label'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
describe '#value' do
|
14
|
+
it 'is 1' do
|
15
|
+
@pair.value.should eq 1
|
16
|
+
end
|
17
|
+
end
|
18
|
+
describe '#first' do
|
19
|
+
it 'is equal #name' do
|
20
|
+
@pair.first.should eq @pair.name
|
21
|
+
end
|
22
|
+
end
|
23
|
+
describe '#last' do
|
24
|
+
it 'is equal #value' do
|
25
|
+
@pair.last.should eq @pair.last
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/mastar_spec.rb
ADDED
@@ -0,0 +1,195 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
def pair_map(pairs)
|
4
|
+
pairs.inject({}) do |result, item|
|
5
|
+
result[item.value] = item.name
|
6
|
+
result
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe Mastar do
|
11
|
+
describe Mastar::ClassMethods do
|
12
|
+
describe '.pairs' do
|
13
|
+
context 'default' do
|
14
|
+
context 'directly called' do
|
15
|
+
before do
|
16
|
+
@all_dows = Dow1.all
|
17
|
+
end
|
18
|
+
it 'defined' do
|
19
|
+
defined?(Dow1.pairs).should be_true
|
20
|
+
end
|
21
|
+
context 'without option argument' do
|
22
|
+
it 'respond to :first' do
|
23
|
+
Dow1.pairs.all? { |pair| pair.respond_to?(:first) }.should be_true
|
24
|
+
end
|
25
|
+
it 'respond to :last' do
|
26
|
+
Dow1.pairs.all? { |pair| pair.respond_to?(:last) }.should be_true
|
27
|
+
end
|
28
|
+
it 'get all rows' do
|
29
|
+
pairs = pair_map(Dow1.pairs)
|
30
|
+
@all_dows.all? { |dow| pairs[dow.id] == dow.name }.should be_true
|
31
|
+
end
|
32
|
+
end
|
33
|
+
context 'with option argument(:name => :short_name, :value => :name)' do# {{{
|
34
|
+
context 'key: Symbol, value: Symbol' do
|
35
|
+
it 'get specified columns by option' do
|
36
|
+
pairs = pair_map(Dow1.pairs(:name => :short_name, :value => :name))
|
37
|
+
@all_dows.all? { |dow| pairs[dow.name] == dow.short_name }.should be_true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
context 'key: String, value: Symbol' do
|
41
|
+
it 'same as above' do
|
42
|
+
pairs = pair_map(Dow1.pairs('name' => :short_name, 'value' => :name))
|
43
|
+
@all_dows.all? { |dow| pairs[dow.name] == dow.short_name }.should be_true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
context 'key: Symbol, value: String' do
|
47
|
+
it 'same as above' do
|
48
|
+
pairs = pair_map(Dow1.pairs(:name => 'short_name', :value => 'name'))
|
49
|
+
@all_dows.all? { |dow| pairs[dow.name] == dow.short_name }.should be_true
|
50
|
+
end
|
51
|
+
end
|
52
|
+
context 'key: String, value: String' do
|
53
|
+
it 'same as above' do
|
54
|
+
pairs = pair_map(Dow1.pairs('name' => 'short_name', 'value' => 'name'))
|
55
|
+
@all_dows.all? { |dow| pairs[dow.name] == dow.short_name }.should be_true
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end# }}}
|
59
|
+
context 'with nil argument' do
|
60
|
+
it 'same as no option' do
|
61
|
+
pairs = pair_map(Dow1.pairs)
|
62
|
+
@all_dows.all? { |dow| pairs[dow.id] == dow.name }.should be_true
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
context 'called by way of ARel' do
|
67
|
+
it 'respond' do
|
68
|
+
Dow1.holiday.respond_to?(:pairs).should be_true
|
69
|
+
end
|
70
|
+
it 'get filtered rows' do
|
71
|
+
pairs = pair_map(Dow1.holiday.pairs)
|
72
|
+
Dow1.where(:holiday => true).all? { |dow| pairs[dow.id] == dow.name }.should be_true
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
context 'use mastar_name method' do
|
77
|
+
it 'use mastar_name attr as name' do
|
78
|
+
pairs = pair_map(Dow3.pairs)
|
79
|
+
Dow3.all.all? { |dow| pairs[dow.id] == dow.short_name }.should be_true
|
80
|
+
end
|
81
|
+
end
|
82
|
+
context 'use mastar_value method' do
|
83
|
+
it 'use mastar_value attr as value' do
|
84
|
+
pairs = pair_map(Dow2.pairs)
|
85
|
+
Dow2.all.all? { |dow| pairs[dow.short_name] == dow.name }.should be_true
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
describe 'define direct class method' do
|
90
|
+
context 'no argument' do
|
91
|
+
it 'get record object' do
|
92
|
+
Dow1.respond_to?(:sunday).should be_false
|
93
|
+
Dow1.sunday.is_a?(Dow1).should be_true
|
94
|
+
Dow1.respond_to?(:sunday).should be_true
|
95
|
+
Dow1.sunday.name.should eq 'sunday'
|
96
|
+
end
|
97
|
+
end
|
98
|
+
context 'with 1 argument' do
|
99
|
+
it 'get attribute value at direct' do
|
100
|
+
Dow1.monday(:short_name).should eq 'Mon.'
|
101
|
+
end
|
102
|
+
end
|
103
|
+
context 'with multiple arguments' do
|
104
|
+
it 'get Array of attribute values at a time' do
|
105
|
+
Dow1.tuesday(:short_name, :holiday).is_a?(Array).should be_true
|
106
|
+
short_name, name = Dow1.wednesday(:short_name, :name)
|
107
|
+
short_name.should eq 'Wed.'
|
108
|
+
name.should eq 'wednesday'
|
109
|
+
end
|
110
|
+
end
|
111
|
+
context 'mastar.key is not set' do
|
112
|
+
it 'raise NoMethodError' do
|
113
|
+
lambda { Dow2.monday }.should raise_error(NoMethodError)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
describe 'not exist in key column' do
|
118
|
+
context 'no argument' do
|
119
|
+
it 'get nil' do
|
120
|
+
Dow1.foo.should be_nil
|
121
|
+
end
|
122
|
+
end
|
123
|
+
context 'with 1 argument' do
|
124
|
+
it 'get nil' do
|
125
|
+
Dow1.bar(:name).should be_nil
|
126
|
+
end
|
127
|
+
end
|
128
|
+
context 'with multiple arguments' do
|
129
|
+
it 'get nil' do
|
130
|
+
Dow1.baz(:name, :value).should be_nil
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
describe '.mastar multiple setting' do
|
135
|
+
context 'mastar#name, mastar#value' do
|
136
|
+
it '.pairs method works' do
|
137
|
+
pairs = pair_map(Dow4.pairs)
|
138
|
+
Dow4.all.all? { |dow| pairs[dow.name] == dow.short_name }.should be_true
|
139
|
+
end
|
140
|
+
end
|
141
|
+
context 'mastar#key' do
|
142
|
+
it 'direct class method defining works' do
|
143
|
+
Dow4.respond_to?(:sunday).should be_false
|
144
|
+
Dow4.sunday.is_a?(Dow4).should be_true
|
145
|
+
Dow4.respond_to?(:sunday).should be_true
|
146
|
+
Dow4.sunday.name.should eq 'sunday'
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
describe '.find' do
|
151
|
+
context 'called by cached id' do
|
152
|
+
it 'get cached record' do
|
153
|
+
rec = Dow4.wednesday
|
154
|
+
rec.respond_to?(:iii).should be_false
|
155
|
+
class << rec
|
156
|
+
def iii
|
157
|
+
'iii'
|
158
|
+
end
|
159
|
+
end
|
160
|
+
Dow4.find(4).respond_to?(:iii).should be_true
|
161
|
+
end
|
162
|
+
end
|
163
|
+
context 'called by uncached id' do
|
164
|
+
it 'get new record and cache' do
|
165
|
+
recs = Dow4.__send__(:mastar_records)
|
166
|
+
recs[5].should be_nil
|
167
|
+
Dow4.find(5)
|
168
|
+
recs = Dow4.__send__(:mastar_records)
|
169
|
+
recs[5].should_not be_nil
|
170
|
+
end
|
171
|
+
end
|
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
|
+
end
|
184
|
+
describe Mastar::InstanceMethods do
|
185
|
+
context 'record update' do
|
186
|
+
it 'direct method record is updated automatically' do
|
187
|
+
Dow1.friday.holiday?.should eq false
|
188
|
+
fri = Dow1.find(Dow1.friday.id)
|
189
|
+
fri.holiday = true
|
190
|
+
fri.save
|
191
|
+
Dow1.friday.holiday?.should eq true
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
data/spec/models.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
class Dow1 < ActiveRecord::Base
|
3
|
+
include Mastar
|
4
|
+
scope :holiday, lambda { where(:holiday => true) }
|
5
|
+
mastar.key :name
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
class Dow2 < ActiveRecord::Base
|
10
|
+
include Mastar
|
11
|
+
mastar.value :short_name
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
class Dow3 < ActiveRecord::Base
|
16
|
+
include Mastar
|
17
|
+
mastar.name :short_name
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
class Dow4 < ActiveRecord::Base
|
22
|
+
include Mastar
|
23
|
+
mastar :name => :short_name, :value => :name, :key => :name
|
24
|
+
end
|
data/spec/schema.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
base_dir = File.dirname(__FILE__)
|
3
|
+
$:.unshift(base_dir, '/../lib')
|
4
|
+
require 'mastar'
|
5
|
+
require 'active_record'
|
6
|
+
|
7
|
+
ActiveRecord::Base.establish_connection(
|
8
|
+
:adapter => 'sqlite3',
|
9
|
+
:database => ':memory:'
|
10
|
+
)
|
11
|
+
|
12
|
+
ActiveRecord::Base.silence do
|
13
|
+
load(base_dir + '/schema.rb')
|
14
|
+
load(base_dir + '/models.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'yaml'
|
18
|
+
(1..4).to_a.each do |i|
|
19
|
+
yml = YAML.load_file("#{base_dir}/fixtures/dows.yml")
|
20
|
+
yml.keys.each do |k|
|
21
|
+
eval("Dow#{i}.create(yml[k])")
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mastar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- pinzolo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
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: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: sqlite3
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: activerecord
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: MASTer table on Active Record
|
79
|
+
email:
|
80
|
+
- pinzolo@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .travis.yml
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- lib/mastar.rb
|
92
|
+
- lib/mastar/configuration.rb
|
93
|
+
- lib/mastar/name_value_pair.rb
|
94
|
+
- lib/mastar/version.rb
|
95
|
+
- mastar.gemspec
|
96
|
+
- spec/fixtures/dows.yml
|
97
|
+
- spec/mastar/configuration_spec.rb
|
98
|
+
- spec/mastar/name_value_pair_spec.rb
|
99
|
+
- spec/mastar_spec.rb
|
100
|
+
- spec/models.rb
|
101
|
+
- spec/schema.rb
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
homepage: https://github.com/pinzolo/mastar
|
104
|
+
licenses: []
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 1.8.23
|
124
|
+
signing_key:
|
125
|
+
specification_version: 3
|
126
|
+
summary: add some features to master table class on ActiveRecord
|
127
|
+
test_files:
|
128
|
+
- spec/fixtures/dows.yml
|
129
|
+
- spec/mastar/configuration_spec.rb
|
130
|
+
- spec/mastar/name_value_pair_spec.rb
|
131
|
+
- spec/mastar_spec.rb
|
132
|
+
- spec/models.rb
|
133
|
+
- spec/schema.rb
|
134
|
+
- spec/spec_helper.rb
|