memory_record 0.0.11 → 0.0.12

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
  SHA256:
3
- metadata.gz: ad1f7912204a4b4b9a098d2258b13e6bc9ab9d1ef231b5d9d7ebb4a97f6cf338
4
- data.tar.gz: 997946f9e26295cf48485487eb8b2b99c941f76fc9600d6edf6a8594a9ff28c7
3
+ metadata.gz: 8a31161518612170878b45f58d9952e75403826b6ed1a3345e3a116b81b8b24c
4
+ data.tar.gz: d0c23e8933f7576c00608b5cc1f5fe233b3f609db3371d22b5ac25e08d198d02
5
5
  SHA512:
6
- metadata.gz: 34edca154caf9330a144c06c6f96859ac90286d6cadf5008a795819f2744547b85ed2213ad9aa420476c7ff44533076c113b5b0233a01f06ffdaa3cf7634e798
7
- data.tar.gz: 6736b1ba71f5003c62b57dc64f06cea334c34a5bad64538112ec03c8f668c12e0fc1aa2c9de93c2b08dfff580dd9f5b464888ef6c8014d8857856699e2a7393d
6
+ metadata.gz: 830f15f5f99178dc80e2f142d10aa4dd7f23431aca3ede47bb3591624d1e98e190485500742eb2622487f929cecd56f5507252b7b4941727e83315bc00ff4cad
7
+ data.tar.gz: bd185cef177836f31268c760225e7df2935f5f42921420168819f4007543d8108046b0d4f7bd76d3e5fb41d61ed83ff726d6d3bf3973f06e6c382278129e6d29
data/README.md CHANGED
@@ -29,26 +29,32 @@ $ bundle install
29
29
  ### Basic usage
30
30
 
31
31
  ```ruby
32
- class Language
32
+ class Palette
33
33
  include MemoryRecord
34
34
  memory_record [
35
- {key: :lisp, author: "John McCarthy" },
36
- {key: :c, author: "Dennis Ritchie" },
37
- {key: :ruby, author: "Yukihiro Matsumoto" },
35
+ { key: :coral, r: 255, g: 127, b: 0 },
36
+ { key: :tomato, r: 255, g: 99, b: 71 },
37
+ { key: :gold, r: 255, g: 215, b: 0 },
38
38
  ]
39
39
 
40
- def mr_author
41
- "Mr. #{author}"
40
+ def rgb
41
+ [r, g, b]
42
42
  end
43
- end
44
43
 
45
- Language[:ruby].key # => :ruby
46
- Language[:ruby].code # => 2
47
- Language[:ruby].author # => "Yukihiro Matsumoto"
48
- Language[:ruby].mr_author # => "Mr. Yukihiro Matsumoto"
44
+ def hex
45
+ "#" + rgb.collect { |e| "%02X" % e }.join
46
+ end
49
47
 
50
- Language.keys # => [:lisp, :c, :ruby]
51
- Language.collect(&:author) # => ["John McCarthy", "Dennis Ritchie", "Yukihiro Matsumoto"]
48
+ def name
49
+ super.capitalize
50
+ end
51
+ end
52
+
53
+ Palette[:tomato].key # => :tomato
54
+ Palette[:tomato].name # => "Tomato"
55
+ Palette[:tomato].rgb # => [255, 99, 71]
56
+ Palette[:tomato].hex # => "#FF6347"
57
+ Palette.collect(&:hex) # => ["#FF7F00", "#FF6347", "#FFD700"]
52
58
  ```
53
59
 
54
60
  ### How to turn as an array?
@@ -56,14 +62,14 @@ Language.collect(&:author) # => ["John McCarthy", "Dennis Ritchie", "Yukihiro Ma
56
62
  **Enumerable** extended, so that **each** method is available
57
63
 
58
64
  ```ruby
59
- Foo.each { |e| ... }
60
- Foo.collect { |e| ... }
65
+ Palette.each { |e| ... }
66
+ Palette.collect { |e| ... }
61
67
  ```
62
68
 
63
69
  ### How do I submit a form to select in Rails?
64
70
 
65
71
  ```ruby
66
- form.collection_select(:selection_code, Foo, :code, :name)
72
+ form.collection_select(:selection_code, Palette, :code, :name)
67
73
  ```
68
74
 
69
75
  ### Is the reference in subscripts slow?
@@ -71,15 +77,15 @@ form.collection_select(:selection_code, Foo, :code, :name)
71
77
  Since it has a hash internally using the key value as a key, it can be acquired with O (1).
72
78
 
73
79
  ```ruby
74
- Foo[1].name # => "A"
75
- Foo[:a].name # => "A"
80
+ Palette[1].name # => "Tomato"
81
+ Palette[:tomato].name # => "Tomato"
76
82
  ```
