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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e421369239e121dad3d994edf47d16263dd1387
4
- data.tar.gz: 1c0f3fdd66e98e6fac289ddbdfbdac8047288549
3
+ metadata.gz: 7ef5b1a50e5754ea420dfb726e6873cf67aca152
4
+ data.tar.gz: 4b2ee777a42fa11f12d27a3c3245bea14c1cf480
5
5
  SHA512:
6
- metadata.gz: 9548a711c3e89e04733b776b809539ce9d67d4c18fcaa5ada22c474bafd394091e2be90aaa63ba8cbc99e3e7a94c30ad06f6fd9fc9e4684f7c72ef2e87e6a6f9
7
- data.tar.gz: 057910c7420323a2ac2c28fdc7b8fe576ddb4d77a2e176f4bf56b9421affcbeafd8b61c72f2bc20f3a16541030ba04889f98732e0b63e8b762a7594aee81bb51
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
- ### A few of loader
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
@@ -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
@@ -4,6 +4,7 @@ module Masterman
4
4
  include Associations
5
5
  include Reflection
6
6
  include Mountable
7
+ include ClassAccessor
7
8
 
8
9
  attr_reader :model_class
9
10
 
@@ -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
- raise NotImplementedError, 'not implemented yet'
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
@@ -5,12 +5,12 @@ module Masterman
5
5
  class Csv < Base
6
6
  self.extensions = [:csv]
7
7
 
8
- def all
8
+ private
9
+
10
+ def find_all
9
11
  CSV.read(options[:path], loader_options)
10
12
  end
11
13
 
12
- private
13
-
14
14
  def default_loader_options
15
15
  { headers: true }
16
16
  end
@@ -5,7 +5,9 @@ module Masterman
5
5
  class Direct < Base
6
6
  self.extensions = []
7
7
 
8
- def all
8
+ private
9
+
10
+ def find_all
9
11
  options[:direct]
10
12
  end
11
13
 
@@ -5,7 +5,9 @@ module Masterman
5
5
  class Json < Base
6
6
  self.extensions = [:json]
7
7
 
8
- def all
8
+ private
9
+
10
+ def find_all
9
11
  ::JSON.parse(File.read(options[:path]))
10
12
  end
11
13
 
@@ -5,7 +5,9 @@ module Masterman
5
5
  class Yaml < Base
6
6
  self.extensions = [:yml]
7
7
 
8
- def all
8
+ private
9
+
10
+ def find_all
9
11
  ::YAML.load_file(options[:path])
10
12
  end
11
13
 
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Masterman
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.8'
3
3
  end
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/core'
9
+ require 'masterman/instance_accessor'
10
+ require 'masterman/class_accessor'
10
11
  require 'masterman/association'
11
- require 'masterman/attribute_methods'
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 Core
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.6
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-01 00:00:00.000000000 Z
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/core.rb
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.4.5.1
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
@@ -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