fixed_record 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/Gemfile.lock +1 -1
  4. data/lib/fixed_record.rb +102 -100
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0e28a9a9f85d811dca1463d6d5ae8944d0d5fd66a098be6e93a9c86b939f4807
4
- data.tar.gz: 1849d6bb120d51aa6f275f1bc9c2d8e78166bf492c4e39887ed2fa010dbfa1fd
3
+ metadata.gz: 572787b07827ff2d2e0279f93bb3b78d37b8865f2063f4076184d792843e6a18
4
+ data.tar.gz: 3b53ae4bba8a9e090e872d550e18f7027f5e7d8719778d4e4f3a588ee7eee04d
5
5
  SHA512:
6
- metadata.gz: a03e177ac9d18c4ee7bd5f5589cdc9657369cc7444dd7b371ca644ec53c0030d160a2fd76ab3669093e44364baae267e1c165f47d23cbe481c7d2c04b61999ed
7
- data.tar.gz: a6462fa10d6eff0704dbd51aa3001a5a5ee367947eea83a32962de27c8fe68c907204b20860270063ac23cbd8ce50aea2697a01d1d0649bc7b58cb1962164165
6
+ metadata.gz: 6c47c29c44fb5ecd9788814a163d5e2eb28d5f14a1e5d5d362ab0d9182bb5c289d9e93c1032d75875f35a48cfc07c97b205964d2dc0f4bd18a7d3590399d8b05
7
+ data.tar.gz: baf11d31820fbcf012aba2bb55ba8d2056f77e90694c1bd6dd7f0d8daef2cda02f1d0323efbaa9c241120bde3d3de55287df9aa6c3a9089a087768b80acf1651
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Gem fixed_record Changelog
2
2
 
3
+ ## 0.6.0
4
+ * Internal refactoring - after reading _Metaprogramming Ruby_!
5
+ * Added size and length methods
6
+
3
7
  ## 0.5.0
4
8
  * Added class method `valid_names` to return valid names in records
5
9
  * Documented class method `filename`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fixed_record (0.5.0)
