Module: Kashmir::Representable
- Defined in:
- lib/kashmir/representable.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #base_representation ⇒ Object
- #cache!(representation_definition, representation, level = 1) ⇒ Object
- #cache_black_list ⇒ Object
- #cacheable? ⇒ Boolean
- #parse_definition(representation_definition) ⇒ Object
- #represent(representation_definition = [], level = 1, skip_cache = false) ⇒ Object
- #represent_with(&block) ⇒ Object
Instance Method Details
#base_representation ⇒ Object
48 49 50 |
# File 'lib/kashmir/representable.rb', line 48 def base_representation self.class.definitions.values.select(&:is_base?).map(&:field) end |
#cache!(representation_definition, representation, level = 1) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/kashmir/representable.rb', line 29 def cache!(representation_definition, representation, level=1) return unless cacheable? (cache_black_list & representation_definition).each do |field_name| representation_definition = representation_definition - [ field_name ] representation.delete(field_name) end Kashmir::Caching.store_presenter(representation_definition, representation, self, level * 60) end |
#cache_black_list ⇒ Object
40 41 42 |
# File 'lib/kashmir/representable.rb', line 40 def cache_black_list self.class.definitions.values.reject(&:should_cache?).map(&:field) end |
#cacheable? ⇒ Boolean
44 45 46 |
# File 'lib/kashmir/representable.rb', line 44 def cacheable? respond_to?(:id) end |
#parse_definition(representation_definition) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/kashmir/representable.rb', line 57 def parse_definition(representation_definition) if representation_definition.is_a?(Symbol) [ representation_definition, [] ] elsif representation_definition.is_a?(Hash) [ representation_definition.keys.first, representation_definition.values.flatten ] end end |
#represent(representation_definition = [], level = 1, skip_cache = false) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/kashmir/representable.rb', line 4 def represent(representation_definition=[], level=1, skip_cache=false) if !skip_cache && cacheable? and cached_presenter = Kashmir::Caching.from_cache(representation_definition, self) return cached_presenter end representation = {} (representation_definition + base_representation).each do |representation_definition| key, arguments = parse_definition(representation_definition) unless self.class.definitions.keys.include?(key) raise "#{self.class.to_s}##{key} is not defined as a representation" end represented_document = self.class.definitions[key].run_for(self, arguments, level) representation = representation.merge(represented_document) end if !skip_cache cache!(representation_definition.dup, representation.dup, level) end representation end |