contentful_model 0.0.3 → 0.0.4
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 +4 -4
- data/lib/contentful_model/chainable_queries.rb +3 -24
- data/lib/contentful_model/queries.rb +30 -0
- data/lib/contentful_model/query.rb +14 -4
- data/lib/contentful_model/version.rb +1 -1
- data/lib/contentful_model.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f655ef4ae60fcc26f4d88f3eedb7709f59489da5
|
4
|
+
data.tar.gz: 2e04d8f9ba67f17737ae0a63d72c91667ce910c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b1ea89fbd1f4accb2969d8a7382ffee88a29ed49004cc975da2aafa2f1ccd369e583cd95be2528e460fd43a6e8368edb8c3e036b5555062df04977582d8b29a
|
7
|
+
data.tar.gz: 0ef31f85d865ba031f454099ef2ab8aaccac6e388d7aa0ba16b72008ad945021c767515400fc3f2cd9bda1426f5210d5969bd9942e5e465fb1c6ac152e43a393
|
@@ -1,32 +1,23 @@
|
|
1
1
|
module ContentfulModel
|
2
2
|
module ChainableQueries
|
3
3
|
def self.included(base)
|
4
|
+
base.include ContentfulModel::Queries
|
4
5
|
base.extend ClassMethods
|
5
|
-
base.class_eval do
|
6
|
-
cattr_accessor :query
|
7
|
-
end
|
8
6
|
end
|
9
7
|
|
10
8
|
module ClassMethods
|
11
|
-
def get_query
|
12
|
-
@query ||= ContentfulModel::Query.new(self)
|
13
|
-
end
|
14
9
|
|
15
10
|
def all
|
16
|
-
get_query
|
17
11
|
raise ArgumentError, "You need to set self.content_type in your model class" if @content_type_id.nil?
|
18
12
|
self
|
19
13
|
end
|
20
14
|
|
21
15
|
def first
|
22
|
-
get_query
|
23
16
|
@query << {'limit' => 1}
|
24
|
-
|
17
|
+
load.first
|
25
18
|
end
|
26
19
|
|
27
20
|
def offset(n)
|
28
|
-
get_query
|
29
|
-
puts @query.inspect
|
30
21
|
@query << {'skip' => n}
|
31
22
|
self
|
32
23
|
end
|
@@ -34,12 +25,11 @@ module ContentfulModel
|
|
34
25
|
alias_method :skip, :offset
|
35
26
|
|
36
27
|
def find_by(*args)
|
37
|
-
get_query
|
38
28
|
args.each do |query|
|
39
29
|
#query is a hash
|
40
30
|
if query.values.first.is_a?(Array) #we need to do an 'in' query
|
41
31
|
@query << {"fields.#{query.keys.first}[in]" => query.values.first.join(",")}
|
42
|
-
|
32
|
+
elsif query.values.first.is_a?(String)
|
43
33
|
@query << {"fields.#{query.keys.first}" => query.values.first}
|
44
34
|
end
|
45
35
|
end
|
@@ -47,7 +37,6 @@ module ContentfulModel
|
|
47
37
|
end
|
48
38
|
|
49
39
|
def search(parameters)
|
50
|
-
get_query
|
51
40
|
if parameters.is_a?(Hash)
|
52
41
|
parameters.each do |field, search|
|
53
42
|
@query << {"fields.#{field}[match]" => search}
|
@@ -58,16 +47,6 @@ module ContentfulModel
|
|
58
47
|
self
|
59
48
|
end
|
60
49
|
|
61
|
-
def load
|
62
|
-
@query.execute
|
63
|
-
end
|
64
|
-
|
65
|
-
def find(id)
|
66
|
-
get_query
|
67
|
-
@query << {'sys.id' => id}
|
68
|
-
@query.execute.first
|
69
|
-
end
|
70
|
-
|
71
50
|
end
|
72
51
|
|
73
52
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ContentfulModel
|
2
|
+
module Queries
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
def inherited(subclass)
|
10
|
+
instanciate_query(subclass)
|
11
|
+
end
|
12
|
+
|
13
|
+
def instanciate_query(klass)
|
14
|
+
klass.instance_variable_set(:"@query",ContentfulModel::Query.new(klass))
|
15
|
+
end
|
16
|
+
|
17
|
+
def load
|
18
|
+
old_query = @query.dup
|
19
|
+
@query.reset
|
20
|
+
return old_query.execute
|
21
|
+
end
|
22
|
+
|
23
|
+
def find(id)
|
24
|
+
@query << {'sys.id' => id}
|
25
|
+
load.first
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -1,17 +1,27 @@
|
|
1
1
|
module ContentfulModel
|
2
2
|
class Query
|
3
3
|
attr_accessor :parameters
|
4
|
-
def initialize(
|
5
|
-
@parameters = parameters || {
|
6
|
-
@client =
|
4
|
+
def initialize(referenced_class, parameters=nil)
|
5
|
+
@parameters = parameters || {}
|
6
|
+
@client = referenced_class.send(:client)
|
7
|
+
@referenced_class = referenced_class
|
7
8
|
end
|
8
9
|
|
9
10
|
def <<(parameters)
|
10
11
|
@parameters.merge!(parameters)
|
11
12
|
end
|
12
13
|
|
14
|
+
def default_parameters
|
15
|
+
{ 'content_type' => @referenced_class.send(:content_type_id) }
|
16
|
+
end
|
17
|
+
|
13
18
|
def execute
|
14
|
-
@
|
19
|
+
query = @parameters.merge!(default_parameters)
|
20
|
+
return @client.entries(query)
|
21
|
+
end
|
22
|
+
|
23
|
+
def reset
|
24
|
+
@parameters = default_parameters
|
15
25
|
end
|
16
26
|
end
|
17
27
|
end
|
data/lib/contentful_model.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Error Creative Studio
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: contentful
|
@@ -65,6 +65,7 @@ files:
|
|
65
65
|
- lib/contentful_model.rb
|
66
66
|
- lib/contentful_model/base.rb
|
67
67
|
- lib/contentful_model/chainable_queries.rb
|
68
|
+
- lib/contentful_model/queries.rb
|
68
69
|
- lib/contentful_model/query.rb
|
69
70
|
- lib/contentful_model/version.rb
|
70
71
|
- lib/tasks/contentful_rails_tasks.rake
|