class Dato::Local::ItemsRepo
Attributes
collections_by_type[R]
entities_repo[R]
item_type_methods[R]
Public Class Methods
new(entities_repo)
click to toggle source
# 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
Public Instance Methods
available_locales()
click to toggle source
# File lib/dato/local/items_repo.rb, line 45 def available_locales site.locales.map(&:to_sym) end
children_of(id)
click to toggle source
# File lib/dato/local/items_repo.rb, line 26 def children_of(id) @items_by_parent_id.fetch(id.to_s, []) end
collection_item_types()
click to toggle source
# 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)
click to toggle source
# File lib/dato/local/items_repo.rb, line 22 def find(id) @items_by_id[id.to_s] end
item_types()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
Calls superclass method
# 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_types()
click to toggle source
# File lib/dato/local/items_repo.rb, line 53 def single_instance_item_types item_types.select(&:singleton) end
site()
click to toggle source
# File lib/dato/local/items_repo.rb, line 38 def site Site.new( entities_repo.find_entities_of_type("site").first, self, ) end
Private Instance Methods
build_cache!()
click to toggle source
# File lib/dato/local/items_repo.rb, line 75 def build_cache! build_item_type_methods! build_collections_by_type! build_singletons_by_type! end
build_collections_by_type!()
click to toggle source
# File lib/dato/local/items_repo.rb, line 108 def build_collections_by_type! item_types.each do |item_type| method = item_type_methods[item_type] @collections_by_type[method] = if item_type.singleton nil else ItemCollection.new end end item_entities.each do |item_entity| item = Item.new(item_entity, self) method = item_type_methods[item_entity.item_type] @collections_by_type[method].push item unless item_entity.item_type.singleton @items_by_id[item.id] = item if item_entity.respond_to?(:parent_id) && item_entity.parent_id @items_by_parent_id[item_entity.parent_id] ||= [] @items_by_parent_id[item_entity.parent_id] << item end end item_types.each do |item_type| method = item_type_methods[item_type] if !item_type.singleton && item_type.sortable nil_items, valid_items = @collections_by_type[method].partition do |item| item.position.nil? end @collections_by_type[method] = valid_items.sort_by(&:position) + nil_items elsif item_type.ordering_field field = item_type.ordering_field.api_key nil_items, valid_items = @collections_by_type[method].partition do |item| item[field].nil? end @collections_by_type[method] = valid_items.sort_by { |item| item[field] } + nil_items @collections_by_type[method].reverse! if item_type.ordering_direction == "desc" end end end
build_item_type_methods!()
click to toggle source
# File lib/dato/local/items_repo.rb, line 81 def build_item_type_methods! @item_type_methods = {} singleton_keys = single_instance_item_types.map(&:api_key) collection_keys = collection_item_types.map(&:api_key) .map(&:pluralize) clashing_keys = singleton_keys & collection_keys item_types.each do |item_type| pluralized_api_key = item_type.api_key.pluralize method = if item_type.singleton item_type.api_key else pluralized_api_key end if clashing_keys.include?(pluralized_api_key) suffix = item_type.singleton ? "instance" : "collection" method = "#{method}_#{suffix}" end @item_type_methods[item_type] = method.to_sym end end
build_singletons_by_type!()
click to toggle source
# File lib/dato/local/items_repo.rb, line 150 def build_singletons_by_type! item_types.each do |item_type| method = item_type_methods[item_type] next unless item_type.singleton item = (@items_by_id[item_type.singleton_item.id] if item_type.singleton_item) @collections_by_type[method] = item end end
item_entities()
click to toggle source
# File lib/dato/local/items_repo.rb, line 161 def item_entities entities_repo.find_entities_of_type("item") end
method_missing(method, *arguments, &block)
click to toggle source
Calls superclass method
# File lib/dato/local/items_repo.rb, line 165 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