lazy_record 0.1.7 → 0.1.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: 2c230f7e027c8258f3252edb747c7287ad6e3328
4
- data.tar.gz: 7c79ccae1ec72aa41c7eb33a19092186e07b081d
3
+ metadata.gz: 9fcd565fc270b0c3ecb5278894e3f6eb1c3287d4
4
+ data.tar.gz: b647814ace73eda6f34300868e852cdbe5eb6724
5
5
  SHA512:
6
- metadata.gz: 58bebeffd2050ecde571b12fbd761271f3dd1ca7c86abde51e3aa0ed68f3d6c5f797eb4a212d832f71faa46b460402eb0e2d2f61f0f75305e8c173765c656eba
7
- data.tar.gz: 61ef6f43c5db74633417eb8cf3ad239e7ac2bee3d5d2dde3f75758c5313698e525ce33361c8ea3dbdc5444731ec10a57e28a0312d59c4120bfc8f010f8514b6b
6
+ metadata.gz: 0ccfaad3e66cd2c7c516f1a791d12dd0d5fab7c5ff0dd5b9c5f1ed01bd09ecde2c584aa78ed1a47e76421fd4a682a55d4c19ca70cab25dc1efe7c5612e80db09
7
+ data.tar.gz: cc8e35116bb835526f000f498e71f79e3d7a3d6d3101b0d9921cf5a789c1020f4b0bdf38f39b8e7395ef39f30afcd541360d814b6a0f6f059929d2b783c29d39
data/.gitignore CHANGED
@@ -7,3 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .idea/
11
+ .ruby-*
data/example/person.rb CHANGED
@@ -80,5 +80,5 @@ class Person < LazyRecord::Base
80
80
  friend: true,
81
81
  years_known: 6
82
82
  }
83
- ).freeze
83
+ )
84
84
  end
@@ -25,6 +25,28 @@ module LazyRecord
25
25
  end
26
26
  end
27
27
 
28
+ def define_collection_counter(collection)
29
+ define_method("#{collection}_count") do
30
+ send(collection).count
31
+ end
32
+ end
33
+
34
+ def define_collections(*collections)
35
+ define_method(:collections) do
36
+ instance_variable_get(:@collections) || instance_variable_set(:@collections,
37
+ collections)
38
+ end
39
+ end
40
+
41
+ def define_collection_counts_to_s
42
+ define_method(:collection_counts_to_s) do
43
+ collections.map do |collection|
44
+ "#{collection}_count: #{stringify_value(send("#{collection}_count"))}"
45
+ end
46
+ end
47
+ private :collection_counts_to_s
48
+ end
49
+
28
50
  def apply_nesting(class_name)
29
51
  "#{to_s.split('::')[0..-3].join('::')}::#{class_name}"
30
52
  end
@@ -33,10 +55,13 @@ module LazyRecord
33
55
  include mod = get_or_set_mod(COLLECTION_MODULE_NAME)
34
56
  mod.extend(Associations)
35
57
  mod.module_eval do
58
+ define_collections(*collections)
59
+ define_collection_counts_to_s
36
60
  collections.each do |collection|
37
61
  class_name = collection.to_s.classify
38
62
  define_collection_getter(collection, class_name)
39
63
  define_collection_setter(collection, class_name)
64
+ define_collection_counter(collection)
40
65
  end
41
66
  end
42
67
  end
@@ -21,13 +21,8 @@ module LazyRecord
21
21
 
22
22
  def define_initialize
23
23
  define_method(:initialize) do |opts = {}, &block|
24
- opts = opts.inject({}) do |memo, (k,v)|
25
- memo[k.to_sym] = v
26
- memo
27
- end
28
-
29
- instance_attr_accessors.each do |attr|
30
- send("#{attr}=", opts[attr.to_sym])
24
+ opts.each do |k, v|
25
+ send("#{k}=", v) if respond_to?("#{k}=")
31
26
  end
32
27
 
33
28
  block&.call self
@@ -39,25 +34,12 @@ module LazyRecord
39
34
  define_method(:instance_attrs_to_s) do
40
35
  instance_attr_accessors.map do |attr|
41
36
  value = send(attr)
42
- attr_to_s = stringify_value(value)
43
- "#{attr.to_s.delete(':')}: #{attr_to_s}"
37
+ "#{attr}: #{stringify_value(value)}"
44
38
  end
45
39
  end
46
40
  private :instance_attrs_to_s
47
41
  end
48
42
 
49
- def define_stringify_value
50
- define_method(:stringify_value) do |value|
51
- if value.is_a?(String)
52
- "\"#{value}\""
53
- elsif value.nil?
54
- 'nil'
55
- else
56
- value
57
- end
58
- end
59
- end
60
-
61
43
  def define_instance_attr_accessors(*names)
62
44
  define_method(:instance_attr_accessors) do
63
45
  names.map(&:to_sym)
@@ -73,7 +55,6 @@ module LazyRecord
73
55
  define_instance_attr_accessors(*names)
74
56
  define_initialize
75
57
  define_instance_attrs_to_s
76
- define_stringify_value
77
58
  end
78
59
  end
79
60
  end
@@ -31,16 +31,35 @@ module LazyRecord
31
31
  []
32
32
  end
33
33
 
34
+ def collection_counts_to_s
35
+ []
36
+ end
37
+
34
38
  def inspect
35
39
  "#<#{self.class} id: #{id ? id : 'nil'}"\
36
- "#{instance_attrs_to_s.unshift('').join(', ')}>"
40
+ "#{instance_attrs_to_s.unshift('').join(', ')}"\
41
+ "#{collection_counts_to_s.unshift('').join(', ')}>"
37
42
  end
38
43
 
39
44
  def id
40
45
  @id.freeze
41
46
  end
42
47
 
43
- private :id=
48
+ def stringify_value(value)
49
+ if value.is_a?(String)
50
+ "\"#{value}\""
51
+ elsif value.nil?
52
+ 'nil'
53
+ else
54
+ value
55
+ end
56
+ end
57
+
58
+ private :id=,
59
+ :stringify_value,
60
+ :instance_attrs_to_s,
61
+ :instance_attr_accessors,
62
+ :collection_counts_to_s
44
63
 
45
64
  # Class methods provided to all LazyRecord classes
46
65
  module ClassMethods
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LazyRecord
4
- VERSION = '0.1.7'
4
+ VERSION = '0.1.8'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - M. Simon Borg