smooth_operator 1.2.5 → 1.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- Yzk0YmZmM2Q1NDE4YWY5YWYxOTYyZjZkYTk2NjBjNjQ4MjMxNDU4ZQ==
4
+ OTQyODc3MjY2NmU2MTlkYTU2NzM0MTRjMjUxYTY0MGRlMDEzNjU5NA==
5
5
  data.tar.gz: !binary |-
6
- OTllZjJjMDgyOGYxY2VkZmM4NWUzN2Y3Y2E5NTgxZjdlMDU5NjE1Ng==
6
+ MDI2NmM2NTYxODllNzRmOWE0ZTAxZjM5NTYzOGIzNmIxMTg1ZmJiZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YTgzYjIwYWEyMjE2YmJmZjk2YzllOWQyNWUwOWJjZTBlYjdjZDMxOTIwOWRh
10
- NmQ0MTc5NWQ4NDY1ZjMzMWM4MzljODk4OTEwOWM0MjQyN2JjODA2NjYwMGEz
11
- OTMzYjZlOTc2ZWY4YzU2ODNjOWE2MWYxMmIyYzJhNjE1ZTU3YTM=
9
+ YWJlMjY1OGI1NmY2NjJhMmVkNzI4YjNmZjYyZWFhMGM5NDNkZWNiYWVlMWZm
10
+ Mzg5MjQ4MTIxOTYxZDVhZmQ4OTQzMjc2YzdkOWI3MjhlMzAzZDg4Y2ZjOTdl
11
+ NDU1MzMyZWY0MWE3N2Q3YzhiNWQ2N2QxNDI5NDNkNWY1NjMwMGU=
12
12
  data.tar.gz: !binary |-
13
- YmU2NDdkOTY3ZjEwOGFkMzNiMDJiNzAzM2NlYjM4MDc1NTc4MDI0N2JjMDEx
14
- YzNkYTRlNWU2MDU4NzJmMjU2N2Q3NDExODkyNTU2YjJkZWFlNmFjOGM0N2Vl
15
- ZmUxMTU4YTI2YzFiYTU1YTNlNTNkYjNlYTg0N2U5NThmZDQzZTg=
13
+ YjU2NTgxMjlhODFhZjUyOWUyM2I0NzZkYmQ1NTgwMjI5ZGJkZDZkZjViYThi
14
+ YTJjZmZlYjhlZTNiMjA4MzJmMDc2NWE2MWU5NjI4MmQxODdmYzYxMGQ0N2Qz
15
+ NDZkM2Q2ZWQ1YmNlOGJlM2EyYTVkNTBkZTA1NzljMGU1ZDZiZTY=
@@ -3,11 +3,7 @@ module SmoothOperator
3
3
  module Delegation
4
4
 
5
5
  def respond_to?(method, include_private = false)
6
- if known_attribute?(method)
7
- true
8
- else
9
- self.class.reflect_on_association(method) ? true : super
10
- end
6
+ known_attribute?(method) ? true : super
11
7
  end
12
8
 
13
9
  def method_missing(method, *args, &block)
@@ -21,9 +17,7 @@ module SmoothOperator
21
17
  when :setter
22
18
  return push_to_internal_data(method_name, args.first)
23
19
  else
24
- if Helpers.safe_call(self.class, :reflect_on_association, method)
25
- return get_relation(method_name)
26
- elsif !self.class.strict_behaviour || known_attribute?(method_name)
20
+ if !self.class.strict_behaviour || known_attribute?(method_name)
27
21
  return get_internal_data(method_name)
28
22
  end
29
23
  end
@@ -1,17 +1,33 @@
1
- require "smooth_operator/relation/single_relation"
2
-
3
1
  module SmoothOperator
4
2
  module Relation
5
- class ArrayRelation < SingleRelation
3
+ class ArrayRelation
4
+
5
+ attr_reader :object, :association
6
+
7
+ def initialize(object, association)
8
+ @object, @association = object, association
9
+ end
10
+
11
+ def method_missing(method, *args, &block)
12
+ data.respond_to?(method) ? data.send(method, *args) : super
13
+ end
6
14
 
7
15
  def data
8
- super || []
16
+ data = object.get_internal_data(association.to_s)
17
+
18
+ data.nil? ? [] : [*data]
9
19
  end
10
20
 
11
21
  def reload
12
22
  "TODO"
13
23
  end
14
24
 
25
+ def new(attributes = {})
26
+ object.class.reflect_on_association(association).klass.new(attributes)
27
+ end
28
+
29
+ alias :build :new
30
+
15
31
  end
16
32
  end
17
33
  end
@@ -5,81 +5,76 @@ module SmoothOperator
5
5
  module Relation
6
6
  module Associations
7
7
 
8
- def relations
9
- @relations ||= {}
8
+ def has_many(nested_object_name, options = {})
9
+ accepts_nested_objects(nested_object_name, :has_many, options)
10
10
  end
11
11
 
12
- def get_relation(relation_name)
13
- if relations.include?(relation_name)
14
- relations[relation_name]
15
- else
16
- relations[relation_name] = build_relation(relation_name)
17
- end
12
+ def has_one(nested_object_name, options = {})
13
+ accepts_nested_objects(nested_object_name, :has_one, options)
18
14
  end
19
15
 
20
- def self.included(base)
21
- base.extend(ClassMethods)
16
+ def belongs_to(nested_object_name, options = {})
17
+ accepts_nested_objects(nested_object_name, :belongs_to, options)
22
18
  end
23
19
 
24
- protected ################### PROTECTED ###################
20
+ def reflections
21
+ Helpers.get_instance_variable(self, :reflections, {})
22
+ end
25
23
 
26
- def build_relation(relation_name)
27
- if self.class.reflect_on_association(relation_name.to_sym).has_many?
28
- ArrayRelation.new(self, relation_name)
29
- elsif Helpers.present? get_internal_data(relation_name)
30
- SingleRelation.new(self, relation_name)
31
- else
32
- nil
33
- end
24
+ def reflect_on_association(association)
25
+ reflections[association]
34
26
  end
35
27
 
36
- module ClassMethods
28
+ def reflect_on_all_associations(macro = nil)
29
+ macro ? reflections.values.select { |reflection| reflection.macro == macro } : reflections.values
30
+ end
37
31
 
38
- def has_many(nested_object_name, options = {})
39
- accepts_nested_objects(nested_object_name, :has_many, options)
40
- end
32
+ protected ###################### PROTECTED ###################
41
33
 
42
- def has_one(nested_object_name, options = {})
43
- accepts_nested_objects(nested_object_name, :has_one, options)
44
- end
34
+ def accepts_nested_objects(association, macro, options = {})
35
+ options = parse_options(options, { macro: macro })
45
36
 
46
- def belongs_to(nested_object_name, options = {})
47
- accepts_nested_objects(nested_object_name, :belongs_to, options)
48
- end
37
+ reflection = AssociationReflection.new(association, Reflection.new(name, {}), options)
49
38
 
50
- def reflections
51
- Helpers.get_instance_variable(self, :reflections, {})
52
- end
39
+ schema(association => reflection.klass)
53
40
 
54
- def reflect_on_association(association)
55
- reflections[association]
56
- end
41
+ reflections.merge!(association => reflection)
57
42
 
58
- def reflect_on_all_associations(macro = nil)
59
- macro ? reflections.values.select { |reflection| reflection.macro == macro } : reflections.values
43
+ if reflection.has_many?
44
+ define_has_many_association_method(reflection, association)
45
+ else
46
+ define_single_association_method(reflection, association)
60
47
  end
48
+ end
61
49
 
62
- protected ###################### PROTECTED ###################
50
+ private ####################### PRIVATE ######################
63
51
 
64
- def accepts_nested_objects(nested_object_name, macro, options = {})
65
- options = parse_options(options, { macro: macro })
52
+ def define_has_many_association_method(reflection, association)
53
+ define_method(association) do
54
+ array_relation = instance_variable_get("@#{association}")
66
55
 
67
- reflection = AssociationReflection.new(nested_object_name, Reflection.new(name, {}), options)
56
+ if array_relation.nil?
57
+ array_relation = ArrayRelation.new(self, association)
68
58
 
69
- reflections.merge!(nested_object_name => reflection)
59
+ instance_variable_set("@#{association}", array_relation)
60
+ end
70
61
 
71
- schema(nested_object_name => reflection.klass)
62
+ array_relation
72
63
  end
64
+ end
73
65
 
74
- private ####################### PRIVATE ######################
66
+ def define_single_association_method(reflection, association)
67
+ define_method(association) { get_internal_data(association.to_s) }
75
68
 
76
- def parse_options(options, default_options)
77
- options = options.is_a?(Hash) ? options.merge(default_options) : default_options
69
+ define_method("build_#{reflection.single_name}") { |attributes = {}| reflection.klass.new(attributes) }
70
+ end
78
71
 
79
- Helpers.symbolyze_keys(options)
80
- end
72
+ def parse_options(options, default_options)
73
+ options = options.is_a?(Hash) ? options.merge(default_options) : default_options
81
74
 
75
+ Helpers.symbolyze_keys(options)
82
76
  end
77
+
83
78
  end
84
79
  end
85
80
  end
@@ -1,3 +1,3 @@
1
1
  module SmoothOperator
2
- VERSION = "1.2.5"
2
+ VERSION = "1.2.6"
3
3
  end
@@ -11,12 +11,12 @@ module SmoothOperator
11
11
  class Base < OpenStruct::Base
12
12
 
13
13
  extend FinderMethods
14
+ extend Relation::Associations
14
15
  extend Translation if defined? I18n
15
16
 
16
17
  include Operator
17
18
  include Persistence
18
19
  include FinderMethods
19
- include Relation::Associations
20
20
 
21
21
  self.strict_behaviour = true
22
22
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smooth_operator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.5
4
+ version: 1.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - João Gonçalves
@@ -100,7 +100,6 @@ files:
100
100
  - lib/smooth_operator/relation/association_reflection.rb
101
101
  - lib/smooth_operator/relation/associations.rb
102
102
  - lib/smooth_operator/relation/reflection.rb
103
- - lib/smooth_operator/relation/single_relation.rb
104
103
  - lib/smooth_operator/remote_call/base.rb
105
104
  - lib/smooth_operator/remote_call/errors/connection_failed.rb
106
105
  - lib/smooth_operator/remote_call/errors/timeout.rb
@@ -1,21 +0,0 @@
1
- module SmoothOperator
2
- module Relation
3
- class SingleRelation
4
-
5
- attr_reader :object, :relation_name
6
-
7
- def initialize(object, relation_name)
8
- @object, @relation_name = object, relation_name
9
- end
10
-
11
- def method_missing(method, *args, &block)
12
- data.respond_to?(method) ? data.send(method, *args) : super
13
- end
14
-
15
- def data
16
- object.get_internal_data(relation_name)
17
- end
18
-
19
- end
20
- end
21
- end