memory_record 0.0.2 → 0.0.3

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: 38453335376c705928d3272ad78e3803060f009c
4
- data.tar.gz: 629b27607427d553237b8ad44b8d78ff55c21428
3
+ metadata.gz: 8c9cd8f7e037fbdb335febc08e26283d0323f704
4
+ data.tar.gz: 0a3eaf7f098d120c2f4c6ce912a2856bd6948462
5
5
  SHA512:
6
- metadata.gz: 9e333eb93e434d2cb83dac1909366daebbefd8890fb8fde55dc2cf859f913b4cc83b13ea836755271d87b56c83d7b843399ce86d9b93aa489e7ecb5ddebfbb15
7
- data.tar.gz: e0f422196c47fc513dcf4bee670c1aca3d41706f30ef76baf7f617b0a959f6cfa38aa60b213fc92d98643defd4c8700d32d492ae4d6faf7d676600641504999f
6
+ metadata.gz: 4885b1819bb3cfdc97ee1acdeee43d4e6ceaf44e21d55c79d67054a52d7954db39539cd09f30d8f55d7a4271d9e932b27b095a54c52bc807759c4dd24789a5ca
7
+ data.tar.gz: a0bb34531ba701cf5a36def1d27d441169987299d64419031ac81dc362e218e2ef81623cdb4a9299de29ff0edb5aa423ff8c8fe404a42ecdfeada027809a4403
data/.travis.yml CHANGED
@@ -1,3 +1 @@
1
1
  language: ruby
2
- rvm:
3
- - 2.2.3
data/README.org CHANGED
@@ -28,7 +28,7 @@ class Language
28
28
  {key: :lisp, author: "John McCarthy" },
29
29
  {key: :c, author: "Dennis Ritchie" },
30
30
  {key: :ruby, author: "Yukihiro Matsumoto" },
31
- ], attr_reader_auto: true
31
+ ]
32
32
 
33
33
  def mr_author
34
34
  "Mr. #{author}"
@@ -53,7 +53,7 @@ class Foo
53
53
  {code: 1, key: :a, name: "A"},
54
54
  {code: 2, key: :b, name: "B"},
55
55
  {code: 3, key: :c, name: "C"},
56
- ], attr_reader: :name
56
+ ]
57
57
  end
58
58
 
59
59
  Foo.collect(&:code) # => [1, 2, 3]
@@ -93,18 +93,6 @@ object.key # => :a
93
93
  object.code # => 1
94
94
  #+END_SRC
95
95
 
96
- *** =attr_reader= option
97
-
98
- The attribute value can be referenced with =@attributes[:xxx]=, but it is troublesome to refer frequently, so it is methodized with =attr_reader: xxx=.
99
-
100
- *** =attr_reader_auto= option
101
-
102
- Automatically all attributes =attr_reader=
103
-
104
- *** What if I do not want you to feel free to access the attributes of a record?
105
-
106
- =attr_reader= with no method definition and reference =object.attributes[:xxx]=
107
-
108
96
  *** How do I add a method to an instance?
109
97
 
110
98
  For that, I am creating a new class so I need to define it normally
@@ -115,7 +103,7 @@ object.code # => 1
115
103
 
116
104
  *** =to_s= method is defined?
117
105
 
118
- = Alias of =name=, =to_s= is defined.
106
+ Alias of =name=, =to_s= is defined.
119
107
 
120
108
  *** If there is no key, use fetch to get an error
121
109
 
@@ -7,7 +7,7 @@ class Language
7
7
  {key: :lisp, author: "John McCarthy" },
8
8
  {key: :c, author: "Dennis Ritchie" },
9
9
  {key: :ruby, author: "Yukihiro Matsumoto" },
10
- ], attr_reader_auto: true
10
+ ]
11
11
 
12
12
  def mr_author
13
13
  "Mr. #{author}"
@@ -7,8 +7,8 @@ class Foo
7
7
  {code: 1, key: :a, name: 'A'},
8
8
  {code: 2, key: :b, name: 'B'},
9
9
  {code: 3, key: :c, name: 'C'},
10
- ], :attr_reader => :name
10
+ ]
11
11
  end
12
12
 
13
- Foo[2].name # => 'B'
14
- Foo[:b].name # => 'B'
13
+ Foo[2].name # => "B"
14
+ Foo[:b].name # => "B"
@@ -6,20 +6,20 @@ class Direction
6
6
  memory_record [
7
7
  {key: :left, name: '←', vector: [-1, 0]},
8
8
  {key: :right, name: '→', vector: [ 1, 0]},
9
- ], attr_reader_auto: true
9
+ ]
10
10
 
