Sha256: 86b7317a55a72352e75541521b93b0a98e506f61dd0165fb27493085e5c652d9

Contents?: true

Size: 826 Bytes

Versions: 5

Compression:

Stored size: 826 Bytes

Contents

module ModelWrapper
  # For cretain values, we wrap them to make the behave as a
  # model.
  def wrap_value(value, lookup)
    if value.is_a?(Array)
      value = new_array_model(value, @options.merge(parent: self, path: path + lookup))
    elsif value.is_a?(Hash)
      value = new_model(value, @options.merge(parent: self, path: path + lookup))
    end

    return value
  end

  def wrap_values(values, lookup=[])
    if values.is_a?(Array)
      # Coming from an array
      values = values.map {|v| wrap_value(v,lookup + [:[]]) }
    elsif values.is_a?(Hash)
      pairs = values.map do |k,v|
        # TODO: We should be able to move wrapping into the method_missing on model
        path = lookup + [k.to_sym]

        [k, wrap_value(v,path)]
      end

      values = Hash[pairs]
    end

    return values
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
volt-0.8.14 lib/volt/models/model_wrapper.rb
volt-0.8.13 lib/volt/models/model_wrapper.rb
volt-0.8.11 lib/volt/models/model_wrapper.rb
volt-0.8.10 lib/volt/models/model_wrapper.rb
volt-0.8.9 lib/volt/models/model_wrapper.rb