Sha256: c5e7d5bb50582ad642505bb1cb78266c333903e37c4f8b7527ca553434c4b7d8

Contents?: true

Size: 1.15 KB

Versions: 17

Compression:

Stored size: 1.15 KB

Contents

module Volt
  # A place for things shared between an ArrayModel and a Model
  module ModelHelpers
    def deep_unwrap(value)
      if value.is_a?(Model)
        value.to_h
      elsif value.is_a?(ArrayModel)
        value.to_a
      else
        value
      end
    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

      klass
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
volt-0.8.27.beta3 lib/volt/models/model_helpers.rb
volt-0.8.27.beta2 lib/volt/models/model_helpers.rb
volt-0.8.27.beta1 lib/volt/models/model_helpers.rb
volt-0.8.26.beta1 lib/volt/models/model_helpers.rb
volt-0.8.26 lib/volt/models/model_helpers.rb
volt-0.8.24 lib/volt/models/model_helpers.rb
volt-0.8.23 lib/volt/models/model_helpers.rb
volt-0.8.22 lib/volt/models/model_helpers.rb
volt-0.8.22.beta2 lib/volt/models/model_helpers.rb
volt-0.8.22.beta1 lib/volt/models/model_helpers.rb
volt-0.8.21 lib/volt/models/model_helpers.rb
volt-0.8.20 lib/volt/models/model_helpers.rb
volt-0.8.19 lib/volt/models/model_helpers.rb
volt-0.8.18 lib/volt/models/model_helpers.rb
volt-0.8.17 lib/volt/models/model_helpers.rb
volt-0.8.16 lib/volt/models/model_helpers.rb
volt-0.8.15 lib/volt/models/model_helpers.rb