Module: SkullIsland::Helpers::ResourceClass

Included in:
Resource
Defined in:
lib/skull_island/helpers/resource_class.rb

Overview

Simple helper class methods for Resource

Instance Method Summary collapse

Instance Method Details

#determine_getter_names(original_name, opts) ⇒ Array<Symbol>

Determine a list of names to use to access a resource entity attribute

Parameters:

  • original_name (String, Symbol)

    the name of the underlying attribute

  • opts (Hash)

    property options as defined in a SkullIsland::Helpers::Resource subclass

Returns:

  • (Array<Symbol>)

    the list of names



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/skull_island/helpers/resource_class.rb', line 11

def determine_getter_names(original_name, opts)
  names = []
  names << original_name
  names << "#{original_name}?" if opts[:type] == :boolean
  if opts[:as]
    Array(opts[:as]).each do |new_name|
      names << (opts[:type] == :boolean ? "#{new_name}?" : new_name)
    end
  end
  names.map(&:to_sym).uniq
end

#determine_setter_names(original_name, opts) ⇒ Array<Symbol>

Determine a list of names to use to set a resource entity attribute

Parameters:

  • original_name (String, Symbol)

    the name of the underlying attribute

  • opts (Hash)

    property options as defined in a SkullIsland::Helpers::Resource subclass

Returns:

  • (Array<Symbol>)

    the list of names



27
28
29
30
31
# File 'lib/skull_island/helpers/resource_class.rb', line 27

def determine_setter_names(original_name, opts)
  names = ["#{original_name}="]
  names.concat(Array(opts[:as]).map { |new_name| "#{new_name}=" }) if opts[:as]
  names.map(&:to_sym).uniq
end

#humanString

Note:

ActiveRecord ActiveModel::Name compatibility method

Produce a more human-readable representation of #i18n_key

Returns:

  • (String)


36
37
38
# File 'lib/skull_island/helpers/resource_class.rb', line 36

def human
  i18n_key.humanize
end

#i18n_keyString Also known as: singular_route_key

Note:

ActiveRecord ActiveModel::Name compatibility method

A mock internationalization key based on the class name

Returns:

  • (String)


48
49
50
# File 'lib/skull_island/helpers/resource_class.rb', line 48

def i18n_key
  name.split('::').last.to_underscore
end

#immutable?Boolean

Check if a resource class is immutable

Returns:

  • (Boolean)


41
42
43
# File 'lib/skull_island/helpers/resource_class.rb', line 41

def immutable?
  @immutable ||= false
end

#param_keySymbol

Note:

ActiveRecord ActiveModel::Name compatibility method

A symbolized version of #i18n_key

Returns:

  • (Symbol)


57
58
59
# File 'lib/skull_island/helpers/resource_class.rb', line 57

def param_key
  i18n_key.to_sym
end

#propertiesHash{Symbol => Hash}

All the properties defined for this Resource class

Returns:

  • (Hash{Symbol => Hash})


63
64
65
# File 'lib/skull_island/helpers/resource_class.rb', line 63

def properties
  @properties ||= {}
end

#route_keyString

Note:

ActiveRecord ActiveModel::Name compatibility method

A route key for building URLs

Returns:

  • (String)


70
71
72
# File 'lib/skull_island/helpers/resource_class.rb', line 70

def route_key
  i18n_key.en.plural
end