77
83
 
78
84
  ### Instances always react to **code** and **key**
79
85
 
80
86
  ```ruby
81
- object = Foo.first
82
- object.key # => :a
87
+ object = Palette[:tomato]
88
+ object.key # => :tomato
83
89
  object.code # => 1
84
90
  ```
85
91
 
@@ -98,23 +104,23 @@ Alias of **name**, **to_s** is defined.
98
104
  ### If there is no key, use fetch to get an error
99
105
 
100
106
  ```ruby
101
- Foo.fetch(:xxx) # => <KeyError: ...>
107
+ Palette.fetch(:xxx) # => <KeyError: ...>
102
108
  ```
103
109
 
104
110
  The following are all the same
105
111
 
106
112
  ```ruby
107
- Foo[:xxx] || :default # => :default
108
- Foo.fetch(:xxx, :default} # => :default
109
- Foo.fetch(:xxx) { :default } # => :default
113
+ Palette[:xxx] || :default # => :default
114
+ Palette.fetch(:xxx, :default} # => :default
115
+ Palette.fetch(:xxx) { :default } # => :default
110
116
  ```
111
117
 
112
118
  ### Use fetch_if to ignore if the key is nil
113
119
 
114
120
  ```ruby
115
- Foo.fetch_if(nil) # => nil
116
- Foo.fetch_if(:a) # => #<Foo:... @attributes={...}>
117
- Foo.fetch_if(:xxx) # => <KeyError: ...>
121
+ Palette.fetch_if(nil) # => nil
122
+ Palette.fetch_if(:tomato) # => #<Palette:... @attributes={...}>
123
+ Palette.fetch_if(:xxx) # => <KeyError: ...>
118
124
  ```
119
125
 
120
126
  ### How to refer to other keys
@@ -123,9 +129,9 @@ Foo.fetch_if(:xxx) # => <KeyError: ...>
123
129
  class Foo
124
130
  include MemoryRecord
125
131
  memory_record [
126
- {key: :a, other_key: :x},
127
- {key: :b, other_key: :y},
128
- {key: :c, other_key: :z},
132
+ { key: :a, other_key: :x },
133
+ { key: :b, other_key: :y },
134
+ { key: :c, other_key: :z },
129
135
  ]
130
136
 
131
137
  class << self
@@ -146,35 +152,16 @@ Foo[:b] == Foo[:y] # => true
146
152
  Foo[:c] == Foo[:z] # => true
147
153
  ```
148
154
 
149
- ### How can I prohibit the hash key from being attr_reader automatically?
150
-
151
- #### attr_reader: false
152
-
153
- I think that it is better to use it when you want to make it difficult to access easily.
154
-
155
- ```ruby
156
- class Foo
157
- include MemoryRecord
158
- memory_record attr_reader: false do
159
- [
160
- {x: 1, y: 1, z: 1},
161
- ]
162
- end
163
- end
164
-
165
- Foo.first.x rescue $! # => #<NoMethodError: undefined method `x' for #<Foo:0x007fb2c710eda8>>
166
- Foo.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<Foo:0x007fb2c710eda8>>
167
- Foo.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<Foo:0x007fb2c710eda8>>
168
- ```
155
+ ### How prohibit the hash key from being attr_reader automatically?
169
156
 
170
- #### attr_reader: {only: :y}
157
+ Use `attr_reader: false` or `attr_reader: {only: ...}` or `attr_reader: {except: ...}`
171
158
 
172
159
  ```ruby
173
160
  class Foo
174
161
  include MemoryRecord
175
162
  memory_record attr_reader: {only: :y} do
176
163
  [
177
- {x: 1, y: 1, z: 1},
164
+ { x: 1, y: 1, z: 1 },
178
165
  ]
179
166
  end
180
167
  end
@@ -184,36 +171,18 @@ Foo.first.y rescue $! # => 1
184
171
  Foo.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<Foo:0x007fcc861ff108>>
185
172
  ```
186
173
 
187
- #### attr_reader: {except: :y}
188
-
189
- ```ruby
190
- class Foo
191
- include MemoryRecord
192
- memory_record attr_reader: {except: :y} do
193
- [
194
- {x: 1, y: 1, z: 1},
195
- ]
196
- end
197
- end
198
-
199
- Foo.first.x rescue $! # => 1
200
- Foo.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<Foo:0x007ff033895e88>>
201
- Foo.first.z rescue $! # => 1
202
- ```
203
-
204
174
  *** How to decide **code** yourself?
205
175
 
206
176
  ```ruby
207
177
  class Foo