11
11
  def long_name
12
- '#{name}方向'
12
+ "#{name}方向"
13
13
  end
14
14
  end
15
15
 
16
- Direction.collect(&:name) # => ['', '']
16
+ Direction.collect(&:name) # => ["", ""]
17
17
  Direction.keys # => [:left, :right]
18
18
 
19
19
  Direction[:right].key # => :right
20
20
  Direction[:right].code # => 1
21
21
  Direction[:right].vector # => [1, 0]
22
- Direction[:right].long_name # => '→方向'
22
+ Direction[:right].long_name # => "→方向"
23
23
 
24
24
  Direction[1].key # => :right
25
25
 
@@ -3,19 +3,19 @@ require 'memory_record'
3
3
 
4
4
  class C
5
5
  include MemoryRecord
6
- memory_record [{key: :a}], attr_reader: :name
6
+ memory_record [{key: :a}]
7
7
  end
8
8
 
9
9
  C.keys # => [:a]
10
10
 
11
11
  # memory_record では更新できない
12
12
  class C
13
- memory_record [{key: :b}], attr_reader: :name
13
+ memory_record [{key: :b}]
14
14
  end
15
15
 
16
16
  C.keys # => [:a]
17
17
 
18
- # memory_record_list_set を使うこと
19
- C.memory_record_list_set [{key: :c}]
18
+ # memory_record_reset を使うこと
19
+ C.memory_record_reset [{key: :c}]
20
20
 
21
21
  C.keys # => [:c]
@@ -5,7 +5,7 @@ class Foo
5
5
  include MemoryRecord
6
6
  memory_record [
7
7
  {key: '↑', name: '上'},
8
- ], attr_reader: :name
8
+ ]
9
9
  end
10
10
 
11
- Foo['↑'].name # => ''
11
+ Foo['↑'].name # => ""
@@ -6,9 +6,9 @@ class Foo
6
6
  memory_record [
7
7
  {key: 'true', name: '有効'},
8
8
  {key: 'false', name: '無効'},
9
- ], attr_reader: :name
9
+ ]
10
10
  end
11
11
 
12
12
  flag = true
13
13
 
14
- Foo[flag.to_s].name # => '有効'
14
+ Foo[flag.to_s].name # => "有効"
@@ -1,4 +1,4 @@
1
- # freeze してしまうとこれらのメモ化ができなくなる
1
+ # If you freeze it will not be able to make these memos
2
2
 
3
3
  $LOAD_PATH.unshift '../lib'
4
4
  require 'memory_record'
@@ -13,7 +13,7 @@ class C2
13
13
  include MemoryRecord
14
14
  memory_record [
15
15
  {model: C},
16
- ], attr_reader_auto: true
16
+ ]
17
17
 
18
18
  def x
19
19
  @x ||= 'OK'
@@ -5,7 +5,7 @@ class Foo
5
5
  include MemoryRecord
6
6
  memory_record [
7
7
  {a: 10},
8
- ], attr_reader: :a
8
+ ]
9
9
 
10
10
  def a
11
11
  super * 2
@@ -6,7 +6,7 @@ class Foo
6
6
  memory_record [
7
7
  {name: 'alice'},
8
8
  {name: 'bob'},
9
- ], attr_reader_auto: true
9
+ ]
10
10
  end
11
11
 
12
12
  require 'active_model'
@@ -6,7 +6,7 @@ class Foo
6
6
  memory_record [
7
7
  {key: '01', name: '→'},
8
8
  {key: '02', name: '←'},
9
- ], attr_reader: :name
9
+ ]
10
10
  end
11
11
 
12
12
  Foo['01'] # => #<Foo:0x007fac2c35db28 @attributes={:key=>:'01', :name=>'→', :code=>0}>
@@ -16,9 +16,9 @@ end
16
16
  class GenderInfo
17
17
  include MemoryRecord
18
18
  memory_record [
19
- {key: :male, name: 'otoko'},
20
- {key: :female, name: 'onna'},
21
- ], attr_reader: :name
19
+ {key: :male, name: ''},
20
+ {key: :female, name: ''},
21
+ ]
22
22
  end
23
23
 
24
24
  class User < ActiveRecord::Base
@@ -30,4 +30,4 @@ class User < ActiveRecord::Base
30
30
  end
31
31
 
32
32
  user = User.create!(gender_key: :male)
33
- user.gender_info.name # => "otoko"
33
+ user.gender_info.name # => ""
@@ -9,30 +9,64 @@ module MemoryRecord
9
9
  extend ActiveSupport::Concern
