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.



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

def initialize(entities_repo)
  @entities_repo = entities_repo
  @collections_by_type = {}
  @items_by_id = {}
  @items_by_parent_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)



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/dato/local/items_repo.rb', line 171

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.



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

def collections_by_type
  @collections_by_type
end

#entities_repoObject (readonly)

Returns the value of attribute entities_repo.



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

def entities_repo
  @entities_repo
end

#item_type_methodsObject (readonly)

Returns the value of attribute item_type_methods.



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

def item_type_methods
  @item_type_methods
end

Instance Method Details

#available_localesObject



45
46
47
# File 'lib/dato/local/items_repo.rb', line 45

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

#children_of(id) ⇒ Object



26
27
28
# File 'lib/dato/local/items_repo.rb', line 26

def children_of(id)
  @items_by_parent_id.fetch(id.to_s, [])
end

#collection_item_typesObject



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

def collection_item_types
  item_types.select do |item_type|
    !item_type.singleton && !item_type.modular_block
  end
end

#find(id) ⇒ Object



22
23
24
# File 'lib/dato/local/items_repo.rb', line 22

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

#item_typesObject



49
50
51
# File 'lib/dato/local/items_repo.rb', line 49

def item_types
  entities_repo.find_entities_of_type('item_type')
end

#items_of_type(item_type) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/dato/local/items_repo.rb', line 63

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)


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

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

#single_instance_item_typesObject



53
54
55
# File 'lib/dato/local/items_repo.rb', line 53

def single_instance_item_types
  item_types.select(&:singleton)
end

#siteObject



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

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