Class: Uchi::View

Inherits:
Object
  • Object
show all
Defined in:
lib/uchi/view.rb,
lib/uchi/view/constants.rb

Overview

Represents a view/action a field or resource can appear on, e.g. :index, :show, :new, :edit.

Uchi::View behaves like the underlying Symbol for equality, hashing and array membership.

Examples:

Uchi::View.new(:show) == :show # => true

[Uchi::View.new(:edit)].include?(:edit) # => true

Constant Summary collapse

NAMES =
[:edit, :index, :new, :show].freeze
EDIT =

Define some commonly used views as constants for convenience.

new(:edit).freeze
INDEX =
new(:index).freeze
NEW =
new(:new).freeze
SHOW =
new(:show).freeze
ALL =

Shorthand for all views.

[EDIT, INDEX, NEW, SHOW].freeze
FORM =

Shorthand for views with a form

[EDIT, NEW].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ View

Returns a new instance of View.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
# File 'lib/uchi/view.rb', line 17

def initialize(name)
  raise ArgumentError, "Unsupported view: #{name.inspect}" unless name.respond_to?(:to_sym)

  @name = name.is_a?(View) ? name.name : name.to_sym
  raise ArgumentError, "unknown view: #{@name.inspect}" unless NAMES.include?(@name)
end

Instance Attribute Details

#name ⇒ Object (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/uchi/view.rb', line 15

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



24
25
26
27
28
29
# File 'lib/uchi/view.rb', line 24

def ==(other)
  return name == other if other.is_a?(Symbol)
  return name == other.name if other.is_a?(self.class)

  false
end

#edit? ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/uchi/view.rb', line 48

def edit?
  name == :edit
end

#hash ⇒ Object



32
33
34
# File 'lib/uchi/view.rb', line 32

def hash
  name.hash
end

#index? ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/uchi/view.rb', line 52

def index?
  name == :index
end

#inspect ⇒ Object



36
37
38
# File 'lib/uchi/view.rb', line 36

def inspect
  "#<Uchi::View #{name.inspect}>"
end

#new? ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/uchi/view.rb', line 56

def new?
  name == :new
end

#show? ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/uchi/view.rb', line 60

def show?
  name == :show
end

#to_s ⇒ Object



40
41
42
# File 'lib/uchi/view.rb', line 40

def to_s
  name.to_s
end

#to_sym ⇒ Object



44
45
46
# File 'lib/uchi/view.rb', line 44

def to_sym
  name
end