208
178
  include MemoryRecord
209
179
  memory_record [
210
- {code: 1, key: :a, name: "A"},
211
- {code: 2, key: :b, name: "B"},
212
- {code: 3, key: :c, name: "C"},
180
+ { code: 1, key: :a, name: "A" },
181
+ { code: 2, key: :b, name: "B" },
213
182
  ]
214
183
  end
215
184
 
216
- Foo.collect(&:code) # => [1, 2, 3]
185
+ Foo.collect(&:code) # => [1, 2]
217
186
  ```
218
187
 
219
188
  It is not recommended to specify it explicitly.
@@ -1,23 +1,29 @@
1
1
  $LOAD_PATH.unshift '../lib'
2
2
  require 'memory_record'
3
3
 
4
- class Language
4
+ class Palette
5
5
  include MemoryRecord
6
6
  memory_record [
7
- {key: :lisp, author: "John McCarthy" },
8
- {key: :c, author: "Dennis Ritchie" },
9
- {key: :ruby, author: "Yukihiro Matsumoto" },
7
+ { key: :coral, r: 255, g: 127, b: 0 },
8
+ { key: :tomato, r: 255, g: 99, b: 71 },
9
+ { key: :gold, r: 255, g: 215, b: 0 },
10
10
  ]
11
11
 
12
- def mr_author
13
- "Mr. #{author}"
12
+ def rgb
13
+ [r, g, b]
14
+ end
15
+
16
+ def hex
17
+ "#" + rgb.collect { |e| "%02X" % e }.join
14
18
  end
15
- end
16
19
 
17
- Language[:ruby].key # => :ruby
18
- Language[:ruby].code # => 2
19
- Language[:ruby].author # => "Yukihiro Matsumoto"
20
- Language[:ruby].mr_author # => "Mr. Yukihiro Matsumoto"
20
+ def name
21
+ super.capitalize
22
+ end
23
+ end
21
24
 
22
- Language.keys # => [:lisp, :c, :ruby]
23
- Language.collect(&:author) # => ["John McCarthy", "Dennis Ritchie", "Yukihiro Matsumoto"]
25
+ Palette[:tomato].key # => :tomato
26
+ Palette[:tomato].name # => "Tomato"
27
+ Palette[:tomato].rgb # => [255, 99, 71]
28
+ Palette[:tomato].hex # => "#FF6347"
29
+ Palette.collect(&:hex) # => ["#FF7F00", "#FF6347", "#FFD700"]
@@ -14,7 +14,12 @@ class Foo
14
14
  def name
15
15
  "(#{super})"
16
16
  end
17
+
18
+ def to_h
19
+ super.merge(x: 1)
20
+ end
17
21
  end
18
22
 
19
23
  Foo.first.a # => 20
20
24
  Foo.first.name # => "(_key0)"
25
+ Foo.first.to_h # => {:a=>10, :code=>0, :key=>:_key0, :x=>1}
@@ -13,5 +13,5 @@ class Foo
13
13
  ]
14
14
  end
15
15
 
16
- Foo["01"] # => #<Foo:0x00007f9e519ba6d0 @attributes={:key=>:"01", :name=>"left", :code=>0}>
17
- Foo["02"] # => #<Foo:0x00007f9e519ba568 @attributes={:key=>:"02", :name=>"right", :code=>1}>
16
+ Foo["01"] # => #<Foo:0x00007fb30e1ddcd8 @attributes={:key=>:"01", :name=>"left", :code=>0}>
17
+ Foo["02"] # => #<Foo:0x00007fb30e1ddbc0 @attributes={:key=>:"02", :name=>"right", :code=>1}>
@@ -10,9 +10,9 @@ class C1
10
10
  end
11
11
  end
12
12
 
13
- C1.first.x rescue $! # => #<NoMethodError: undefined method `x' for #<C1:0x00007f83bc8e7078>>
14
- C1.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<C1:0x00007f83bc8e7078>>
15
- C1.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<C1:0x00007f83bc8e7078>>
13
+ C1.first.x rescue $! # => #<NoMethodError: undefined method `x' for #<C1:0x00007fb4d51afd60>>
14
+ C1.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<C1:0x00007fb4d51afd60>>
15
+ C1.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<C1:0x00007fb4d51afd60>>
16
16
 
17
17
  class C2
18
18
  include MemoryRecord
@@ -23,9 +23,9 @@ class C2
23
23
  end
24
24
  end
25
25
 
26
- C2.first.x rescue $! # => #<NoMethodError: undefined method `x' for #<C2:0x00007f83bc9419d8>>
26
+ C2.first.x rescue $! # => #<NoMethodError: undefined method `x' for #<C2:0x00007fb4d50dfb60>>
27
27
  C2.first.y rescue $! # => 1
