lazy_record 0.2.2 → 0.3.0

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: 55c8da2e4b0c99ab9bb225170580f6d36370c7d7
4
- data.tar.gz: '08d95435954bb6ad6ab13087663d6e25ffd66600'
3
+ metadata.gz: 1dd02d425756e1e782fb8ddb8200ebd979d8b46d
4
+ data.tar.gz: 8033557b1ed171bacc267b69402f59122f496ee6
5
5
  SHA512:
6
- metadata.gz: 5f46a8ab2737916b7a867a6b2ad44d68f1d89e1618ce01a7857dc0879eca825894a8e73cab580dc2b032fed26e6d9e327e08c9cc1734bb0de65242b8babac126
7
- data.tar.gz: bd977ce90033072edcc5d8b6ae7e50d79a87b87dc9d0f76a7c5eb19349d4b4ee41607abc260b9161a818a7e36e225766170a0e453181807cf9bfc8347e7acaae
6
+ metadata.gz: 5e9680e081c05fef1268a6b6ec662634bce7ba75eff2d5db8518bbe9eaa36152b28f4bb66fffa44ab03d11897f35ada16d1abf59377a58fd72ddfdb50a32fb85
7
+ data.tar.gz: 2de4c3dd15b30c3540e1f01142c5c03155af15720ecd83aa4ee828e8228c567c3b19903e94611d9e4e8d18f02efb32b89eb88ed5d622caae6d62a7d07b6de32f
data/lazy_record.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.email = ['msimonborg@gmail.com']
12
12
 
13
13
  spec.summary = 'Some ActiveRecord magic for your table-less POROs. WIP.'
14
- spec.description = 'Add some convenience macros for your POROs that cut down on boilerplate code. Method definition macros, more powerful attr_accessors, and easy associations between in-memory objects. Somewhat mocks the ActiveRecord API to make it feel comfortable and intuitive for Rails developers. The main intent of this project is to explore dynamic programming in Ruby. Maybe someone will find it useful. WIP.'
14
+ spec.description = 'Add some convenience macros for your POROs that cut down on boilerplate code. Method definition macros, more powerful attr_accessors, and easy associations between in-memory objects. Mocks the ActiveRecord API to make it feel comfortable and intuitive for Rails developers. The main intent of this project is to explore dynamic programming in Ruby. Maybe someone will find it useful. WIP.'
15
15
  spec.homepage = 'https://www.github.com/msimonborg/lazy_record'
16
16
  spec.license = 'MIT'
17
17
  spec.required_ruby_version = '>= 2.1.0'
@@ -7,7 +7,7 @@ module LazyRecord
7
7
  NESTED_ATTRS_MODULE_NAME = :NestedAttributes
8
8
 
9
9
  def define_collection_getter(collection)
10
- model = find_scoped_class(collection)
10
+ model = find_scoped_collection_class(collection)
11
11
  define_method(collection) do
12
12
  if instance_variable_get("@#{collection}").nil?
13
13
  instance_variable_set("@#{collection}", Relation.new(model: model))
@@ -17,7 +17,7 @@ module LazyRecord
17
17
  end
18
18
 
19
19
  def define_collection_setter(collection)
20
- model = find_scoped_class(collection)
20
+ model = find_scoped_collection_class(collection)
21
21
  define_method("#{collection}=") do |coll|
22
22
  coll = Relation.new(model: model, array: coll) if coll.is_a?(Array)
23
23
  return instance_variable_set("@#{collection}", coll) if coll.is_a? Relation
@@ -25,7 +25,7 @@ module LazyRecord
25
25
  end
26
26
  end
27
27
 
28
- def find_scoped_class(collection)
28
+ def find_scoped_collection_class(collection)
29
29
  -> { apply_nesting(collection.to_s.classify).constantize }
30
30
  end
31
31
 
@@ -60,6 +60,12 @@ module LazyRecord
60
60
  mod.module_eval { add_collection_methods(collections) }
61
61
  end
62
62
 
63
+ def lr_has_one(*args)
64
+ include mod = get_or_set_mod(COLLECTION_MODULE_NAME)
65
+ mod.extend(Associations) unless mod.const_defined?('Associations')
66
+ mod.module_eval { add_has_one_methods(args) }
67
+ end
68
+
63
69
  def add_collection_methods(collections)
64
70
  define_collections(*collections)
65
71
  define_collection_counts_to_s
@@ -70,7 +76,49 @@ module LazyRecord
70
76
  end
71
77
  end
