Class: RailsLegacyMapper::Mapper::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_legacy_mapper/mapper.rb

Overview

:nodoc:

Direct Known Subclasses

SingletonResource

Constant Summary collapse

DEFAULT_ACTIONS =
:index, :create, :new, :edit, :show, :update, :destroy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entities, options, defaults) ⇒ Resource

Returns a new instance of Resource.



194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/rails_legacy_mapper/mapper.rb', line 194

def initialize(entities, options, defaults)
  @plural   ||= entities
  @singular ||= options[:singular] || plural.to_s.singularize
  @path_segment = options.delete(:as) || @plural

  @options = options
  @defaults = defaults

  arrange_actions
  add_default_actions
  set_allowed_actions
  set_prefixes
end

Instance Attribute Details

#collection_methodsObject (readonly)

Returns the value of attribute collection_methods.



189
190
191
# File 'lib/rails_legacy_mapper/mapper.rb', line 189

def collection_methods
  @collection_methods
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



192
193
194
# File 'lib/rails_legacy_mapper/mapper.rb', line 192

def defaults
  @defaults
end

#member_methodsObject (readonly)

Returns the value of attribute member_methods.



189
190
191
# File 'lib/rails_legacy_mapper/mapper.rb', line 189

def member_methods
  @member_methods
end

#name_prefixObject (readonly)

Returns the value of attribute name_prefix.



190
191
192
# File 'lib/rails_legacy_mapper/mapper.rb', line 190

def name_prefix
  @name_prefix
end

#new_methodsObject (readonly)

Returns the value of attribute new_methods.



189
190
191
# File 'lib/rails_legacy_mapper/mapper.rb', line 189

def new_methods
  @new_methods
end

#optionsObject (readonly)

Returns the value of attribute options.



192
193
194
# File 'lib/rails_legacy_mapper/mapper.rb', line 192

def options
  @options
end

#path_prefixObject (readonly)

Returns the value of attribute path_prefix.



190
191
192
# File 'lib/rails_legacy_mapper/mapper.rb', line 190

def path_prefix
  @path_prefix
end

#path_segmentObject (readonly)

Returns the value of attribute path_segment.



190
191
192
# File 'lib/rails_legacy_mapper/mapper.rb', line 190

def path_segment
  @path_segment
end

#pluralObject (readonly)

Returns the value of attribute plural.



191
192
193
# File 'lib/rails_legacy_mapper/mapper.rb', line 191

def plural
  @plural
end

#singularObject (readonly)

Returns the value of attribute singular.



191
192
193
# File 'lib/rails_legacy_mapper/mapper.rb', line 191

def singular
  @singular
end

Instance Method Details

#action_separatorObject



253
254
255
# File 'lib/rails_legacy_mapper/mapper.rb', line 253

def action_separator
  @action_separator ||= RailsLegacyMapper.resource_action_separator
end

#conditionsObject



219
220
221
# File 'lib/rails_legacy_mapper/mapper.rb', line 219

def conditions
  @conditions ||= @options[:conditions] || {}
end

#controllerObject



208
209
210
# File 'lib/rails_legacy_mapper/mapper.rb', line 208

def controller
  @controller ||= "#{options[:namespace]}#{(options[:controller] || plural).to_s}"
end

#has_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


261
262
263
# File 'lib/rails_legacy_mapper/mapper.rb', line 261

def has_action?(action)
  !DEFAULT_ACTIONS.include?(action) || action_allowed?(action)
end

#member_pathObject



237
238
239
# File 'lib/rails_legacy_mapper/mapper.rb', line 237

def member_path
  @member_path ||= "#{shallow_path_prefix}/#{path_segment}/:id"
end

#nesting_name_prefixObject



249
250
251
# File 'lib/rails_legacy_mapper/mapper.rb', line 249

def nesting_name_prefix
  "#{shallow_name_prefix}#{singular}_"
end

#nesting_path_prefixObject



241
242
243
# File 'lib/rails_legacy_mapper/mapper.rb', line 241

def nesting_path_prefix
  @nesting_path_prefix ||= "#{shallow_path_prefix}/#{path_segment}/:#{singular}_id"
end

#new_pathObject



227
228
229
230
231
# File 'lib/rails_legacy_mapper/mapper.rb', line 227

def new_path
  new_action   = self.options[:path_names][:new] if self.options[:path_names]
  new_action ||= self.defaults[:path_names][:new]
  @new_path  ||= "#{path}/#{new_action}"
end

#pathObject



223
224
225
# File 'lib/rails_legacy_mapper/mapper.rb', line 223

def path
  @path ||= "#{path_prefix}/#{path_segment}"
end

#requirements(with_id = false) ⇒ Object



212
213
214
215
216
217
# File 'lib/rails_legacy_mapper/mapper.rb', line 212

def requirements(with_id = false)
  @requirements   ||= @options[:requirements] || {}
  @id_requirement ||= { :id => @requirements.delete(:id) || /[^#{ActionDispatch::Routing::SEPARATORS.join}]+/ }

  with_id ? @requirements.merge(@id_requirement) : @requirements
end

#shallow_name_prefixObject



245
246
247
# File 'lib/rails_legacy_mapper/mapper.rb', line 245

def shallow_name_prefix
  @shallow_name_prefix ||= @options[:shallow] ? @options[:namespace].try(:gsub, /\//, '_') : name_prefix
end

#shallow_path_prefixObject



233
234
235
# File 'lib/rails_legacy_mapper/mapper.rb', line 233

def shallow_path_prefix
  @shallow_path_prefix ||= @options[:shallow] ? @options[:namespace].try(:sub, /\/$/, '') : path_prefix
end

#uncountable?Boolean

Returns:

  • (Boolean)


257
258
259
# File 'lib/rails_legacy_mapper/mapper.rb', line 257

def uncountable?
  @singular.to_s == @plural.to_s
end