Class: Reality::List
- Inherits:
-
Array
- Object
- Array
- Reality::List
- Defined in:
- lib/reality/list.rb
Overview
List is subclass of Array, which allows effective entities storing and loading.
You can create list from entities or just their names:
Reality::List.new('Argentina', 'Ukraine')
# => #<Reality::List[Argentina?, Ukraine?]>
...or from existing entities:
Reality::List.new(Reality::Entity.new('Argentina'), Reality::Entity.new('Ukraine'))
# => #<Reality::List[Argentina?, Ukraine?]>
List is useful for compact inspect (see above), and for effective batch loading (see #load!).
Also, List is smart enough to remain List on multiple enumerable
methods like #select, #reject, #map and so on:
Reality::List.new('Argentina', 'Bolivia', 'Chile').sample(2)
# => #<Reality::List[Chile?, Argentina?]>
Reality::List.new('Argentina', 'Bolivia', 'Chile').load!.map(&:capital)
# => #<Reality::List[Buenos Aires?, La Paz?, Santiago?]>
Instance Method Summary collapse
-
#describe ⇒ nil
Prints compact description of the list.
-
#initialize(*names) ⇒ List
constructor
Creates List from a set of entity names or entity objects.
- #inspect ⇒ String
-
#load! ⇒ self
Loads all entities in batch.
Constructor Details
#initialize(*names) ⇒ List
Creates List from a set of entity names or entity objects.
Also aliased as Reality::List() method.
39 40 41 |
# File 'lib/reality/list.rb', line 39 def initialize(*names) super names.map(&method(:coerce)) end |
Instance Method Details
#describe ⇒ nil
Prints compact description of the list. Implicitly loads all list if not loaded.
Reality::List.new('Argentina', 'Bolivia', 'Chile').describe
# -------------------------
# #<Reality::List(3 items)>
# -------------------------
# keys: adm_divisions (3), area (3), calling_code (3), capital (3), continent (3), coord (3), country (3), created_at (3), currency (3), gdp_nominal (3), gdp_ppp (3), head_of_government (2), head_of_state (3), highest_point (3), iso2_code (3), iso3_code (3), long_name (3), neighbours (3), official_website (1), organizations (3), part_of (3), population (3), tld (3), tz_offset (3)
# types: country (3)
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/reality/list.rb', line 85 def describe load! unless all?(&:loaded?) = { types: map(&:wikipedia_type).compact.map(&:symbol). group_count.sort_by(&:first).map{|t,c| "#{t} (#{c})"}.join(', '), keys: map(&:values).map(&:keys).flatten. group_count.sort_by(&:first).map{|k,c| "#{k} (#{c})"}.join(', '), } # hard to read, yet informative version: #keys = map(&:values).map(&:to_a).flatten(1). #group_by(&:first).map{|key, vals| #values = vals.map(&:last) #[key, "(#{values.compact.count}) example: #{values.compact.first.inspect}"] #}.to_h puts Util::Format.describe("#<#{self.class.name}(#{count} items)>", ) end |
#inspect ⇒ String
68 69 70 |
# File 'lib/reality/list.rb', line 68 def inspect "#<#{self.class.name}[#{map{|e| e ? e.to_s? : e.inspect}.join(', ')}]>" end |
#load! ⇒ self
Loads all entities in batch. Optimized to make as few requests as possible. Typically, when you want to load several entities
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/reality/list.rb', line 47 def load! compact.partition(&:wikidata_id).tap{|wd, wp| load_by_wikipedia(wp) load_by_wikidata(wd) } # try to fallback to labels: compact.reject(&:loaded?).tap{|entities| load_by_wikidata_labels(entities) } self end |