contentful_model 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/contentful_model/associations.rb +61 -0
- data/lib/contentful_model/base.rb +1 -0
- data/lib/contentful_model/queries.rb +0 -1
- data/lib/contentful_model/version.rb +1 -1
- data/lib/contentful_model.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b0c557075e720d6f54dd624f9342c0b9a001909e
|
4
|
+
data.tar.gz: 8c0705dac36560cf57a4c09022e6c0f9ee77c0db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/contentful_model.rb
CHANGED
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.
|
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
|