Class: Beds::Scaffold
- Inherits:
-
Object
- Object
- Beds::Scaffold
- Defined in:
- lib/beds.rb
Overview
Generates Scaffolds based off of DataMappr models.
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
Returns an array of generated routes.
-
#templates ⇒ Object
readonly
Returns a hash containing file_name => file_contents key pairs.
-
#views ⇒ Object
readonly
Returns an array of views and file names.
Instance Method Summary collapse
-
#generate ⇒ Object
Generate views and routes for models.
-
#initialize(opts = {:exclude => [], :only => nil}) ⇒ Scaffold
constructor
-
opts should be a hash * Defautly loads data for all models.
-
Constructor Details
#initialize(opts = {:exclude => [], :only => nil}) ⇒ Scaffold
-
opts should be a hash
-
Defautly loads data for all models.
Valid Options:
:only - An array that only generates views for the specified models.
:exclude - An array containing models that should not be generatd.
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/beds.rb', line 24 def initialize(opts = {:exclude => [], :only => nil}) @routes = {} @views = {} @models = [] @opts = opts DataMapper::Model.descendants.entries.each {|entry| @models << entry } @models.delete_if { |model| @opts[:exclude].include? model.name or @opts[:exclude].include? model.name.downcase } @models.select! { |model| @opts[:only].include? model.name or @opts[:only].include? model.name.downcase } unless @opts[:only].nil? load_templates end |
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns an array of generated routes
13 14 15 |
# File 'lib/beds.rb', line 13 def routes @routes end |
#templates ⇒ Object (readonly)
Returns a hash containing file_name => file_contents key pairs.
17 18 19 |
# File 'lib/beds.rb', line 17 def templates @templates end |
#views ⇒ Object (readonly)
Returns an array of views and file names.
15 16 17 |
# File 'lib/beds.rb', line 15 def views @views end |
Instance Method Details
#generate ⇒ Object
Generate views and routes for models. Will genrate the view and routes attributes.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/beds.rb', line 37 def generate result = Hash.new routes = Hash.new @templates[:static_routes].each { |key,value| next if @opts[:exclude].include? key.to_s; puts "Processing Route: #{key}"; @routes['default'] ||= String.new ; @routes['default'] << ERB.new(value,nil,"<>").result(binding) } if @opts[:only].nil? @templates[:static_erb].each { |key,value| next if @opts[:exclude].include? key.to_s; puts "Processing Template: #{key}";@views["#{key.to_s}.erb"]=ERB.new(value,nil,"<>").result(binding) } if @opts[:only].nil? @models.each { |entry| @model_name = entry.inspect @properties = entry.properties puts "Processing Model: #{@model_name}" @templates[:routes].each {|key,value| @routes[entry.to_s.downcase] ||= String.new @routes[entry.to_s.downcase] << ERB.new(value,nil,"<>").result(binding) } @templates[:erb].each { |key,value| @views["#{key.to_s}_#{@model_name.downcase}.erb"] = ERB.new(value,nil,"<>").result(binding) } } return true end |