masterman 0.0.6 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +32 -1
- data/lib/masterman/attributes.rb +6 -1
- data/lib/masterman/base.rb +1 -0
- data/lib/masterman/class_accessor.rb +51 -0
- data/lib/masterman/instance_accessor.rb +98 -0
- data/lib/masterman/loader/base.rb +10 -2
- data/lib/masterman/loader/csv.rb +3 -3
- data/lib/masterman/loader/direct.rb +3 -1
- data/lib/masterman/loader/json.rb +3 -1
- data/lib/masterman/loader/yaml.rb +3 -1
- data/lib/masterman/mountable.rb +5 -0
- data/lib/masterman/version.rb +1 -1
- data/lib/masterman.rb +8 -4
- metadata +5 -5
- data/lib/masterman/attribute_methods.rb +0 -30
- data/lib/masterman/core.rb +0 -56
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ef5b1a50e5754ea420dfb726e6873cf67aca152
|
4
|
+
data.tar.gz: 4b2ee777a42fa11f12d27a3c3245bea14c1cf480
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2263790ed1f1bbcc9baf76adb39cff1623e1921ba72aea7c6dc55173595dd785fd5b4df4a7c4958e2c8ff73a234ef7c0a0e0bbfc0ba7f1259ae60357a1729ad4
|
7
|
+
data.tar.gz: ebe03040fcb4facf43cf510ad3415158eb7e0126b5ea25cf9417c43b3919db7f0d85b6ad53cd9265b0bcba4000db503f36d0a9fe8927dadaed0e137260c851d6
|
data/README.md
CHANGED
@@ -48,7 +48,23 @@ ShippingCost.first.prefecture.is_a?(Prefecture)
|
|
48
48
|
ShippingCost.first.attributes # => { 'id' => ..., 'price' => ..., 'prefecture_id' => ... }
|
49
49
|
```
|
50
50
|
|
51
|
-
###
|
51
|
+
### Directly mount single data to class
|
52
|
+
|
53
|
+
```
|
54
|
+
class Administrator
|
55
|
+
include Masterman
|
56
|
+
|
57
|
+
masterman do
|
58
|
+
# Can not have association
|
59
|
+
cattr_reader :email, :name
|
60
|
+
class_mount path: '../prefecture.yml'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
Prefecture.email.present? # => true
|
65
|
+
```
|
66
|
+
|
67
|
+
### Select filetype of static data
|
52
68
|
|
53
69
|
```
|
54
70
|
class Item
|
@@ -83,6 +99,21 @@ class Item
|
|
83
99
|
end
|
84
100
|
```
|
85
101
|
|
102
|
+
### Enable cache
|
103
|
+
|
104
|
+
When masterman loader called `.find` or `.all`, loader is not cached.
|
105
|
+
If you want to cache static data, call `.mount` with `cache: true`
|
106
|
+
|
107
|
+
```
|
108
|
+
class Item
|
109
|
+
include Masterman
|
110
|
+
|
111
|
+
masterman do
|
112
|
+
mount path: 'item.yml', cache: true
|
113
|
+
end
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
86
117
|
## TODO
|
87
118
|
|
88
119
|
- Support `has_and_belongs_to` association
|
data/lib/masterman/attributes.rb
CHANGED
@@ -5,12 +5,17 @@ module Masterman
|
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
7
|
included do
|
8
|
-
class_attribute :attr_readers
|
8
|
+
class_attribute :attr_readers, :cattr_readers
|
9
9
|
self.attr_readers = []
|
10
|
+
self.cattr_readers = []
|
10
11
|
end
|
11
12
|
|
12
13
|
def attr_reader(*attrs)
|
13
14
|
self.attr_readers += attrs
|
14
15
|
end
|
16
|
+
|
17
|
+
def cattr_reader(*attrs)
|
18
|
+
self.cattr_readers += attrs
|
19
|
+
end
|
15
20
|
end
|
16
21
|
end
|
data/lib/masterman/base.rb
CHANGED
@@ -0,0 +1,51 @@
|
|
1
|
+
module Masterman
|
2
|
+
module ClassAccessor
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
class GeneratedAttributeReaders < Module; end
|
6
|
+
|
7
|
+
included do
|
8
|
+
class_attribute :generated_cattr_readers
|
9
|
+
end
|
10
|
+
|
11
|
+
def mount_class_attributes!
|
12
|
+
initialize_generated_cattr_readers unless mountable_class_attributes?
|
13
|
+
end
|
14
|
+
|
15
|
+
def attributes
|
16
|
+
masterman.attr_readers.each_with_object({}) do |attr, memo|
|
17
|
+
value = public_send(attr)
|
18
|
+
memo[attr.to_s] = value if value
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def [](key)
|
23
|
+
public_send(key)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def mountable_class_attributes?
|
29
|
+
self.class.generated_cattr_readers && self.class.cloader && self.class.cattr_readers.length > 0
|
30
|
+
end
|
31
|
+
|
32
|
+
def initialize_generated_cattr_readers
|
33
|
+
self.generated_cattr_readers = GeneratedAttributeReaders.new
|
34
|
+
self.model_class.extend(generated_cattr_readers)
|
35
|
+
define_cattributes
|
36
|
+
end
|
37
|
+
|
38
|
+
def define_cattributes
|
39
|
+
readers = cattr_readers
|
40
|
+
|
41
|
+
generated_cattr_readers.module_eval do
|
42
|
+
readers.each do |reader|
|
43
|
+
define_method reader do
|
44
|
+
mounted = masterman.class_loader.all
|
45
|
+
mounted[reader.to_s] || mounted[reader.to_sym]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'masterman/mountable'
|
2
|
+
require 'masterman/reflection'
|
3
|
+
require 'masterman/association'
|
4
|
+
require 'masterman/collection'
|
5
|
+
|
6
|
+
module Masterman
|
7
|
+
module InstanceAccessor
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
included do
|
11
|
+
class_attribute :generated_attr_readers, :generated_cattr_readers
|
12
|
+
end
|
13
|
+
|
14
|
+
class_methods do
|
15
|
+
def mount_class_attributes!
|
16
|
+
initialize_generated_cattr_readers unless mountable_class_attributes?
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def mountable_class_attributes?
|
22
|
+
self.class.generated_cattr_readers && self.class.cloader && self.class.cattr_readers.length > 0
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize_generated_cattr_readers
|
26
|
+
self.generated_cattr_readers = GeneratedAttributeReaders.new
|
27
|
+
self.extend(generated_cattr_readers)
|
28
|
+
define_cattributes
|
29
|
+
end
|
30
|
+
|
31
|
+
def define_cattributes
|
32
|
+
readers = masterman.cattr_readers
|
33
|
+
|
34
|
+
generated_cattr_readers.module_eval do
|
35
|
+
readers.each do |reader|
|
36
|
+
define_method reader do
|
37
|
+
masterman.class_loader.all[reader.to_s]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class GeneratedAttributeReaders < Module; end
|
45
|
+
|
46
|
+
def initialize(attributes = {})
|
47
|
+
initialize_generated_attr_readers unless self.class.generated_attr_readers
|
48
|
+
|
49
|
+
attributes.each do |key, value|
|
50
|
+
instance_variable_set(:"@#{key}", value)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def ==(other)
|
55
|
+
other.is_a?(self.class) &&
|
56
|
+
!other[self.class.masterman.primary_key].nil? &&
|
57
|
+
other.attributes == attributes
|
58
|
+
end
|
59
|
+
|
60
|
+
def attributes
|
61
|
+
masterman.attr_readers.each_with_object({}) do |attr, memo|
|
62
|
+
value = public_send(attr)
|
63
|
+
memo[attr.to_s] = value if value
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def [](key)
|
68
|
+
public_send(key)
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def initialize_generated_attr_readers
|
74
|
+
self.class.generated_attr_readers = GeneratedAttributeReaders.new
|
75
|
+
self.class.include(generated_attr_readers)
|
76
|
+
define_attributes
|
77
|
+
define_reflections
|
78
|
+
end
|
79
|
+
|
80
|
+
def define_reflections
|
81
|
+
self.class.masterman.reflections.each do |name, _|
|
82
|
+
generated_attr_readers.module_eval do
|
83
|
+
define_method(name) do
|
84
|
+
self.class.masterman.association(name, self).reader
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def define_attributes
|
91
|
+
readers = self.class.masterman.attr_readers
|
92
|
+
|
93
|
+
generated_attr_readers.module_eval do
|
94
|
+
attr_reader *readers
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -13,12 +13,20 @@ module Masterman
|
|
13
13
|
raise NotImplementedError, 'not implemented yet'
|
14
14
|
end
|
15
15
|
|
16
|
-
def all
|
17
|
-
|
16
|
+
def all
|
17
|
+
if options[:cache]
|
18
|
+
@all ||= find_all
|
19
|
+
else
|
20
|
+
find_all
|
21
|
+
end
|
18
22
|
end
|
19
23
|
|
20
24
|
private
|
21
25
|
|
26
|
+
def find_all
|
27
|
+
raise NotImplementedError, 'not implemented yet'
|
28
|
+
end
|
29
|
+
|
22
30
|
def loader_options
|
23
31
|
default_loader_options.merge(@options.fetch(:loader_options, {}))
|
24
32
|
end
|
data/lib/masterman/loader/csv.rb
CHANGED
data/lib/masterman/mountable.rb
CHANGED
@@ -1,11 +1,16 @@
|
|
1
1
|
module Masterman
|
2
2
|
module Mountable
|
3
3
|
attr_writer :primary_key
|
4
|
+
attr_reader :class_loader
|
4
5
|
|
5
6
|
def mount(options = {})
|
6
7
|
@loader = Loader.build(options)
|
7
8
|
end
|
8
9
|
|
10
|
+
def class_mount(options = {})
|
11
|
+
@class_loader = Loader.build(options)
|
12
|
+
end
|
13
|
+
|
9
14
|
def primary_key
|
10
15
|
@primary_key || :id
|
11
16
|
end
|
data/lib/masterman/version.rb
CHANGED
data/lib/masterman.rb
CHANGED
@@ -6,9 +6,10 @@ require 'active_support/core_ext/class/attribute'
|
|
6
6
|
require 'active_support/core_ext/array/wrap'
|
7
7
|
|
8
8
|
require 'masterman/version'
|
9
|
-
require 'masterman/
|
9
|
+
require 'masterman/instance_accessor'
|
10
|
+
require 'masterman/class_accessor'
|
10
11
|
require 'masterman/association'
|
11
|
-
require 'masterman/
|
12
|
+
require 'masterman/attributes'
|
12
13
|
require 'masterman/associations'
|
13
14
|
require 'masterman/collection'
|
14
15
|
require 'masterman/reflection'
|
@@ -23,9 +24,12 @@ module Masterman
|
|
23
24
|
extend ActiveSupport::Concern
|
24
25
|
|
25
26
|
included do
|
26
|
-
include
|
27
|
-
include AttributeMethods
|
27
|
+
include InstanceAccessor
|
28
28
|
extend Collection
|
29
|
+
|
30
|
+
def masterman
|
31
|
+
self.class.masterman
|
32
|
+
end
|
29
33
|
end
|
30
34
|
|
31
35
|
class_methods do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: masterman
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- alpaca-tc
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -138,11 +138,11 @@ files:
|
|
138
138
|
- lib/masterman.rb
|
139
139
|
- lib/masterman/association.rb
|
140
140
|
- lib/masterman/associations.rb
|
141
|
-
- lib/masterman/attribute_methods.rb
|
142
141
|
- lib/masterman/attributes.rb
|
143
142
|
- lib/masterman/base.rb
|
143
|
+
- lib/masterman/class_accessor.rb
|
144
144
|
- lib/masterman/collection.rb
|
145
|
-
- lib/masterman/
|
145
|
+
- lib/masterman/instance_accessor.rb
|
146
146
|
- lib/masterman/loader.rb
|
147
147
|
- lib/masterman/loader/base.rb
|
148
148
|
- lib/masterman/loader/csv.rb
|
@@ -175,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
175
175
|
version: '0'
|
176
176
|
requirements: []
|
177
177
|
rubyforge_project:
|
178
|
-
rubygems_version: 2.
|
178
|
+
rubygems_version: 2.5.1
|
179
179
|
signing_key:
|
180
180
|
specification_version: 4
|
181
181
|
summary: Masterman is static data loader for Ruby.
|
@@ -1,30 +0,0 @@
|
|
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
|
data/lib/masterman/core.rb
DELETED
@@ -1,56 +0,0 @@
|
|
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
|