Class: Yaks::DefaultPolicy

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/yaks/default_policy.rb

Constant Summary collapse

DEFAULTS =

Default policy options.

{
  rel_template: "rel:{rel}",
  namespace: Object,
  mapper_rules: {}
}

Instance Method Summary collapse

Methods included from Util

#Resolve, #camelize, #extract_options, #reject_keys, #slice_hash, #symbolize_keys, #underscore

Constructor Details

#initialize(options = {}) ⇒ DefaultPolicy

Returns a new instance of DefaultPolicy.

Parameters:

  • options (Hash) (defaults to: {})

    options



18
19
20
# File 'lib/yaks/default_policy.rb', line 18

def initialize(options = {})
  @options = DEFAULTS.merge(options)
end

Instance Method Details

#derive_mapper_from_association(association) ⇒ Object



103
104
105
# File 'lib/yaks/default_policy.rb', line 103

def derive_mapper_from_association(association)
  @options[:namespace].const_get("#{camelize(association.singular_name)}Mapper")
end

#derive_mapper_from_collection(collection) ⇒ Class

Derives a mapper from the given collection.

Parameters:

  • collection (Object)

Returns:

  • (Class)

    A mapper, typically a subclass of Yaks::Mapper



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/yaks/default_policy.rb', line 41

def derive_mapper_from_collection(collection)
  if m = collection.first
    name = "#{m.class.name.split('::').last}CollectionMapper"
    begin
      return @options[:namespace].const_get(name)
    rescue NameError               # rubocop:disable Lint/HandleExceptions
    end
  end
  begin
    return @options[:namespace].const_get(:CollectionMapper)
  rescue NameError                 # rubocop:disable Lint/HandleExceptions
  end
  CollectionMapper
end

#derive_mapper_from_item(item) ⇒ Class

Derives a mapper from the given item. This item should not be a collection.

Parameters:

  • item (Object)

Returns:

  • (Class)

    A mapper, typically a subclass of Yaks::Mapper

Raises:

  • (RuntimeError)

    only occurs when no mapper is found for the given item.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/yaks/default_policy.rb', line 63

def derive_mapper_from_item(item)
  klass = item.class
  namespaces = klass.name.split("::")[0...-1]
  begin
    return build_mapper_class(namespaces, klass)
  rescue NameError
    klass = next_class_for_lookup(item, namespaces, klass)
    retry if klass
  end
  raise_mapper_not_found(item)
end

#derive_mapper_from_object(model) ⇒ Class

Main point of entry for mapper derivation. Calls derive_mapper_from_collection or derive_mapper_from_item depending on the model.

Parameters:

  • model (Object)

Returns:

  • (Class)

    A mapper, typically a subclass of Yaks::Mapper

Raises:



30
31
32
33
34
35
# File 'lib/yaks/default_policy.rb', line 30

def derive_mapper_from_object(model)
  mapper = detect_configured_mapper_for(model)
  return mapper if mapper
  return derive_mapper_from_collection(model) if model.respond_to? :to_ary
  derive_mapper_from_item(model)
end

#derive_rel_from_association(association) ⇒ String

Parameters:

Returns:

  • (String)


109
110
111
# File 'lib/yaks/default_policy.rb', line 109

def derive_rel_from_association(association)
  expand_rel(association.name)
end

#derive_type_from_collection(collection) ⇒ String|nil

Derive the mapper type name from a collection

This inspects the first element of the collection, so it requires a collection with truthy elements. Will return ‘nil` if the collection has no truthy elements.

Parameters:

  • collection (#first)

Returns:

  • (String|nil)

Raises:

  • (NameError)


98
99
100
101
# File 'lib/yaks/default_policy.rb', line 98

def derive_type_from_collection(collection)
  return if collection.none?
  derive_type_from_mapper_class(derive_mapper_from_object(collection.first))
end

#derive_type_from_mapper_class(mapper_class) ⇒ String

Derive the a mapper type name

This returns the ‘system name’ for a mapper, e.g. ShowEventMapper => show_event.

Parameters:

  • mapper_class (Class)

Returns:

  • (String)


83
84
85
# File 'lib/yaks/default_policy.rb', line 83

def derive_type_from_mapper_class(mapper_class)
  underscore(mapper_class.name.split('::').last.sub(/Mapper$/, ''))
end

#expand_rel(relname) ⇒ String

Parameters:

  • relname (String)

Returns:

  • (String)


115
116
117
# File 'lib/yaks/default_policy.rb', line 115

def expand_rel(relname)
  URITemplate.new(@options[:rel_template]).expand(rel: relname)
end

#optionsObject



15
16
17
# File 'lib/yaks/default_policy.rb', line 15

def options
  @options
end