dato 0.1.17 → 0.1.18

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c9a4f4edd88febcd394e74c7e0d3da7be4b36ae4
4
- data.tar.gz: daed03a684e4d497f9096ba0d9815ea6b81abd01
3
+ metadata.gz: 8ca87da24b5ba4828b5cc9b96045bed562e57f79
4
+ data.tar.gz: 07687fceac70df85b38e917b1d00ef4810a112cb
5
5
  SHA512:
6
- metadata.gz: 90e12d686cd45a8f4dfe874ca6f2af7892e407c945ad6054518d8f655d3cdbb4edd738bc8104f84d1420318c150c9312edecea3e528f55889def3c14d599f40d
7
- data.tar.gz: ba8fe3950475b56b40c6c41e05335bc23cc13302f7fd034e2f7c403714549701f24503d53c03f9ce70d04fb854c98844fada1b49b4922a0e38dd5cacee24f124
6
+ metadata.gz: 90da28a4087d9bbaaa9e29fcc31bafcff7b757b282372949de1fd58bdac8f6b5c94f088b1c28da541ca54ab6cb7e7fbdc07dfe73d14e755baa5f8ea8e9a4ea25
7
+ data.tar.gz: 44f2d443e6d49df140c6532173f1d66e0881d30ea1723bb970ed5c90ff74c67618594870402e02ea6a04796471941c2b2c8b991f08f12782e7ab7762a02b8f98
@@ -117,6 +117,14 @@ module Dato
117
117
  else
118
118
  super
119
119
  end
120
+ rescue NoMethodError
121
+ message = []
122
+ message << "Undefined method `#{method}`"
123
+ message << 'Available fields for this DatoCMS item:'
124
+ message += fields.map do |f|
125
+ "* .#{f.api_key}"
126
+ end
127
+ raise NoMethodError, message.join("\n")
120
128
  end
121
129
  end
122
130
  end
@@ -5,12 +5,13 @@ require 'dato/local/item'
5
5
  module Dato
6
6
  module Local
7
7
  class ItemsRepo
8
- attr_reader :entities_repo, :collections_by_type
8
+ attr_reader :entities_repo, :collections_by_type, :item_type_methods
9
9
 
10
10
  def initialize(entities_repo)
11
11
  @entities_repo = entities_repo
12
12
  @collections_by_type = {}
13
13
  @items_by_id = {}
14
+ @item_type_methods = {}
14
15
 
15
16
  build_cache!
16
17
  end
@@ -29,19 +30,39 @@ module Dato
29
30
 
30
31
  private
31
32
 
32
- def item_type_key(item_type)
33
- api_key = item_type.api_key
34
- if item_type.singleton
35
- [api_key.to_sym, true]
36
- else
37
- [api_key.pluralize.to_sym, false]
33
+ def build_cache!
34
+ build_item_type_methods!
35
+ build_collections_by_type!
36
+ end
37
+
38
+ def build_item_type_methods!
39
+ @item_type_methods = {}
40
+
41
+ singleton_keys = singleton_item_type_entities.map(&:api_key)
42
+ collection_keys = collection_item_type_entities.map(&:api_key)
43
+ .map(&:pluralize)
44
+
45
+ clashing_keys = singleton_keys & collection_keys
46
+
47
+ item_type_entities.each do |item_type|
48
+ singleton = item_type.singleton
49
+ pluralized_api_key = item_type.api_key.pluralize
50
+ method = singleton ? item_type.api_key : pluralized_api_key
51
+
52
+ if clashing_keys.include?(pluralized_api_key)
53
+ suffix = singleton ? 'instance' : 'collection'
54
+ method = "#{method}_#{suffix}"
55
+ end
56
+
57
+ @item_type_methods[item_type] = [method.to_sym, singleton]
38
58
  end
39
59
  end
40
60
 
41
- def build_cache!
61
+ def build_collections_by_type!
42
62
  item_type_entities.each do |item_type|
43
- key, singleton = item_type_key(item_type)
44
- @collections_by_type[key] = if singleton
63
+ method, singleton = item_type_methods[item_type]
64
+
65
+ @collections_by_type[method] = if singleton
45
66
  nil
46
67
  else
47
68
  ItemCollection.new
@@ -50,12 +71,12 @@ module Dato
50
71
 
51
72
  item_entities.each do |item_entity|
52
73
  item = Item.new(item_entity, self)
74
+ method, singleton = item_type_methods[item_entity.item_type]
53
75
 
54
- key, singleton = item_type_key(item_entity.item_type)
55
76
  if singleton
56
- @collections_by_type[key] = item
77
+ @collections_by_type[method] = item
57
78
  else
58
- @collections_by_type[key].push item
79
+ @collections_by_type[method].push item
59
80
  end
60
81
 
61
82
  @items_by_id[item.id] = item
@@ -70,12 +91,28 @@ module Dato
70
91
  entities_repo.find_entities_of_type('item')
71
92
  end
72
93
 
94
+ def singleton_item_type_entities
95
+ item_type_entities.select(&:singleton)
96
+ end
97
+
98
+ def collection_item_type_entities
99
+ item_type_entities - singleton_item_type_entities
100
+ end
101
+
73
102
  def method_missing(method, *arguments, &block)
74
103
  if collections_by_type.key?(method) && arguments.empty?
75
104
  collections_by_type[method]
76
105
  else
77
106
  super
78
107
  end
108
+ rescue NoMethodError
109
+ message = []
110
+ message << "Undefined method `#{method}`"
111
+ message << 'Available DatoCMS collections/items:'
112
+ message += collections_by_type.map do |key, _value|
113
+ "* .#{key}"
114
+ end
115
+ raise NoMethodError, message.join("\n")
79
116
  end
80
117
 
81
118
  class ItemCollection < Array
data/lib/dato/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Dato
3
- VERSION = '0.1.17'
3
+ VERSION = '0.1.18'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.17
4
+ version: 0.1.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefano Verna