10
10
 
11
11
  class_methods do
12
- def memory_record(list, **options, &block)
12
+ # Example
13
+ #
14
+ # memory_record [
15
+ # {id: 1, name: "alice"},
16
+ # {id: 2, name: "bob" },
17
+ # ], attr_reader: false
18
+ #
19
+ # or
20
+ #
21
+ # memory_record(attr_reader: false) do
22
+ # [
23
+ # {id: 1, name: "alice"},
24
+ # {id: 2, name: "bob" },
25
+ # ]
26
+ # end
27
+ #
28
+ def memory_record(records = nil, **options, &block)
13
29
  return if memory_record_defined?
14
30
 
31
+ if block
32
+ options = records || {}
33
+ records = block.call
34
+ end
35
+
15
36
  extend ActiveModel::Translation
16
37
  extend Enumerable
17
38
  include ::MemoryRecord::SingletonMethods
18
39
 
19
- class_attribute :memory_record_configuration
20
- self.memory_record_configuration = {
21
- attr_reader: [],
40
+ class_attribute :memory_record_options
41
+ self.memory_record_options = {
42
+ attr_reader: true,
22
43
  }.merge(options)
23
44
 
24
45
  if block_given?
25
- yield memory_record_configuration
46
+ yield memory_record_options
26
47
  end
27
48
 
28
- if memory_record_configuration[:attr_reader_auto]
29
- _attr_reader = list.inject([]) { |a, e| a | e.keys.collect(&:to_sym) }
30
- else
31
- _attr_reader = memory_record_configuration[:attr_reader]
49
+ attr_reader_args = []
50
+ all_keys = records.inject([]) { |a, e| a | e.keys.collect(&:to_sym) }
51
+ case v = memory_record_options[:attr_reader]
52
+ when true
53
+ attr_reader_args = all_keys
54
+ when Hash
55
+ if v[:only]
56
+ all_keys &= Array(v[:only])
57
+ elsif v[:except]
58
+ all_keys -= Array(v[:except])
59
+ end
60
+ attr_reader_args = all_keys
61
+ when Array
62
+ attr_reader_args = v
63
+ when Symbol, String
64
+ attr_reader_args = [v]
32
65
  end
33
66
 
67
+ # Define it to an ancestor so that super method can be used.
34
68
  include Module.new.tap { |m|
35
- ([:key, :code] + Array.wrap(_attr_reader)).uniq.each do |key|
69
+ ([:key, :code] + attr_reader_args).uniq.each do |key|
36
70
  m.class_eval do
37
71
  define_method(key) { @attributes[key.to_sym] }
38
72
  end
@@ -49,7 +83,7 @@ module MemoryRecord
49
83
  end
50
84
  }
51
85
 
52
- memory_record_list_set(list)
86
+ memory_record_reset(records)
53
87
  end
54
88
 
55
89
  def memory_record_defined?
@@ -64,7 +98,10 @@ module MemoryRecord
64
98
  end
65
99
 
66
100
  def lookup(key)
67
- return key if key.kind_of? self
101
+ if key.kind_of? self
102
+ return key
103
+ end
104
+
68
105
  case key
69
106
  when Symbol, String
70
107
  @values_hash[:key][key.to_sym]
@@ -75,7 +112,10 @@ module MemoryRecord
75
112
  alias [] lookup
76
113
 
77
114
  def fetch(key, default = nil, &block)
78
- raise ArgumentError if block_given? && default
115
+ if block_given? && default
116
+ raise ArgumentError
117
+ end
118
+
79
119
  v = lookup(key)
80
120
  unless v
81
121
  case
@@ -112,18 +152,20 @@ module MemoryRecord
112
152
 
113
153
  attr_reader :values
114
154
 
115
- def memory_record_list_set(list)
155
+ def memory_record_reset(records)
116
156
  @keys = nil
117
157
  @codes = nil
118
- @values = list.collect.with_index {|e, i| new(_attributes_normalize(e, i)) }.freeze
158
+ @values = records.collect.with_index { |e, i|
159
+ new(_attributes_normalize(e, i))
160
+ }.freeze
119
161
  @values_hash = {}
120
162
  [:code, :key].each do |pk|
121
163
  @values_hash[pk] = @values.inject({}) do |a, e|
122
164
  a.merge(e.send(pk) => e) do |key, a, b|
123
165
  raise ArgumentError, [
124
166
  "#{name}##{pk} #{key.inspect} is duplicate",
125
- "Existing: #{a.attributes.inspect}",
126
- "Collision: #{b.attributes.inspect}",
167
+ "Existing: #{a.attributes.inspect}",
168
+ "Conflict: #{b.attributes.inspect}",
127
169
  ].join("\n")
128
170
  end
129
171
  end
@@ -1,3 +1,3 @@
1
1
  module MemoryRecord
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -20,7 +20,7 @@ RSpec.describe MemoryRecord do
20
20
  def __define(table)
21
21
  Class.new {
22
22
  include MemoryRecord
23
- memory_record table, attr_reader_auto: true
23
+ memory_record table, attr_reader: true
24
24
  }
25
25
  end
26
26
 
@@ -82,7 +82,7 @@ RSpec.describe MemoryRecord do
82
82
  context 'Re-set' do
83
83
  before do
84
84
  @model = __define [{key: :a}]
85
- @model.memory_record_list_set [{key: :b}, {key: :c}]
85
+ @model.memory_record_reset [{key: :b}, {key: :c}]
86
86
  end
87
87
  it 'changed' do
88
88
  assert_equal [:b, :c], @model.keys
@@ -132,13 +132,13 @@ RSpec.describe MemoryRecord do
132
132
  end
133
133
  end
134
134
 
135
- describe 'attr_reader_auto' do
135
+ describe 'attr_reader' do
136
136
  class Model3
137
137
  include MemoryRecord
138
138
  memory_record [
139
139
  {a: 1},
140
140
  {b: 2},
141
- ], attr_reader_auto: true
141
+ ]
142
142
  end