28
- C2.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<C2:0x00007f83bc9419d8>>
28
+ C2.first.z rescue $! # => #<NoMethodError: undefined method `z' for #<C2:0x00007fb4d50dfb60>>
29
29
 
30
30
  class C3
31
31
  include MemoryRecord
@@ -37,5 +37,5 @@ class C3
37
37
  end
38
38
 
39
39
  C3.first.x rescue $! # => 1
40
- C3.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<C3:0x00007f83bc948968>>
40
+ C3.first.y rescue $! # => #<NoMethodError: undefined method `y' for #<C3:0x00007fb4d50d4440>>
41
41
  C3.first.z rescue $! # => 1
@@ -15,5 +15,5 @@ class C
15
15
  end
16
16
  end
17
17
 
18
- C.lookup(:alice) # => #<C:0x00007ffe599ba4d8 @attributes={:key=>:alice, :code=>0}>
19
- C[:alice] # => #<C:0x00007ffe599ba4d8 @attributes={:key=>:alice, :code=>0}>
18
+ C.lookup(:alice) # => #<C:0x00007fa9dc11d8b0 @attributes={:key=>:alice, :code=>0}>
19
+ C[:alice] # => #<C:0x00007fa9dc11d8b0 @attributes={:key=>:alice, :code=>0}>
@@ -17,6 +17,6 @@ class Foo
17
17
  end
18
18
  end
19
19
 
20
- v = Foo.first # => #<Foo:0x00007fdc05a532d0 @attributes={:key=>:a, :code=>0}, @var=1>
20
+ v = Foo.first # => #<Foo:0x00007f9da02595a0 @attributes={:key=>:a, :code=>0}, @var=1>
21
21
  v.frozen? # => true
22
22
  v.a # => 1
@@ -62,38 +62,32 @@ module MemoryRecord
62
62
  end
63
63
 
64
64
  # Define it to an ancestor so that super method can be used.
65
- include Module.new.tap { |m|
65
+ include Module.new {
66
66
  ([:key, :code] + attr_reader_args).uniq.each do |key|
67
- m.module_eval do
68
- define_method(key) { @attributes[key.to_sym] }
69
- end
67
+ define_method(key) { @attributes[key.to_sym] }
70
68
  end
71
69
 
72
- unless m.method_defined?(:name)
73
- m.module_eval do
74
- define_method(:name) { key.to_s }
75
- end
70
+ unless method_defined?(:name)
71
+ define_method(:name) { key.to_s }
76
72
  end
77
73
 
78
- m.module_eval do
79
- # sort matches definition order
80
- def <=>(other)
81
- [self.class, code] <=> [other.class, other.code]
82
- end
74
+ # sort matches definition order
75
+ def <=>(other)
76
+ [self.class, code] <=> [other.class, other.code]
77
+ end
83
78
 
84
- # Even if duplicate, objects match
85
- def ==(other)
86
- self.class == other.class && key == other.key
87
- end
79
+ # Even if duplicate, objects match
80
+ def ==(other)
81
+ self.class == other.class && key == other.key
82
+ end
88
83
 
89
- # Even if object_id of objects used as hash keys are different, they match. It also speeds up by defining hash.
90
- def eql?(other)
91
- self.class == other.class && key == other.key
92
- end
84
+ # Even if object_id of objects used as hash keys are different, they match. It also speeds up by defining hash.
85
+ def eql?(other)
86
+ self.class == other.class && key == other.key
87
+ end
93
88
 
94
- def hash
95
- self.class.hash ^ key.hash
96
- end
89
+ def hash
90
+ self.class.hash ^ key.hash
97
91
  end
98
92
  }
99
93
 
@@ -205,7 +199,7 @@ module MemoryRecord
205
199
 
206
200
  attr_reader :attributes
207
201
 
208
- delegate :[], to: :attributes
202
+ delegate :[], :to_h, to: :attributes
209
203
  delegate :to_s, to: :name
210
204
 
211
205
  def initialize(attributes)
@@ -1,3 +1,3 @@
1
1
  module MemoryRecord
2
- VERSION = '0.0.11'
2
+ VERSION = '0.0.12'
3
3
  end
@@ -71,6 +71,7 @@ RSpec.describe MemoryRecord do
71
71
 
72
72
  context 'instance accessor' do
73
73
  it do
74
+ assert Model.first.to_h
74
75
  assert Model.first.attributes
75
76
  assert Model.first.key
76
77
  assert Model.first.code
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.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - akicho8
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-14 00:00:00.000000000 Z
11
+ date: 2018-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake