Class: Dato::Local::ItemsRepo

Inherits:
Object
  • Object
show all
Defined in:
lib/dato/local/items_repo.rb

Defined Under Namespace

Classes: ItemCollection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entities_repo) ⇒ ItemsRepo

Returns a new instance of ItemsRepo.



11
12
13
14
15
16
17
18
# File 'lib/dato/local/items_repo.rb', line 11

def initialize(entities_repo)
  @entities_repo = entities_repo
  @collections_by_type = {}
  @items_by_id = {}
  @item_type_methods = {}

  build_cache!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object (private)



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/dato/local/items_repo.rb', line 146

def method_missing(method, *arguments, &block)
  if collections_by_type.key?(method) && arguments.empty?
    collections_by_type[method]
  else
    super
  end
rescue NoMethodError
  message = []
  message << "Undefined method `#{method}`"
  message << 'Available DatoCMS collections/items:'
  message += collections_by_type.map do |key, _value|
    "* .#{key}"
  end
  raise NoMethodError, message.join("\n")
end

Instance Attribute Details

#collections_by_typeObject (readonly)

Returns the value of attribute collections_by_type.



9
10
11
# File 'lib/dato/local/items_repo.rb', line 9

def collections_by_type
  @collections_by_type
end

#entities_repoObject (readonly)

Returns the value of attribute entities_repo.



9
10
11
# File 'lib/dato/local/items_repo.rb', line 9

def entities_repo
  @entities_repo
end

#item_type_methodsObject (readonly)

Returns the value of attribute item_type_methods.



9
10
11
# File 'lib/dato/local/items_repo.rb', line 9

def item_type_methods
  @item_type_methods
end

Instance Method Details

#available_localesObject



39
40
41
# File 'lib/dato/local/items_repo.rb', line 39

def available_locales
  site.locales.map(&:to_sym)
end

#collection_item_typesObject



51
52
53
# File 'lib/dato/local/items_repo.rb', line 51

def collection_item_types
  item_types - single_instance_item_types
end

#find(id) ⇒ Object



20
21
22
# File 'lib/dato/local/items_repo.rb', line 20

def find(id)
  @items_by_id[id.to_s]
end

#item_typesObject



43
44
45
# File 'lib/dato/local/items_repo.rb', line 43

def item_types
  entities_repo.find_entities_of_type('item_type')
end

#items_of_type(item_type) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/dato/local/items_repo.rb', line 55

def items_of_type(item_type)
  method = item_type_methods[item_type]

  if item_type.singleton
    Array(@collections_by_type[method])
  else
    @collections_by_type[method]
  end
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
# File 'lib/dato/local/items_repo.rb', line 24

def respond_to_missing?(method, include_private = false)
  if collections_by_type.key?(method)
    true
  else
    super
  end
end

#single_instance_item_typesObject



47
48
49
# File 'lib/dato/local/items_repo.rb', line 47

def single_instance_item_types
  item_types.select(&:singleton)
end

#siteObject



32
33
34
35
36
37
# File 'lib/dato/local/items_repo.rb', line 32

def site
  Site.new(
    entities_repo.find_entities_of_type('site').first,
    self
  )
end