143
143
 
144
144
  it do
@@ -149,8 +149,8 @@ RSpec.describe MemoryRecord do
149
149
 
150
150
  describe 'Do not duplicate key and code' do
151
151
  it do
152
- expect { Model.memory_record_list_set([{key: :a}, {key: :a}]) }.to raise_error(ArgumentError)
153
- expect { Model.memory_record_list_set([{code: 0}, {code: 0}]) }.to raise_error(ArgumentError)
152
+ expect { Model.memory_record_reset([{key: :a}, {key: :a}]) }.to raise_error(ArgumentError)
153
+ expect { Model.memory_record_reset([{code: 0}, {code: 0}]) }.to raise_error(ArgumentError)
154
154
  end
155
155
  end
156
156
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memory_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - akicho8
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-11 00:00:00.000000000 Z
11
+ date: 2017-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -94,21 +94,20 @@ files:
94
94
  - LICENSE.txt
95
95
  - README.org
96
96
  - Rakefile
97
- - examples/0110_basic.rb
98
- - examples/0120_attr_reader_auto_option.rb
99
- - examples/0130_for_legacy_code.rb
100
- - examples/0140_key_join_underscore_if_array.rb
101
- - examples/0150_behavior_like_hash.rb
102
- - examples/0160_practice.rb
103
- - examples/0170_reset.rb
104
- - examples/0180_key_is_anything_ok.rb
105
- - examples/0190_tips_on_handling_boolean_type.rb
106
- - examples/0200_freeze_makes_it_impossible_to_memorize.rb
107
- - examples/0210_super_usable.rb
108
- - examples/0230_validation.rb
109
- - examples/0240_key_duplicate.rb
110
- - examples/0250_when_key_like_numeric.rb
111
- - examples/0260_with_use_activerecord_enum.rb
97
+ - examples/0100_basic.rb
98
+ - examples/0110_for_legacy_code.rb
99
+ - examples/0120_key_join_underscore_if_array.rb
100
+ - examples/0130_behavior_like_hash.rb
101
+ - examples/0140_practice.rb
102
+ - examples/0150_reset.rb
103
+ - examples/0160_key_is_anything_ok.rb
104
+ - examples/0170_tips_on_handling_boolean_type.rb
105
+ - examples/0180_freeze_makes_it_impossible_to_memorize.rb
106
+ - examples/0190_super_usable.rb
107
+ - examples/0200_validation.rb
108
+ - examples/0210_key_duplicate.rb
109
+ - examples/0220_when_key_like_numeric.rb
110
+ - examples/0230_with_use_activerecord_enum.rb
112
111
  - lib/memory_record.rb
113
112
  - lib/memory_record/memory_record.rb
114
113
  - lib/memory_record/version.rb
@@ -1,14 +0,0 @@
1
- $LOAD_PATH.unshift '../lib'
2
- require 'memory_record'
3
-
4
- model = Class.new do
5
- include MemoryRecord
6
- memory_record [
7
- {a: 1},
8
- {b: 2},
9
- ], attr_reader_auto: true
10
- end
11
-
12
- model.first.name # => nil
13
- model.first.a # => 1
14
- model.first.b # => nil