masterman 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +3 -0
- data/Guardfile +17 -0
- data/LICENSE.txt +21 -0
- data/README.md +96 -0
- data/Rakefile +6 -0
- data/lib/masterman/association.rb +51 -0
- data/lib/masterman/associations.rb +51 -0
- data/lib/masterman/attribute_methods.rb +30 -0
- data/lib/masterman/attributes.rb +16 -0
- data/lib/masterman/base.rb +14 -0
- data/lib/masterman/collection.rb +16 -0
- data/lib/masterman/core.rb +56 -0
- data/lib/masterman/loader/base.rb +31 -0
- data/lib/masterman/loader/csv.rb +21 -0
- data/lib/masterman/loader/direct.rb +15 -0
- data/lib/masterman/loader/json.rb +15 -0
- data/lib/masterman/loader/yaml.rb +15 -0
- data/lib/masterman/loader.rb +44 -0
- data/lib/masterman/mountable.rb +17 -0
- data/lib/masterman/reflection/macro.rb +96 -0
- data/lib/masterman/reflection.rb +41 -0
- data/lib/masterman/relation.rb +70 -0
- data/lib/masterman/version.rb +3 -0
- data/lib/masterman.rb +42 -0
- data/masterman.gemspec +27 -0
- metadata +182 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0e421369239e121dad3d994edf47d16263dd1387
|
4
|
+
data.tar.gz: 1c0f3fdd66e98e6fac289ddbdfbdac8047288549
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9548a711c3e89e04733b776b809539ce9d67d4c18fcaa5ada22c474bafd394091e2be90aaa63ba8cbc99e3e7a94c30ad06f6fd9fc9e4684f7c72ef2e87e6a6f9
|
7
|
+
data.tar.gz: 057910c7420323a2ac2c28fdc7b8fe576ddb4d77a2e176f4bf56b9421affcbeafd8b61c72f2bc20f3a16541030ba04889f98732e0b63e8b762a7594aee81bb51
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
ignore [
|
2
|
+
%r{^(?:\.bundle)},
|
3
|
+
%r{^(?:Gemfile|Gemfile\.lock|Guardfile)}
|
4
|
+
]
|
5
|
+
|
6
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
7
|
+
require 'guard/rspec/dsl'
|
8
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
9
|
+
|
10
|
+
# RSpec files
|
11
|
+
rspec = dsl.rspec
|
12
|
+
watch(rspec.spec_files)
|
13
|
+
|
14
|
+
# Ruby files
|
15
|
+
ruby = dsl.ruby
|
16
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
17
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 alpaca-tc
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
# Masterman
|
2
|
+
|
3
|
+
**Unstable version**
|
4
|
+
|
5
|
+
Masterman is static data loader for Ruby.
|
6
|
+
It loads data from direct or file, and defines accessor to read attributes.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'masterman'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install masterman
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
```
|
27
|
+
class Prefecture
|
28
|
+
include Masterman
|
29
|
+
|
30
|
+
masterman do
|
31
|
+
attr_reader :id, :name
|
32
|
+
mount path: '../prefecture.yml'
|
33
|
+
has_many :shipping_costs
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class ShippingCost
|
38
|
+
include Masterman
|
39
|
+
|
40
|
+
masterman do
|
41
|
+
attr_reader :id, :price, prefecture_id
|
42
|
+
mount path: '../shipping_cost.yml'
|
43
|
+
belongs_to :prefecture
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
ShippingCost.first.prefecture.is_a?(Prefecture)
|
48
|
+
ShippingCost.first.attributes # => { 'id' => ..., 'price' => ..., 'prefecture_id' => ... }
|
49
|
+
```
|
50
|
+
|
51
|
+
### A few of loader
|
52
|
+
|
53
|
+
```
|
54
|
+
class Item
|
55
|
+
include Masterman
|
56
|
+
|
57
|
+
masterman do
|
58
|
+
# You can use either loader
|
59
|
+
# mount direct: [{ id: 1 }]
|
60
|
+
# mount path: 'item.yml', loader: :yml
|
61
|
+
# mount path: 'item.csv', loader: :csv
|
62
|
+
# mount path: 'item.json', loader: :json
|
63
|
+
end
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
### ActiveRecord-like associations
|
68
|
+
|
69
|
+
```
|
70
|
+
class Item
|
71
|
+
include Masterman
|
72
|
+
|
73
|
+
masterman do
|
74
|
+
mount direct: [{ id: 1 }]
|
75
|
+
belongs_to :user
|
76
|
+
has_one :main_attachment
|
77
|
+
has_many :variations
|
78
|
+
has_many :attachments, through: :variations
|
79
|
+
|
80
|
+
# Filter records by scope which is evaluated by using `#instance_exec`.
|
81
|
+
has_many :odd_variations, -> { id % 2 == 1 }, source: :variations
|
82
|
+
end
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
## TODO
|
87
|
+
|
88
|
+
- Support `has_and_belongs_to` association
|
89
|
+
- Should I support lazy loading?
|
90
|
+
- Support cache records.
|
91
|
+
- Validate options for association
|
92
|
+
- Try to install masterman to [pixivFACTORY](factory.pixiv.net)
|
93
|
+
|
94
|
+
## License
|
95
|
+
|
96
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
module Masterman
|
2
|
+
class Association
|
3
|
+
attr_reader :model, :reflection
|
4
|
+
|
5
|
+
def initialize(model, reflection)
|
6
|
+
@model = model
|
7
|
+
@reflection = reflection
|
8
|
+
end
|
9
|
+
|
10
|
+
def reader
|
11
|
+
result = read_reflection
|
12
|
+
|
13
|
+
if result && reflection.scope
|
14
|
+
Array.wrap(result).select do |record|
|
15
|
+
record.instance_exec(&reflection.scope)
|
16
|
+
end
|
17
|
+
else
|
18
|
+
result
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def read_reflection
|
25
|
+
case reflection
|
26
|
+
when Reflection::BelongsToReflection
|
27
|
+
reflection.klass.find(model[reflection.foreign_key])
|
28
|
+
when Reflection::HasManyReflection
|
29
|
+
model_class = collection_target.model_class.model_class
|
30
|
+
model_class.select { |record| record[collection_target.foreign_key] == model[model.class.masterman.primary_key] }
|
31
|
+
when Reflection::HasOneReflection
|
32
|
+
model_class = collection_target.model_class.model_class
|
33
|
+
model_class.to_a.find { |record| record[collection_target.foreign_key] == model[model.class.masterman.primary_key] }
|
34
|
+
when Reflection::ThroughReflection
|
35
|
+
through_reflection = self.model.class.masterman.reflections[reflection.options[:through].to_s]
|
36
|
+
through_association = self.class.new(model, through_reflection).reader
|
37
|
+
if reflection.collection?
|
38
|
+
through_association.map { |association|
|
39
|
+
self.class.new(association, reflection.delegate_reflection).reader
|
40
|
+
}.flatten.compact
|
41
|
+
else
|
42
|
+
through_association[reflection.delegate_reflection.name]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def collection_target
|
48
|
+
reflection.klass.masterman.reflections[model.class.name.underscore]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Masterman
|
2
|
+
module Associations
|
3
|
+
def belongs_to(name, scope = nil, options = {})
|
4
|
+
build_reflection_and_add(:belongs_to, name, scope, options)
|
5
|
+
end
|
6
|
+
|
7
|
+
def has_many(name, scope = nil, options = {})
|
8
|
+
build_reflection_and_add(:has_many, name, scope, options)
|
9
|
+
end
|
10
|
+
|
11
|
+
def has_one(name, scope = nil, options = {})
|
12
|
+
build_reflection_and_add(:has_one, name, scope, options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def association(name, instance)
|
16
|
+
association = association_instance_get(name)
|
17
|
+
|
18
|
+
if association.nil?
|
19
|
+
reflection = self._reflections[name]
|
20
|
+
association = reflection.build_association(instance, reflection)
|
21
|
+
association_instance_set(name, association)
|
22
|
+
end
|
23
|
+
|
24
|
+
association
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def build_reflection_and_add(macro, name, scope, options)
|
30
|
+
if scope.is_a?(Hash)
|
31
|
+
options = scope
|
32
|
+
scope = nil
|
33
|
+
end
|
34
|
+
|
35
|
+
reflection = Reflection.build(macro, name, scope, options, self)
|
36
|
+
Reflection.add_reflection(self, name, reflection)
|
37
|
+
end
|
38
|
+
|
39
|
+
def association_instance_get(name)
|
40
|
+
association_instance_cache[name]
|
41
|
+
end
|
42
|
+
|
43
|
+
def association_instance_set(name, value)
|
44
|
+
association_instance_cache[name] = value
|
45
|
+
end
|
46
|
+
|
47
|
+
def association_instance_cache
|
48
|
+
@association_instance_cache ||= {}
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Masterman
|
2
|
+
module AttributeMethods
|
3
|
+
def attributes
|
4
|
+
masterman.attr_readers.each_with_object({}) do |attr, memo|
|
5
|
+
value = public_send(attr)
|
6
|
+
memo[attr.to_s] = value if value
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def assign_attributes(attributes)
|
11
|
+
attributes.each do |key, value|
|
12
|
+
public_send("#{key}=", value)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def [](key)
|
17
|
+
public_send(key)
|
18
|
+
end
|
19
|
+
|
20
|
+
def []=(key, value)
|
21
|
+
public_send("#{key}=", value)
|
22
|
+
end
|
23
|
+
|
24
|
+
def ==(other)
|
25
|
+
other.is_a?(self.class) &&
|
26
|
+
!other[self.class.masterman.primary_key].nil? &&
|
27
|
+
other.attributes == attributes
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
2
|
+
|
3
|
+
module Masterman
|
4
|
+
module Attributes
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
class_attribute :attr_readers
|
9
|
+
self.attr_readers = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def attr_reader(*attrs)
|
13
|
+
self.attr_readers += attrs
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Masterman
|
2
|
+
module Collection
|
3
|
+
include Enumerable
|
4
|
+
extend Forwardable
|
5
|
+
|
6
|
+
delegate %i(all find take take! first first! last last! exists? any? many?) => :relation
|
7
|
+
delegate %i(find_by find_by!) => :relation
|
8
|
+
delegate %i(pluck ids) => :relation
|
9
|
+
delegate %i(each) => :relation
|
10
|
+
|
11
|
+
def relation
|
12
|
+
records = masterman.loader.all
|
13
|
+
Masterman::Relation.new(masterman.model_class, records)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'masterman/mountable'
|
2
|
+
require 'masterman/reflection'
|
3
|
+
require 'masterman/association'
|
4
|
+
require 'masterman/collection'
|
5
|
+
require 'masterman/attributes'
|
6
|
+
|
7
|
+
module Masterman
|
8
|
+
module Core
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
included do
|
12
|
+
class_attribute :generated_attr_readers
|
13
|
+
end
|
14
|
+
|
15
|
+
class GeneratedAttributeReaders < Module; end
|
16
|
+
|
17
|
+
def initialize(attributes = {})
|
18
|
+
initialize_generated_modules unless self.class.generated_attr_readers
|
19
|
+
|
20
|
+
attributes.each do |key, value|
|
21
|
+
instance_variable_set(:"@#{key}", value)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def initialize_generated_modules
|
28
|
+
self.class.generated_attr_readers = GeneratedAttributeReaders.new
|
29
|
+
self.class.include(generated_attr_readers)
|
30
|
+
define_attributes
|
31
|
+
define_reflections
|
32
|
+
end
|
33
|
+
|
34
|
+
def define_reflections
|
35
|
+
masterman.reflections.each do |name, _|
|
36
|
+
generated_attr_readers.module_eval do
|
37
|
+
define_method(name) do
|
38
|
+
self.class.masterman.association(name, self).reader
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def define_attributes
|
45
|
+
readers = masterman.attr_readers
|
46
|
+
|
47
|
+
generated_attr_readers.module_eval do
|
48
|
+
attr_reader *readers
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def masterman
|
53
|
+
self.class.masterman
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Masterman
|
2
|
+
module Loader
|
3
|
+
class Base
|
4
|
+
attr_reader :options
|
5
|
+
class_attribute :extensions
|
6
|
+
self.extensions = []
|
7
|
+
|
8
|
+
def initialize(options = {})
|
9
|
+
@options = options
|
10
|
+
end
|
11
|
+
|
12
|
+
def find(*)
|
13
|
+
raise NotImplementedError, 'not implemented yet'
|
14
|
+
end
|
15
|
+
|
16
|
+
def all(*)
|
17
|
+
raise NotImplementedError, 'not implemented yet'
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def loader_options
|
23
|
+
default_loader_options.merge(@options.fetch(:loader_options, {}))
|
24
|
+
end
|
25
|
+
|
26
|
+
def default_loader_options
|
27
|
+
{}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
module Masterman
|
4
|
+
module Loader
|
5
|
+
class Csv < Base
|
6
|
+
self.extensions = [:csv]
|
7
|
+
|
8
|
+
def all
|
9
|
+
CSV.read(options[:path], loader_options)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def default_loader_options
|
15
|
+
{ headers: true }
|
16
|
+
end
|
17
|
+
|
18
|
+
Loader.register_loader(self)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'active_support/core_ext/string/inflections'
|
2
|
+
|
3
|
+
module Masterman
|
4
|
+
module Loader
|
5
|
+
class << self
|
6
|
+
def register_loader(loader)
|
7
|
+
loaders.push(loader)
|
8
|
+
end
|
9
|
+
|
10
|
+
def build(options = {})
|
11
|
+
loader = detect_loader(options)
|
12
|
+
loader.new(options)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def loaders
|
18
|
+
@loaders ||= []
|
19
|
+
end
|
20
|
+
|
21
|
+
def detect_loader(options)
|
22
|
+
if options[:loader].is_a?(Class)
|
23
|
+
# Given loader class as option
|
24
|
+
options[:loader]
|
25
|
+
elsif options.key?(:direct)
|
26
|
+
Loader::Direct
|
27
|
+
elsif !options.key?(:loader) && options.key?(:path)
|
28
|
+
# Detect loader from path path if Missing loader options
|
29
|
+
extname = File.extname(options[:path]).sub(/^\./, '').to_sym
|
30
|
+
loaders.find { |loader| loader.extensions.include?(extname) }
|
31
|
+
else
|
32
|
+
# From string or symbol
|
33
|
+
const_get(options[:loader].to_s.classify)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
require 'masterman/loader/base'
|
39
|
+
require 'masterman/loader/yaml'
|
40
|
+
require 'masterman/loader/json'
|
41
|
+
require 'masterman/loader/csv'
|
42
|
+
require 'masterman/loader/direct'
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Masterman
|
2
|
+
module Mountable
|
3
|
+
attr_writer :primary_key
|
4
|
+
|
5
|
+
def mount(options = {})
|
6
|
+
@loader = Loader.build(options)
|
7
|
+
end
|
8
|
+
|
9
|
+
def primary_key
|
10
|
+
@primary_key || :id
|
11
|
+
end
|
12
|
+
|
13
|
+
def loader
|
14
|
+
@loader || MastermanError.new('not mounted or bad option given')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module Masterman
|
2
|
+
module Reflection
|
3
|
+
class Macro
|
4
|
+
attr_reader :name, :scope, :options, :model_class
|
5
|
+
|
6
|
+
def initialize(name, scope, options, model_class)
|
7
|
+
@name = name
|
8
|
+
@scope = scope
|
9
|
+
@options = options
|
10
|
+
@model_class = model_class
|
11
|
+
end
|
12
|
+
|
13
|
+
def foreign_key
|
14
|
+
(options[:foreign_key] || "#{name.to_s}_id").to_s
|
15
|
+
end
|
16
|
+
|
17
|
+
def macro
|
18
|
+
raise NotImplementedError, 'macro is not defined'
|
19
|
+
end
|
20
|
+
|
21
|
+
def build_association(instance, reflection)
|
22
|
+
Masterman::Association.new(instance, reflection)
|
23
|
+
end
|
24
|
+
|
25
|
+
def klass
|
26
|
+
@klass ||= compute_class(class_name)
|
27
|
+
end
|
28
|
+
|
29
|
+
def through_reflection
|
30
|
+
if options[:through]
|
31
|
+
model_class._reflections[options[:through].to_s]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def collection?
|
36
|
+
raise NotImplementedError, 'collection? is not defined'
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def class_name
|
42
|
+
name.to_s.camelize
|
43
|
+
end
|
44
|
+
|
45
|
+
def compute_class(name)
|
46
|
+
class_name = options.fetch(:source, name).to_s
|
47
|
+
class_name = class_name.singularize if collection?
|
48
|
+
class_name.camelize.constantize
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class SinglurReflection < Macro
|
53
|
+
def collection?
|
54
|
+
false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class BelongsToReflection < SinglurReflection
|
59
|
+
def macro
|
60
|
+
:belongs_to
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
class HasOneReflection < SinglurReflection
|
65
|
+
def macro
|
66
|
+
:has_one
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
class HasManyReflection < Macro
|
71
|
+
def macro
|
72
|
+
:has_many
|
73
|
+
end
|
74
|
+
|
75
|
+
def collection?
|
76
|
+
true
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
class ThroughReflection
|
81
|
+
extend Forwardable
|
82
|
+
|
83
|
+
delegate %i(
|
84
|
+
model_class collection?
|
85
|
+
collection macro options scope klass foreign_key
|
86
|
+
through_reflection build_association
|
87
|
+
) => :@delegate_reflection
|
88
|
+
|
89
|
+
attr_reader :delegate_reflection
|
90
|
+
|
91
|
+
def initialize(delegate_reflection)
|
92
|
+
@delegate_reflection = delegate_reflection
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'masterman/reflection/macro'
|
2
|
+
|
3
|
+
module Masterman
|
4
|
+
module Reflection
|
5
|
+
def self.build(macro, name, scope, options, model_class)
|
6
|
+
case macro
|
7
|
+
when :belongs_to
|
8
|
+
return BelongsToReflection.new(name, scope, options, model_class)
|
9
|
+
when :has_one
|
10
|
+
reflection = HasOneReflection.new(name, scope, options, model_class)
|
11
|
+
when :has_many
|
12
|
+
reflection = HasManyReflection.new(name, scope, options, model_class)
|
13
|
+
else
|
14
|
+
raise NotImplementedError, "#{macro} is not supported"
|
15
|
+
end
|
16
|
+
|
17
|
+
if options[:through]
|
18
|
+
ThroughReflection.new(reflection)
|
19
|
+
else
|
20
|
+
reflection
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.add_reflection(masterman_base, name, reflection)
|
25
|
+
masterman_base._reflections.merge!(name.to_s => reflection)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns cloned reflections
|
29
|
+
def reflections
|
30
|
+
_reflections.clone
|
31
|
+
end
|
32
|
+
|
33
|
+
def _reflections
|
34
|
+
@_reflections ||= {}
|
35
|
+
end
|
36
|
+
|
37
|
+
def _reflections=(val)
|
38
|
+
@_reflections = val
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Masterman
|
2
|
+
class Relation
|
3
|
+
include Enumerable
|
4
|
+
extend Forwardable
|
5
|
+
|
6
|
+
delegate %i(each last) => :to_a
|
7
|
+
alias_method :exists?, :any?
|
8
|
+
|
9
|
+
%i(first last take find_by).each do |method_name|
|
10
|
+
define_method("#{method_name}!") { |*args| find_or_error!(method_name, *args) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(klass, records = [])
|
14
|
+
@klass = klass
|
15
|
+
@instances = to_instances(records)
|
16
|
+
end
|
17
|
+
|
18
|
+
def find(id)
|
19
|
+
@instances[id] || raise(RecordNotFound.new('missing record'))
|
20
|
+
end
|
21
|
+
|
22
|
+
def find_by(attributes)
|
23
|
+
@instances.each do |_, record|
|
24
|
+
return record if attributes.all? { |key, value| record[key] == value }
|
25
|
+
end
|
26
|
+
|
27
|
+
nil
|
28
|
+
end
|
29
|
+
|
30
|
+
def pluck(*column_names)
|
31
|
+
if column_names.one?
|
32
|
+
all.map { |record| record[column_names.first] }
|
33
|
+
else
|
34
|
+
all.map do |record|
|
35
|
+
column_names.map { |name| record[name] }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def ids
|
41
|
+
pluck(@klass.masterman.primary_key)
|
42
|
+
end
|
43
|
+
|
44
|
+
def many?
|
45
|
+
count > 1
|
46
|
+
end
|
47
|
+
|
48
|
+
def all
|
49
|
+
to_a
|
50
|
+
end
|
51
|
+
|
52
|
+
def to_a
|
53
|
+
@instances.values
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def find_or_error!(method_name, *args)
|
59
|
+
public_send(method_name, *args) || raise(RecordNotFound.new("Couldn't find by #{method_name}"))
|
60
|
+
end
|
61
|
+
|
62
|
+
# [{ primary_key => instance, primary_key => instance, ... }]
|
63
|
+
def to_instances(records)
|
64
|
+
records.each_with_object({}) do |attributes, memo|
|
65
|
+
record = @klass.new(attributes)
|
66
|
+
memo[record[@klass.masterman.primary_key]] = record
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/lib/masterman.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
require 'active_support/core_ext/module/concerning'
|
4
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
5
|
+
require 'active_support/core_ext/class/attribute'
|
6
|
+
require 'active_support/core_ext/array/wrap'
|
7
|
+
|
8
|
+
require 'masterman/version'
|
9
|
+
require 'masterman/core'
|
10
|
+
require 'masterman/association'
|
11
|
+
require 'masterman/attribute_methods'
|
12
|
+
require 'masterman/associations'
|
13
|
+
require 'masterman/collection'
|
14
|
+
require 'masterman/reflection'
|
15
|
+
require 'masterman/relation'
|
16
|
+
require 'masterman/loader'
|
17
|
+
require 'masterman/base'
|
18
|
+
|
19
|
+
module Masterman
|
20
|
+
class MastermanError < StandardError; end
|
21
|
+
class RecordNotFound < MastermanError; end
|
22
|
+
|
23
|
+
extend ActiveSupport::Concern
|
24
|
+
|
25
|
+
included do
|
26
|
+
include Core
|
27
|
+
include AttributeMethods
|
28
|
+
extend Collection
|
29
|
+
end
|
30
|
+
|
31
|
+
class_methods do
|
32
|
+
def masterman(&block)
|
33
|
+
@masterman ||= Masterman::Base.new(self)
|
34
|
+
|
35
|
+
if block_given?
|
36
|
+
@masterman.instance_exec(&block)
|
37
|
+
else
|
38
|
+
@masterman
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/masterman.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'masterman/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'masterman'
|
7
|
+
spec.version = Masterman::VERSION
|
8
|
+
spec.authors = ['alpaca-tc']
|
9
|
+
spec.email = ['alpaca-tc@alpaca.tc']
|
10
|
+
|
11
|
+
spec.summary = %q{Masterman is static data loader for Ruby.}
|
12
|
+
spec.description = %q{Masterman is static data loader for Ruby. It loads data from direct or file, and defines accessor to read attributes.}
|
13
|
+
spec.homepage = 'https://github.com/alpaca-tc/masterman'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.require_paths = ['lib']
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
|
21
|
+
spec.add_runtime_dependency 'activesupport', '~> 4.2'
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
23
|
+
spec.add_development_dependency 'rspec', '~> 3.4', '>= 3.4.0'
|
24
|
+
spec.add_development_dependency 'pry', '~> 0.9', '>= 0.9.12'
|
25
|
+
spec.add_development_dependency 'guard', '~> 2.13', '>= 2.13.0'
|
26
|
+
spec.add_development_dependency 'guard-rspec', '~> 4.6', '>= 4.6.4'
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: masterman
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- alpaca-tc
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-01 00:00:00.000000000 Z
|
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: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.4'
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 3.4.0
|
51
|
+
type: :development
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - "~>"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '3.4'
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 3.4.0
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: pry
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0.9'
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 0.9.12
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - "~>"
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0.9'
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 0.9.12
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: guard
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - "~>"
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '2.13'
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: 2.13.0
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - "~>"
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '2.13'
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: 2.13.0
|
101
|
+
- !ruby/object:Gem::Dependency
|
102
|
+
name: guard-rspec
|
103
|
+
requirement: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - "~>"
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '4.6'
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 4.6.4
|
111
|
+
type: :development
|
112
|
+
prerelease: false
|
113
|
+
version_requirements: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '4.6'
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: 4.6.4
|
121
|
+
description: Masterman is static data loader for Ruby. It loads data from direct or
|
122
|
+
file, and defines accessor to read attributes.
|
123
|
+
email:
|
124
|
+
- alpaca-tc@alpaca.tc
|
125
|
+
executables: []
|
126
|
+
extensions: []
|
127
|
+
extra_rdoc_files: []
|
128
|
+
files:
|
129
|
+
- ".gitignore"
|
130
|
+
- ".rspec"
|
131
|
+
- ".travis.yml"
|
132
|
+
- CODE_OF_CONDUCT.md
|
133
|
+
- Gemfile
|
134
|
+
- Guardfile
|
135
|
+
- LICENSE.txt
|
136
|
+
- README.md
|
137
|
+
- Rakefile
|
138
|
+
- lib/masterman.rb
|
139
|
+
- lib/masterman/association.rb
|
140
|
+
- lib/masterman/associations.rb
|
141
|
+
- lib/masterman/attribute_methods.rb
|
142
|
+
- lib/masterman/attributes.rb
|
143
|
+
- lib/masterman/base.rb
|
144
|
+
- lib/masterman/collection.rb
|
145
|
+
- lib/masterman/core.rb
|
146
|
+
- lib/masterman/loader.rb
|
147
|
+
- lib/masterman/loader/base.rb
|
148
|
+
- lib/masterman/loader/csv.rb
|
149
|
+
- lib/masterman/loader/direct.rb
|
150
|
+
- lib/masterman/loader/json.rb
|
151
|
+
- lib/masterman/loader/yaml.rb
|
152
|
+
- lib/masterman/mountable.rb
|
153
|
+
- lib/masterman/reflection.rb
|
154
|
+
- lib/masterman/reflection/macro.rb
|
155
|
+
- lib/masterman/relation.rb
|
156
|
+
- lib/masterman/version.rb
|
157
|
+
- masterman.gemspec
|
158
|
+
homepage: https://github.com/alpaca-tc/masterman
|
159
|
+
licenses:
|
160
|
+
- MIT
|
161
|
+
metadata: {}
|
162
|
+
post_install_message:
|
163
|
+
rdoc_options: []
|
164
|
+
require_paths:
|
165
|
+
- lib
|
166
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: '0'
|
171
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
172
|
+
requirements:
|
173
|
+
- - ">="
|
174
|
+
- !ruby/object:Gem::Version
|
175
|
+
version: '0'
|
176
|
+
requirements: []
|
177
|
+
rubyforge_project:
|
178
|
+
rubygems_version: 2.4.5.1
|
179
|
+
signing_key:
|
180
|
+
specification_version: 4
|
181
|
+
summary: Masterman is static data loader for Ruby.
|
182
|
+
test_files: []
|