contentful_model 0.0.7 → 0.0.8

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: f1199c6bcfa0312db762ee9d3a5873dbe082db12
4
- data.tar.gz: c9ddc557291ce3eaca06cd860f53e2d1ad4a6401
3
+ metadata.gz: b0c557075e720d6f54dd624f9342c0b9a001909e
4
+ data.tar.gz: 8c0705dac36560cf57a4c09022e6c0f9ee77c0db
5
5
  SHA512:
6
- metadata.gz: b577849558fb930dbf9479b620f7bfcdde58712eed7ba961dbb1edffd9d385cd0f0c3cde3cc3e9c805f92b7a8b9b7ffbfcc74dc5ba5794a713b8d2dff92d82d4
7
- data.tar.gz: 1c64cc844615b9dc4d8c187d54fa650ca0ae71d9640a12f2c35e75a283153dc06e15278eb55d005f57492ebc1c1270076c5e8dca5726776f1a7ca6f21c8d8208
6
+ metadata.gz: 8b9463610d3aa57d4abde50284b46a9293b70772571aff50d9c3d6b6698087d2ebe606148ffd6067082213e458fd59210761fd9d406e97f8622c9934970dad70
7
+ data.tar.gz: 8e9ed3329c34045efd4211dae3b5526b1c3abdc64a9c8d35b220e5dc2194f972674e84c9df100d4ea476da29b33cd9a987e07a29e84b8ef74b6ca3850ae34b34
@@ -0,0 +1,61 @@
1
+ # A module to map relationships, a little like ActiveRecord::Relation
2
+ # This is necessary because Contentful::Link classes are not 2-way, so you can't
3
+ # get the parent from a child.
4
+ module ContentfulModel
5
+ module Associations
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ # has_many is called on the parent model, and sets an instance var on the child
12
+ # which is named the plural of the class this module is mixed into.
13
+ #
14
+ # e.g
15
+ # class Foo
16
+ # has_many :bars
17
+ # end
18
+ # @param classname [Symbol] the name of the child model, as a plural symbol
19
+ def has_many(classname)
20
+ #define an instance method called the same as the arg passed in
21
+ #e.g. bars()
22
+ define_method "#{classname}" do
23
+ # call bars() on super, and for each, call bar=(self)
24
+ super().collect do |instance|
25
+ instance.send(:"#{self.class.to_s.singularize.camelize(:lower)}=",self)
26
+ #return the instance to the collect() method
27
+ instance
28
+ end
29
+ end
30
+ end
31
+
32
+ # has_one is called on the parent model, and sets a single instance var on the child
33
+ # it's conceptually identical to `has_many()`
34
+ # which is named the singular of the class this module is mixed into
35
+ def has_one(classname)
36
+ define_method "#{classname}" do
37
+ super().send(:"#{self.class.to_s.singularize.camelize(:lower)}=",self)
38
+ instance
39
+ end
40
+ end
41
+
42
+
43
+ # belongs_to is called on the child, and creates methods for mapping to the parent
44
+ # @param classname [Symbol] the singular name of the parent
45
+ def belongs_to(classname)
46
+ raise ArgumentError, "belongs_to requires a class name as a symbol" unless classname.is_a?(Symbol)
47
+ define_method "#{classname}" do
48
+ #this is where we need to return the parent class
49
+ self.instance_variable_get(:"@#{classname}")
50
+ end
51
+
52
+ define_method "#{classname}=" do |instance|
53
+ #this is where we need to set the class name
54
+ self.instance_variable_set(:"@#{classname}",instance)
55
+ end
56
+
57
+
58
+ end
59
+ end
60
+ end
61
+ end
@@ -1,6 +1,7 @@
1
1
  module ContentfulModel
2
2
  class Base < Contentful::Entry
3
3
  include ContentfulModel::ChainableQueries
4
+ include ContentfulModel::Associations
4
5
 
5
6
  def initialize(*args)
6
7
  super
@@ -8,7 +8,6 @@ module ContentfulModel
8
8
 
9
9
  def inherited(subclass)
10
10
  instantiate_query(subclass)
11
- subclass.send(:attr_reader,:query)
12
11
  end
13
12
 
14
13
  def instantiate_query(klass)
@@ -1,3 +1,3 @@
1
1
  module ContentfulModel
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -2,6 +2,7 @@ require 'contentful'
2
2
  require "contentful_model/query"
3
3
  require "contentful_model/queries"
4
4
  require "contentful_model/chainable_queries"
5
+ require "contentful_model/associations"
5
6
  require "contentful_model/base"
6
7
  require "active_support/all"
7
8
 
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.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Error Creative Studio
@@ -63,6 +63,7 @@ files:
63
63
  - MIT-LICENSE
64
64
  - Rakefile
65
65
  - lib/contentful_model.rb
66
+ - lib/contentful_model/associations.rb
66
67
  - lib/contentful_model/base.rb
67
68
  - lib/contentful_model/chainable_queries.rb
68
69
  - lib/contentful_model/queries.rb