Class: Yaks::DefaultPolicy
- Inherits:
-
Object
- Object
- Yaks::DefaultPolicy
- 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
- #derive_mapper_from_association(association) ⇒ Object
-
#derive_mapper_from_collection(collection) ⇒ Class
Derives a mapper from the given collection.
-
#derive_mapper_from_item(item) ⇒ Class
Derives a mapper from the given item.
-
#derive_mapper_from_object(model) ⇒ Class
Main point of entry for mapper derivation.
- #derive_rel_from_association(association) ⇒ String
-
#derive_type_from_collection(collection) ⇒ String|nil
Derive the mapper type name from a collection.
-
#derive_type_from_mapper_class(mapper_class) ⇒ String
Derive the a mapper type name.
- #expand_rel(relname) ⇒ String
-
#initialize(options = {}) ⇒ DefaultPolicy
constructor
A new instance of DefaultPolicy.
- #options ⇒ Object
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.
18 19 20 |
# File 'lib/yaks/default_policy.rb', line 18 def initialize( = {}) @options = DEFAULTS.merge() 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.
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.
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.
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
109 110 111 |
# File 'lib/yaks/default_policy.rb', line 109 def derive_rel_from_association(association) (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.
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.
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
115 116 117 |
# File 'lib/yaks/default_policy.rb', line 115 def (relname) URITemplate.new(@options[:rel_template]).(rel: relname) end |
#options ⇒ Object
15 16 17 |
# File 'lib/yaks/default_policy.rb', line 15 def @options end |