Class: Venduitz::View
- Inherits:
-
Object
- Object
- Venduitz::View
- Defined in:
- lib/venduitz/view.rb
Class Attribute Summary collapse
-
.collects ⇒ Object
readonly
Properties.
-
.props ⇒ Object
readonly
Properties.
Class Method Summary collapse
-
.collection(name, view, opts = {}) ⇒ Object
Define a collection for it.
-
.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.
-
.prop(name, prc = nil) ⇒ Object
Define a property for the view.
-
.to_json(obj, excluded = [], cache = Cache.enabled, cache_options = Cache.config) ⇒ Hash
Parse it.
Class Attribute Details
.collects ⇒ Object (readonly)
Properties
8 9 10 |
# File 'lib/venduitz/view.rb', line 8 def collects @collects end |
.props ⇒ Object (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
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.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, ) if cache # Return the result result end |
.prop(name, prc = nil) ⇒ Object
Define a property for the view
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
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.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, ) if cache # Return the result result end |