Class: Venduitz::View

Inherits:
Object
  • Object
show all
Defined in:
lib/venduitz/view.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.collectsObject (readonly)

Properties



8
9
10
# File 'lib/venduitz/view.rb', line 8

def collects
  @collects
end

.propsObject (readonly)

Properties



8
9
10
# File 'lib/venduitz/view.rb', line 8

def props
  @props
end

Class Method Details

.collection(name, view, opts = {}) ⇒ Object

Define a collection for it



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/venduitz/view.rb', line 23

def collection(name, view, opts = {})
  # Initialize it if its necessary
  @collects = {} if @collects.nil?

  # Define the collection
  @collects[name] = {
    view: view,
    value: (opts.key?(:value) ? opts[:value] : name),
    excluded: (opts.key?(:exclude) ? opts[:exclude] : [])
  }
end

.generate(obj, excluded = [], cache = Cache.enabled, cache_options = Cache.config) ⇒ Hash

This method will generate the hash object based on the properties and the colletions for the specific argument

Parameters:

  • obj (Object)

    This must contain the declared properies and collections

  • excluded (Array) (defaults to: [])

    This array contain the keys that will not be added to the result

  • cache (Boolean) (defaults to: Cache.enabled)

    This flag indicates if cache will be performed

  • cache_options (Hash) (defaults to: Cache.config)

    Cache options

Returns:

  • (Hash)

    JSON ready hash containing the specified view fields



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/venduitz/view.rb', line 43

def generate(obj, excluded = [], cache = Cache.enabled, cache_options = Cache.config)
  # Generate the cache key if cache is enabled
  key = cache_key(obj, excluded, :generate) if cache

  # Cache?
  return Cache.get(key) if cache && Cache.exist?(key)

  # Reset the values to prevent errors
  @props = {} if @props.nil?
  @collects = {} if @collects.nil?

  # Map both props and collects
  props = @props.select { |prop, value| !excluded.include?(prop) }
  coll = @collects.select { |prop, value| !excluded.include?(prop) }

  # Return the properties
  props = props.map do |prop, value|
    next [prop, obj.send(value)] if value.is_a?(Symbol)
    [prop, value.call(obj)]
  end

  # Return the collections
  coll = coll.map do |collect, info|
    values = info[:value].is_a?(Symbol) ? obj.send(info[:value]) : info[:value].call(obj)
    next [collect, []] if values.nil? || values.empty?
    [collect, values.map {|val| info[:view].generate(val, info[:excluded]) }]
  end

  # Full hash
  result = Hash[props].merge(Hash[coll])

  # Check if cache has to be created
  Cache.set(key, result, cache_options) if cache

  # Return the result
  result
end

.prop(name, prc = nil) ⇒ Object

Define a property for the view

Parameters:

  • name (Symbol/String)

    Name used as the JSON field

  • prc (Proc) (defaults to: nil)

    If its necessary pass a Proc to generate the value used by the generator



14
15
16
17
18
19
20
# File 'lib/venduitz/view.rb', line 14

def prop(name, prc = nil)
  # Initialize it if its necessary
  @props = {} if @props.nil?

  # Define!
  @props[name] = prc.nil? ? name : prc
end

.to_json(obj, excluded = [], cache = Cache.enabled, cache_options = Cache.config) ⇒ Hash

Parse it

Parameters:

  • obj (Object)

    This must contain the declared properies and collections

  • excluded (Array) (defaults to: [])

    This array contain the keys that will not be added to the result

  • cache (Boolean) (defaults to: Cache.enabled)

    This flag indicates if cache will be performed

  • cache_options (Hash) (defaults to: Cache.config)

    Cache options

Returns:

  • (Hash)

    The JSON string itself



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/venduitz/view.rb', line 87

def to_json(obj, excluded = [], cache = Cache.enabled, cache_options = Cache.config)
  # Generate the cache key if cache is enabled
  key = cache_key(obj, excluded, :json) if cache

  # Cache?
  return Cache.get(key) if cache && Cache.exist?(key)

  # Transform the result into json
  result = MultiJson.dump generate(obj, excluded)

  # Cache must be performed?
  Cache.set(key, result, cache_options) if cache

  # Return the result
  result
end