active_pstore 0.4.1 → 0.4.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 125870653562bd257a502f6750c4e2bb792e6b0a
4
- data.tar.gz: 25311665810359fc3c960c5a7f6818baca48532d
3
+ metadata.gz: 16594a19df0b062b6a1866925cf24b98199834de
4
+ data.tar.gz: e5354198e64e1075210336cf814faad3d22cf1ec
5
5
  SHA512:
6
- metadata.gz: 63d9efc14e56ae15d00e1411983e8326dd1d1d7dac5dae82bb0f894af443e7a6333993e00c03e44f0bab9e8afd8968d9a39e78b3af7c8238a49f67c2b261b902
7
- data.tar.gz: 398cd85df1d2d39e4f7e65c122c2d6a0784df3ba5f79da8a03132db9e797a8ffa6bd717cc5511f5c64b4c556030897322a95fed2a9aae9069ccbf1c517c21c38
6
+ metadata.gz: 0d8d2fae4a20dbb40894438ac0ec4f7473dbbf1224ddc861d0b13da225dfbc12d56d07c46184345b3ec6ef5a6d9450509c7978ee2b508880ba2ef43a9a53f5cf
7
+ data.tar.gz: 29d6384cf79b3c34a6159fa4f9326e37b886c46d2b79527cd1e48528715aa7dae17a55434032733ab537ffd82bb5d7c4771d339edb7518b07dd56829c9bcd9c8
data/README.ja.md CHANGED
@@ -98,6 +98,16 @@ Or install it yourself as:
98
98
  $ gem install active_pstore
99
99
  ```
100
100
 
101
+ And require it as:
102
+
103
+ ```
104
+ require 'active_pstore'
105
+ ```
106
+
107
+ ## プレゼンテーション文書
108
+
109
+ * 誕生秘話 ... http://www.slideshare.net/koic/software-development-and-rubygems
110
+
101
111
  ## LICENCE
102
112
 
103
113
  The MIT Licence
data/README.md CHANGED
@@ -97,6 +97,16 @@ Or install it yourself as:
97
97
  $ gem install active_pstore
98
98
  ```
99
99
 
100
+ And require it as:
101
+
102
+ ```
103
+ require 'active_pstore'
104
+ ```
105
+
106
+ ## Presentation Document
107
+
108
+ * The making of a story (Japanese text) ... http://www.slideshare.net/koic/software-development-and-rubygems
109
+
100
110
  ## LICENCE
101
111
 
102
112
  The MIT Licence
@@ -1,6 +1,7 @@
1
1
  module ActivePStore
2
2
  class Base
3
3
  extend ActivePStore::ConnectionHandling
4
+ extend ActivePStore::DynamicMatchers
4
5
  extend ActivePStore::FinderMethods
5
6
  extend ActivePStore::Inheritance
6
7
  extend ActivePStore::Querying
@@ -0,0 +1,121 @@
1
+ #
2
+ # Original code is ActiveRecord::DynamicMatcher, thx.
3
+ #
4
+ # https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/dynamic_matchers.rb
5
+ #
6
+ module ActivePStore
7
+ module DynamicMatchers
8
+ def respond_to?(name, include_private = false)
9
+ if self == Base
10
+ super
11
+ else
12
+ match = Method.match(self, name)
13
+ match || super
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def method_missing(name, *arguments, &block)
20
+ match = Method.match(self, name)
21
+
22
+ if match
23
+ match.define
24
+ send(name, *arguments, &block)
25
+ else
26
+ super
27
+ end
28
+ end
29
+
30
+ class Method
31
+ @matchers = []
32
+
33
+ class << self
34
+ attr_reader :matchers
35
+
36
+ def match(model, name)
37
+ klass = matchers.find {|k| k.pattern === name }
38
+ klass.new(model, name) if klass
39
+ end
40
+
41
+ def pattern
42
+ @pattern ||= /\A#{prefix}_([_a-zA-Z]\w*)#{suffix}\Z/
43
+ end
44
+
45
+ def prefix
46
+ raise NotImplementedError
47
+ end
48
+
49
+ def suffix
50
+ ''
51
+ end
52
+ end
53
+
54
+ attr_reader :model, :name, :attribute_names
55
+
56
+ def initialize(model, name)
57
+ @model = model
58
+ @name = name.to_s
59
+ @attribute_names = @name.match(self.class.pattern)[1].split('_and_')
60
+ end
61
+
62
+ def define
63
+ model.class_eval <<-CODE, __FILE__, __LINE__ + 1
64
+ def self.#{name}(#{signature})
65
+ #{body}
66
+ end
67
+ CODE
68
+ end
69
+
70
+ private
71
+
72
+ def body
73
+ "#{finder}(#{attributes_hash})"
74
+ end
75
+
76
+ # The parameters in the signature may have reserved Ruby words, in order
77
+ # to prevent errors, we start each param name with `_`.
78
+ def signature
79
+ attribute_names.map {|name| "_#{name}" }.join(', ')
80
+ end
81
+
82
+ # Given that the parameters starts with `_`, the finder needs to use the
83
+ # same parameter name.
84
+ def attributes_hash
85
+ "{" + attribute_names.map {|name| ":#{name} => _#{name}" }.join(',') + "}"
86
+ end
87
+
88
+ def finder
89
+ raise NotImplementedError
90
+ end
91
+ end
92
+
93
+ class FindBy < Method
94
+ Method.matchers << self
95
+
96
+ def self.prefix
97
+ 'find_by'
98
+ end
99
+
100
+ def finder
101
+ 'find_by'
102
+ end
103
+ end
104
+
105
+ class FindByBang < Method
106
+ Method.matchers << self
107
+
108
+ def self.prefix
109
+ 'find_by'
110
+ end
111
+
112
+ def self.suffix
113
+ '!'
114
+ end
115
+
116
+ def finder
117
+ 'find_by!'
118
+ end
119
+ end
120
+ end
121
+ end
@@ -1,3 +1,3 @@
1
1
  module ActivePStore
2
- VERSION = '0.4.1'
2
+ VERSION = '0.4.2'
3
3
  end
data/lib/active_pstore.rb CHANGED
@@ -3,6 +3,7 @@ module ActivePStore; end
3
3
  require 'active_pstore/collection'
4
4
  require 'active_pstore/connection_handling'
5
5
  require 'active_pstore/core'
6
+ require 'active_pstore/dynamic_matchers'
6
7
  require 'active_pstore/finder_methods'
7
8
  require 'active_pstore/inheritance'
8
9
  require 'active_pstore/persistence'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_pstore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koichi ITO
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-06 00:00:00.000000000 Z
11
+ date: 2015-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: codeclimate-test-reporter
@@ -52,6 +52,7 @@ files:
52
52
  - lib/active_pstore/collection.rb
53
53
  - lib/active_pstore/connection_handling.rb
54
54
  - lib/active_pstore/core.rb
55
+ - lib/active_pstore/dynamic_matchers.rb
55
56
  - lib/active_pstore/errors.rb
56
57
  - lib/active_pstore/finder_methods.rb
57
58
  - lib/active_pstore/inheritance.rb