Class: Kashmir::Representation

Inherits:
Object
  • Object
show all
Defined in:
lib/kashmir/representation.rb

Direct Known Subclasses

ActiveRecordRepresentation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, options) ⇒ Representation

Returns a new instance of Representation.



6
7
8
9
# File 'lib/kashmir/representation.rb', line 6

def initialize(field, options)
  @field = field
  @options = options
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



4
5
6
# File 'lib/kashmir/representation.rb', line 4

def field
  @field
end

Instance Method Details

#is_base?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/kashmir/representation.rb', line 29

def is_base?
  @options.has_key?(:is_base) and !!@options[:is_base]
end

#present_array(value, arguments, level = 1, skip_cache = false) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kashmir/representation.rb', line 60

def present_array(value, arguments, level=1, skip_cache=false)
  cached_presenters = Kashmir::Caching.bulk_from_cache(arguments, value)

  uncached = []
  value.zip(cached_presenters).each do |record, cached_presenter|
    if cached_presenter.nil?
      uncached << record
    end
  end

  uncached_representations = uncached.map do |element|
    if primitive?(element)
      element
    else
      present_value(element, arguments, level, true)
    end
  end

  if rep = uncached.first and rep.is_a?(Kashmir) and rep.cacheable?
    Kashmir::Caching.bulk_write(arguments, uncached_representations, uncached, level * 60)
  end

  cached_presenters.compact + uncached_representations
end

#present_hash(value, arguments, level = 1, skip_cache = false) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/kashmir/representation.rb', line 85

def present_hash(value, arguments, level=1, skip_cache=false)
  new_hash = {}
  value.each_pair do |key, value|
    args = if arguments.is_a?(Hash)
             arguments[key.to_sym]
           else
             arg = arguments.find do |arg|
               (arg.is_a?(Hash) && arg.has_key?(key.to_sym)) || arg == key.to_sym
             end
             if arg.is_a?(Hash)
               arg = arg[key.to_sym]
             end

             arg
           end
    new_hash[key] = primitive?(value) ? value : present_value(value, args || [], level, skip_cache)
  end
  new_hash
end

#present_value(value, arguments, level = 1, skip_cache = false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kashmir/representation.rb', line 41

def present_value(value, arguments, level=1, skip_cache=false)

  if value.is_a?(Kashmir)
    return value.represent(arguments, level + 1, skip_cache)
  end

  if value.is_a?(Hash)
    return present_hash(value, arguments, level + 1, skip_cache)
  end

  if value.is_a?(Array)
    return present_array(value, arguments, level + 1, skip_cache)
  end

  if value.respond_to?(:represent)
    return value.represent(arguments, skip_cache)
  end
end

#primitive?(field_value) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/kashmir/representation.rb', line 113

def primitive?(field_value)
  [Fixnum, String, Date, Time, TrueClass, FalseClass, Symbol].include?(field_value.class)
end

#read_value(instance, field) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/kashmir/representation.rb', line 105

def read_value(instance, field)
  if instance.respond_to?(field)
    instance.send(field)
  else
    instance.instance_variable_get("@#{field}")
  end
end

#run_for(instance, arguments, level = 1) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/kashmir/representation.rb', line 11

def run_for(instance, arguments, level=1)
  representation = {}
  instance_vars = instance.instance_variables

  value = read_value(instance, @field)
  if primitive?(value)
    representation[@field] = value
  else
    if value.is_a?(Hash)
      representation[@field] = new_hash
    else
      representation[@field] = present_value(value, arguments, level)
    end
  end

  representation
end

#should_cache?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/kashmir/representation.rb', line 33

def should_cache?
  if @options.has_key?(:cacheable)
    return !!@options[:cacheable]
  end

  true
end