Module: Videojuicer::Resource::Inferrable::SingletonMethods

Defined in:
lib/videojuicer/resource/inferrable.rb

Instance Method Summary collapse

Instance Method Details

#base_path(options = {}) ⇒ Object

The root route for requests to the API. By default this is inferred from the plural name e.g. Videojuicer::Presentation uses /presentations as the resource_path.



127
128
129
130
131
132
133
134
# File 'lib/videojuicer/resource/inferrable.rb', line 127

def base_path(options={})
 options = {
 	:nested=>true
 }.merge(options)
  r = "/#{plural_name}"
  r = "#{self.containing_class.nesting_route rescue nil}#{r}" if options[:nested]
  return r
end

#compile_route(mask, properties = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/videojuicer/resource/inferrable.rb', line 49

def compile_route(mask, properties={})
  properties = properties.deep_stringify
  result = []
  mask.split("/").each do |member|
    if member[0..0] == ":"
      result << (properties[member.delete(":")])
    else
      result << member
    end            
  end
  "#{result.join("/")}"
end

#containing_classObject

Nested resources are inferred from the class hierarchy: Something.parent_class #=> nil Something::Else.parent_class #=> Something

Monkeys::Are::Delicious



66
67
68
69
# File 'lib/videojuicer/resource/inferrable.rb', line 66

def containing_class
  c = self.to_s.split("::"); c.pop
  (c.any?)? Object.full_const_get(c.join("::")) : nil
end

#nesting_routeObject

The route fragment under which nested resources should be mapped.



72
73
74
75
# File 'lib/videojuicer/resource/inferrable.rb', line 72

def nesting_route
  r = ["/#{plural_name}/#{nesting_route_key}"]
  r = ([(self.containing_class.nesting_route rescue nil)] + r).compact.join("")
end

#nesting_route_keyObject

The key used by other models when referring to this one in resource routes. User.nesting_route_key == “:user_id”



78
79
80
# File 'lib/videojuicer/resource/inferrable.rb', line 78

def nesting_route_key
  ":#{resource_name}_id"
end

#parameter_nameObject

The name used to send parameters to the API. For instance, if a model named Cake has an attribute is_lie, and the parameter name of Cake is “cake”, this attribute will be sent to the API as a parameter named cake with the appropriate value.



111
112
113
# File 'lib/videojuicer/resource/inferrable.rb', line 111

def parameter_name
  singular_name
end

#plural_nameObject

Returns the plural version of the underscored singular name. This method, when compared directly and fairly to something like ActiveSupport’s inflection module, is an idiot. You would be best to treat it as one.



95
96
97
98
99
100
101
# File 'lib/videojuicer/resource/inferrable.rb', line 95

def plural_name
  if singular_name.match(/y$/)
  	return singular_name.gsub(/y$/, "ies")
  end          
  # Fall back to the simplest rules
  (singular_name.match(/s$/))? "#{singular_name}es" : "#{singular_name}s"
end

#resource_nameObject

The name used to identify this resource to the API, and in responses from the API.



104
105
106
# File 'lib/videojuicer/resource/inferrable.rb', line 104

def resource_name
  singular_name
end

#resource_route(action = nil, route_options = {}) ⇒ Object

The path to this class’s resource given the desired action route. Returned as a mask with replaceable keys e.g. /fixed/fixed/:replaceme/fixed



117
118
119
120
121
122
123
# File 'lib/videojuicer/resource/inferrable.rb', line 117

def resource_route(action=nil, route_options={})
  id = route_options.delete(:id)
  action_stem = (id)? "/#{id}" : ""
  action_stem += (action)? "/#{action}" : ""
  action_stem += ".json" unless action_stem.empty?
  "#{base_path(route_options)}#{action_stem}"
end

#singular_nameObject

Returns the lowercased, underscored version of the including class name. e.g. Videojuicer::ExampleModel.singular_name => “example_model”



84
85
86
87
88
89
90
# File 'lib/videojuicer/resource/inferrable.rb', line 84

def singular_name
  @singular_name ||=  self.to_s.split("::").last.
                      gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
                      gsub(/([a-z\d])([A-Z])/,'\1_\2').
                      tr("-", "_").
                      downcase.snake_case
end