Sha256: aad8ae70436ded1474f307c75c3d1f2799a9634b09ea7ae3d7057880ccd29802

Contents?: true

Size: 1.16 KB

Versions: 9

Compression:

Stored size: 1.16 KB

Contents

class Guide::ViewModel < OpenStruct
  def initialize(defaults = {}, overrides = {})
    inappropriate_overrides = overrides.keys - defaults.keys
    if inappropriate_overrides.any?
      raise Guide::Errors::InterfaceViolation.new(
        "You added the #{'method'.pluralize(inappropriate_overrides.size)} "\
        "[#{inappropriate_overrides.join(", ")}] to the #{self.class.name} "\
        "in your scenario that #{'is'.pluralize(inappropriate_overrides.size)} "\
        "not included in its official declaration "\
        "(maybe in the `view_model` method on your Structure)"
      )
    end

    @guide_view_model_interface_methods = defaults.keys

    super(defaults.merge(overrides))
  end

  def method_missing(method, *args, &block)
    if respond_to?(method)
      super
    else
      raise Guide::Errors::InterfaceViolation.new(
        "You called a method '#{method}' from your template, "\
        "but it does not exist on the #{self.class.name} in your Structure."
      )
    end
  end

  def guide_view_model_interface_methods
    @guide_view_model_interface_methods
  end

  def to_ary
    nil # because Cells calls ViewModel#flatten for some reason
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
guide-0.8.0 app/models/guide/view_model.rb
guide-0.7.0 app/models/guide/view_model.rb
guide-0.6.1 app/models/guide/view_model.rb
guide-0.6.0 app/models/guide/view_model.rb
guide-0.5.0 app/models/guide/view_model.rb
guide-0.4.1 app/models/guide/view_model.rb
guide-0.4.0 app/models/guide/view_model.rb
guide-0.3.2 app/models/guide/view_model.rb
guide-0.3.1 app/models/guide/view_model.rb