Class: Agave::Local::ItemsRepo

Inherits:
Object
  • Object
show all
Defined in:
lib/agave/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
19
# File 'lib/agave/local/items_repo.rb', line 11

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)



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/agave/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 AgaveCMS 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/agave/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/agave/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/agave/local/items_repo.rb', line 9

def item_type_methods
  @item_type_methods
end

Instance Method Details

#available_localesObject



44
45
46
# File 'lib/agave/local/items_repo.rb', line 44

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

#children_of(id) ⇒ Object



25
26
27
# File 'lib/agave/local/items_repo.rb', line 25

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

#collection_item_typesObject



56
57
58
59
60
# File 'lib/agave/local/items_repo.rb', line 56

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

#find(id) ⇒ Object



21
22
23
# File 'lib/agave/local/items_repo.rb', line 21

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

#item_typesObject



48
49
50
# File 'lib/agave/local/items_repo.rb', line 48

def item_types
  entities_repo.find_entities_of_type('item_type')
end

#items_of_type(item_type) ⇒ Object



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

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)


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

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

#single_instance_item_typesObject



52
53
54
# File 'lib/agave/local/items_repo.rb', line 52

def single_instance_item_types
  item_types.select(&:singleton)
end

#siteObject



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

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