72
78
 
73
- def define_association_attributes_setter(collection, class_name)
79
+ def add_has_one_methods(args)
80
+ define_has_one_associations(*args)
81
+ define_has_one_associations_to_s
82
+ args.each do |association|
83
+ define_association_setter(association)
84
+ define_association_getter(association)
85
+ end
86
+ end
87
+
88
+ def define_has_one_associations(*args)
89
+ define_method(:associations) do
90
+ args
91
+ end
92
+ end
93
+
94
+ def define_has_one_associations_to_s
95
+ define_method(:has_one_associations_to_s) do
96
+ associations.map do |association|
97
+ "#{association}: #{stringify_value(send(association))}"
98
+ end
99
+ end
100
+ private :has_one_associations_to_s
101
+ end
102
+
103
+ def define_association_setter(association)
104
+ model = find_scoped_association_class(association)
105
+ define_method("#{association}=") do |assoc|
106
+ return instance_variable_set("@#{association}", assoc) if assoc.is_a? model.call
107
+ raise ArgumentError, "Argument must be a #{model.call}"
108
+ end
109
+ end
110
+
111
+ def define_association_getter(association)
112
+ define_method(association) do
113
+ instance_variable_get("@#{association}")
114
+ end
115
+ end
116
+
117
+ def find_scoped_association_class(association)
118
+ -> { apply_nesting(association.to_s.camelize).constantize }
119
+ end
120
+
121
+ def define_collection_attributes_setter(collection, class_name)
74
122
  define_method("#{collection}_attributes=") do |collection_attributes|
75
123
  collection_attributes.values.each do |attributes|
76
124
  send(collection) << class_name.new(attributes)
@@ -85,7 +133,7 @@ module LazyRecord
85
133
  mod.module_eval do
86
134
  collections.each do |collection|
87
135
  class_name = collection.to_s.classify.constantize
88
- define_association_attributes_setter(collection, class_name)
136
+ define_collection_attributes_setter(collection, class_name)
89
137
  end
90
138
  end
91
139
  end
@@ -36,9 +36,14 @@ module LazyRecord
36
36
  []
37
37
  end
38
38
 
39
+ def has_one_associations_to_s
40
+ []
41
+ end
42
+
39
43
  def inspect
40
44
  "#<#{self.class} id: #{id ? id : 'nil'}"\
41
45
  "#{instance_attrs_to_s.unshift('').join(', ')}"\
46
+ "#{has_one_associations_to_s.unshift('').join(', ')}"\
42
47
  "#{collection_counts_to_s.unshift('').join(', ')}>"
43
48
  end
44
49
 
@@ -64,26 +69,28 @@ module LazyRecord
64
69
 
65
70
  # Class methods provided to all LazyRecord classes
66
71
  module ClassMethods
67
- attr_reader :all
72
+ def all
73
+ @all ||= Relation.new(model: self)
74
+ end
68
75
 
69
76
  def count
70
- @all.count
77
+ all.count
71
78
  end
72
79
 
73
80
  def first
74
- @all.first
81
+ all.first
75
82
  end
76
83
 
77
84
  def last
78
- @all.last
85
+ all.last
79
86
  end
80
87
 
81
88
  def where(condition = nil, &block)
82
- @all.where(condition, &block)
89
+ all.where(condition, &block)
83
90
  end
84
91
 
85
92
  def destroy_all
86
- @all.send(:clear)
93
+ all.send(:clear)
87
94
  end
88
95
  end
89
96
  end
@@ -63,6 +63,10 @@ module LazyRecord
63
63
  self[-1]
64
64
  end
65
65
 
66
+ def empty?
67
+ all.empty?
68
+ end
69
+
66
70
  def clear
67
71
  all.clear
68
72
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LazyRecord
4
- VERSION = '0.2.2'
4
+ VERSION = '0.3.0'
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.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - M. Simon Borg
@@ -82,7 +82,7 @@ dependencies:
82
82
  version: '10.0'
83
83
  description: Add some convenience macros for your POROs that cut down on boilerplate
84
84
  code. Method definition macros, more powerful attr_accessors, and easy associations
85
- between in-memory objects. Somewhat mocks the ActiveRecord API to make it feel comfortable
85
+ between in-memory objects. Mocks the ActiveRecord API to make it feel comfortable
86
86
  and intuitive for Rails developers. The main intent of this project is to explore
87
87
  dynamic programming in Ruby. Maybe someone will find it useful. WIP.
88
88
  email: