contentful_model 0.1.0.1 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8b6d798eaadc837a847885b9ead996769e692048
4
- data.tar.gz: b80b759d2d0442067530bbb17580de7464606de6
3
+ metadata.gz: 199e61fb05a5d359cbf69b01d5db2dba308239a9
4
+ data.tar.gz: b9478b41fda3f6b2870d54a9f627d44a9662c0ce
5
5
  SHA512:
6
- metadata.gz: f8d2ec2fb167a45b1d072c7c94c45024fc89ad8a07e8bb3fe577db9236ec79e8d200127243984130dcf4c275b185a083655ac63e096983d9f0d69ddc385c2606
7
- data.tar.gz: 59c21e1d2d95c8f9ef695dfc1faa99d8f065fab572ff69961b1957279e1d02fa51357d262559facd7454862eea07957c419a507d76ea9cb4c8d3e30d74d7da2b
6
+ metadata.gz: 8801e3881b967c5cf1e44918cb605a58ded0fc33e1247018aa718c934c491159f3f8b2b6ad33931e5ac3b329ba63351a7caf94a786eeec6548910f0d0364ec0f
7
+ data.tar.gz: 20e98e13891d9b9b33c0b50e192b5b9ddef673e522f631b90f53f279b4f424cda334f220af2a2a9b7068bc6a39dca2f3d2714f7ab11f17d8cdd75b8603f356a9
@@ -25,6 +25,24 @@ module ContentfulModel
25
25
  inverse_of: self.to_s.underscore.to_sym
26
26
  }
27
27
  options = default_options.merge(opts)
28
+ # Set up the association name for the instance which loaded this object
29
+ # This is useful in situations where, primarily, it's a 1:* relationship (i.e. belongs_to)
30
+ # even though this isn't actually supported by Contentful
31
+ #
32
+ # f = Foo.first
33
+ # b = f.bars.first
34
+ # b.foo #would return the instance of Foo which loaded it
35
+
36
+ define_method association_names.to_s.singularize do
37
+ instance_variable_get(:"@#{association_names.to_s.singularize}")
38
+ end
39
+
40
+ define_method "#{association_names.to_s.singularize}=" do |parent|
41
+ instance_variable_set(:"@#{association_names.to_s.singularize}",parent)
42
+ return self
43
+ end
44
+
45
+ # Set up the association name (plural)
28
46
  if self.respond_to?(association_names)
29
47
  self.send(association_names)
30
48
  else
@@ -18,14 +18,19 @@ module ContentfulModel
18
18
  # @param association_names [Symbol] the name of the child model, as a plural symbol
19
19
  def has_many(association_names, options = {})
20
20
  default_options = {
21
- class_name: association_names.to_s.singularize.classify
21
+ class_name: association_names.to_s.singularize.classify,
22
+ inverse_of: self.to_s.underscore.to_s
22
23
  }
23
24
  options = default_options.merge(options)
24
25
  define_method association_names do
25
26
  begin
26
- # try calling the association name directly on the superclass - will be picked up by
27
- # ContentfulModel::Base#method_missing and return a value if there is one for the attribute
28
- super()
27
+ # Start by calling the association name as a method on the superclass.
28
+ # this will end up in ContentfulModel::Base#method_missing and return the value from Contentful.
29
+ # We set the singular of the association name on each object in the collection to allow easy
30
+ # reverse recursion without another API call (i.e. finding the Foo which called .bars())
31
+ super().collect do |child|
32
+ child.send(:"#{options[:inverse_of]}=",self)
33
+ end
29
34
  rescue ContentfulModel::AttributeNotFoundError
30
35
  # If AttributeNotFoundError is raised, that means that the association name isn't available on the object.
31
36
  # We try to call the class name (pluralize) instead, or give up and return an empty collection
@@ -17,14 +17,17 @@ module ContentfulModel
17
17
  # @param options [Hash] a hash, the only key of which is important is class_name.
18
18
  def has_one(association_name, options = {})
19
19
  default_options = {
20
- class_name: association_name.to_s.classify
20
+ class_name: association_name.to_s.classify,
21
+ inverse_of: self.to_s.underscore.to_sym
21
22
  }
22
23
  options = default_options.merge(options)
23
24
  # Define a method which matches the association name
24
25
  define_method association_name do
25
26
  begin
26
- # Start by calling the association name as a method on the superclass. This will probably end up at ContentfulModel::Base#method_missing
27
- super()
27
+ # Start by calling the association name as a method on the superclass.
28
+ # this will end up in ContentfulModel::Base#method_missing and return the value from Contentful.
29
+ # We set the singular of the association name on this object to allow easy recursing.
30
+ super().send(:"#{options[:inverse_of]}=",self)
28
31
  rescue ContentfulModel::AttributeNotFoundError
29
32
  # If method_missing returns an error, the field doesn't exist. If a class is specified, try that.
30
33
  if options[:class_name].underscore.to_sym != association_name
@@ -1,3 +1,3 @@
1
1
  module ContentfulModel
2
- VERSION = "0.1.0.1"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contentful_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Error Creative Studio