Sha256: f8a9b9e5fa643346d9a5c6800d94662a584d31435ce2b4835a39ea57b3bcd96f

Contents?: true

Size: 1.08 KB

Versions: 5

Compression:

Stored size: 1.08 KB

Contents

# A place for things shared between an ArrayModel and a Model
module ModelHelpers
  def deep_unwrap(value)
    if value.is_a?(Model)
      value = value.to_h
    elsif value.is_a?(ArrayModel)
      value = value.to_a
    end

    return value
  end

  # Pass to the persisotr
  def event_added(event, first, first_for_event)
    @persistor.event_added(event, first, first_for_event) if @persistor
  end

  # Pass to the persistor
  def event_removed(event, last, last_for_event)
    @persistor.event_removed(event, last, last_for_event) if @persistor
  end

  # Gets the class for a model at the specified path.
  def class_at_path(path)
    if path
      begin
        # remove the _ and then singularize
        if path.last == :[]
          index = -2
        else
          index = -1
        end

        klass_name = path[index].singularize.camelize

        klass = $page.model_classes[klass_name] || Model
      rescue NameError => e
        # Ignore exception, just means the model isn't defined
        klass = Model
      end
    else
      klass = Model
    end

    return klass
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

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