4
+ fixed_record (0.6.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/lib/fixed_record.rb CHANGED
@@ -3,8 +3,9 @@ require 'yaml'
3
3
  require 'psych'
4
4
  require 'set'
5
5
 
6
+ # Provides error-checked simplified access to a YAML data file
6
7
  class FixedRecord
7
- VERSION = "0.5.0"
8
+ VERSION = "0.6.0"
8
9
 
9
10
  # Lazy load data from given filename
10
11
  # creating accessors for top level attributes
@@ -13,51 +14,118 @@ class FixedRecord
13
14
  optional = optional.map( &:to_s )
14
15
  throw ArgumentError, "Required and Optional names overlap" unless (required & optional).empty?
15
16
 
16
- valid_keys = Set.new( required )
17
- valid_keys.merge( optional )
18
- required_keys = Set.new( required )
17
+ # Although not necessary, the class_eval makes it easier to see
18
+ # we are defining variables and methods in the context of the child class which
19
+ # called us
20
+ class_eval do
21
+ # Use @x names for class variables to simplify access
22
+ @filename = filename
23
+ @valid_keys = Set.new( required )
24
+ @valid_keys.merge( optional )
25
+ @required_keys = Set.new( required )
26
+ @singleton = singleton
27
+
28
+ # Load the data and create the methods...
29
+ def self.load!
30
+ return unless @items.nil?
31
+ begin
32
+ y = YAML.load_file( @filename )
33
+ rescue Errno::ENOENT
34
+ raise
35
+ rescue Psych::SyntaxError => error
36
+ fname = File.basename(@filename)
37
+ msg = error.message
38
+ if msg.include? @filename
39
+ msg.sub!( @filename, fname )
40
+ msg = "#{error.class.name} #{msg}"
41
+ else
42
+ msg = "#{error.class.name} #{fname} #{error.message}"
43
+ end
44
+ raise ArgumentError, msg
45
+ end
46
+
47
+ validate_structure( y, @singleton, @filename )
19
48
 
20
- self.class_variable_set( :@@filename, filename )
21
- self.class_variable_set( :@@required_keys, required_keys )
22
- self.class_variable_set( :@@valid_keys, valid_keys )
23
- self.class_variable_set( :@@items, nil )
24
- self.class_variable_set( :@@singleton, singleton )
49
+ if @valid_keys.empty?
50
+ # Grab keys from file
51
+ if @singleton
52
+ @valid_keys = y.keys
53
+ elsif y.is_a?(Array)
54
+ @valid_keys = y.first.keys
55
+ @required_keys = @valid_keys
56
+ elsif y.is_a?(Hash)
57
+ @valid_keys = y[y.keys.first].keys
58
+ @required_keys = @valid_keys
59
+ end
60
+ end
61
+
62
+ if @singleton
63
+ @items = y
64
+ elsif y.is_a?(Array)
65
+ @items = y.map.with_index do |values,i|
66
+ validate_item( @valid_keys, @required_keys, values, i )
67
+ r = new
68
+ r.instance_variable_set( :@values, values )
69
+ r
70
+ end
71
+ elsif y.is_a?(Hash)
72
+ @items = Hash.new
73
+ add_key = !@valid_keys.member?('key')
74
+ y.each do |k,values|
75
+ validate_item( @valid_keys, @required_keys, values, k )
76
+ values['key'] = k if add_key
77
+ r = new
78
+ r.instance_variable_set( :@values, values )
79
+ @items[k] = r
80
+ end
81
+ define_method( :key ) { @values['key'] } if add_key
82
+ end
83
+ create_methods( @valid_keys )
84
+ end
85
+
86
+ # filename data was loaded from
87
+ def self.filename
88
+ @filename
89
+ end
90
+
91
+ # valid keys (as strings)
92
+ def self.valid_names
93
+ load!
94
+ @valid_keys
95
+ end
96
+ end #class_eval
25
97
 
26
98
  if singleton
27
- class_eval %Q{
99
+ class_eval do # class methods for singleton object
28
100
  def self.[](k)
29
101
  load!
30
102
  k = k.to_s
31
- raise ArgumentError, "\#{k} is not a valid key" unless @@valid_keys.member?(k)
32
- @@items[k]
103
+ raise ArgumentError, "#{k} is not a valid key" unless @valid_keys.member?(k)
104
+ @items[k]
33
105
  end
34
- }
106
+ end # class_eval
35
107
  else
36
- class_eval %Q{
37
- class << self
38
- include Enumerable
39
- end
40
-
41
- def self.all
108
+ # Add methods for Coillection based objects
109
+ self.class.include Enumerable
110
+ class_eval do
111
+ def self.all
42
112
  load!
43
- @@items
113
+ @items
44
114
  end
45
115
 
46
116
  def self.each( &block )
47
- load!
48
- @@items.each(&block)
117
+ all.each(&block)
49
118
  end
50
119
 
51
- def self.count
52
- load!
53
- @@items.length
120
+ def self.size
121
+ all.size
54
122
  end
55
123
 
56
124
  def self.[]( k )
57
125
  if all.is_a?(Hash)
58
126
  all[k.to_s]
59
127
  else
60
- nil
128
+ nil # Arguably we could index the array, but if we did your code would smell...
61
129
  end
62
130
  end
63
131
 
@@ -68,76 +136,16 @@ class FixedRecord
68
136
  false
69
137
  end
70
138
  end
71
-
72
- }
73
- end
74
-
75
- class_eval %Q{
76
- def self.filename
77
- @@filename
139
+ end #class_eval
140
+
141
+ # Only way I can find to alias class methods...
142
+ class << self
143
+ alias length size
144
+ alias count size
78
145
  end
79
146
 
80
- def self.valid_names
81
- load!
82
- @@valid_keys
83
- end
84
-
85
- def self.load!
86
- if @@items.nil?
87
- begin
88
- y = YAML.load_file( @@filename )
89
- rescue Errno::ENOENT
90
- raise
91
- rescue Psych::SyntaxError => error
92
- fname = File.basename(@@filename)
93
- msg = error.message
94
- if msg.include? @@filename
95
- msg.sub!( @@filename, fname )
96
- msg = "\#{error.class.name} \#{msg}"
97
- else
98
- msg = "\#{error.class.name} \#{fname} \#{error.message}"
99
- end
100
- raise ArgumentError, msg
101
- end
102
- validate_structure( y, @@singleton, @@filename )
103
- if @@valid_keys.empty?
104
- # Grab keys from file
105
- if @@singleton
106
- @@valid_keys = y.keys
107
- elsif y.is_a?(Array)
108
- @@valid_keys = y.first.keys
109
- @@required_keys = @@valid_keys
110
- elsif y.is_a?(Hash)
111
- @@valid_keys = y[y.keys.first].keys
112
- @@required_keys = @@valid_keys
113
- end
114
- end
147
+ end
115
148
 
116
- if @@singleton
117
- @@items = y
118
- elsif y.is_a?(Array)
119
- @@items = y.map.with_index do |values,i|
120
- validate_item( @@valid_keys, @@required_keys, values, i )
121
- r = new
122
- r.instance_variable_set( :@values, values )
123
- r
124
- end
125
- elsif y.is_a?(Hash)
126
- @@items = Hash.new
127
- add_key = !@@valid_keys.member?('key')
128
- y.each do |k,values|
129
- validate_item( @@valid_keys, @@required_keys, values, k )
130
- values['key'] = k if add_key
131
- r = new
132
- r.instance_variable_set( :@values, values )
133
- @@items[k] = r
134
- end
135
- define_method( :key ) { @values['key'] } if add_key
136
- end
137
- create_methods( @@valid_keys )
138
- end
139
- end
140
- }
141
149
  end
142
150
 
143
151
 
@@ -155,11 +163,7 @@ private
155
163
  define_method( key.to_sym) { @values[key] }
156
164
  end
157
165
  # Test if a value is defined (could be nil) for a name
158
- class_eval %Q{
159
- def present?(name)
160
- @values.key?( name.to_s )
161
- end
162
- }
166
+ define_method( :present? ) { |name| @values.key?(name.to_s)}
163
167
  end
164
168
 
165
169
 
@@ -205,6 +209,4 @@ private
205
209
  validate( values, index )
206
210
  end
207
211
 
208
-
209
-
210
212
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixed_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Bell
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-06 00:00:00.000000000 Z
11
+ date: 2020-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler