Class: Dryml::DrymlGenerator
- Inherits:
-
Object
- Object
- Dryml::DrymlGenerator
- Defined in:
- lib/dryml/dryml_generator.rb
Constant Summary collapse
- HEADER =
"<!-- AUTOMATICALLY GENERATED FILE - DO NOT EDIT -->\n\n"
Class Attribute Summary collapse
-
.output_directory ⇒ Object
Returns the value of attribute output_directory.
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
— Helper methods for the templates — #.
-
#subsite ⇒ Object
Returns the value of attribute subsite.
Class Method Summary collapse
- .enable(generator_directories = [], output_directory = nil) ⇒ Object
- .run(generator_directories = nil, output_directory = nil) ⇒ Object
Instance Method Summary collapse
- #a_or_an(word) ⇒ Object
- #controllers ⇒ Object
- #creator_names ⇒ Object
- #creators ⇒ Object
- #digest(s) ⇒ Object
- #dryml_changed?(name, dryml) ⇒ Boolean
- #each_controller ⇒ Object
- #each_model ⇒ Object
-
#initialize(generator_directories = nil) ⇒ DrymlGenerator
constructor
A new instance of DrymlGenerator.
- #load_templates(generator_directories) ⇒ Object
- #model ⇒ Object
- #model_class ⇒ Object
-
#model_name(*options) ⇒ Object
This method is used only to generate static default Model names in english only it does not provide any translation Localized apps will override the defaults by setting the specific keys in their locale files.
- #models ⇒ Object
- #output_dir(s = subsite) ⇒ Object
- #run ⇒ Object
- #run_for_subsite(subsite) ⇒ Object
- #run_one(name, src) ⇒ Object
- #sortable_collection?(collection, model = self.model) ⇒ Boolean
-
#sq_escape(s) ⇒ Object
escape single quotes and backslashes for use in a single quoted string.
- #standard_fields(*args) ⇒ Object
- #through_collection_names(klass = model) ⇒ Object
- #transition_names ⇒ Object
- #transitions ⇒ Object
- #view_hints ⇒ Object
Constructor Details
#initialize(generator_directories = nil) ⇒ DrymlGenerator
Returns a new instance of DrymlGenerator.
29 30 31 32 33 34 |
# File 'lib/dryml/dryml_generator.rb', line 29 def initialize(generator_directories=nil) @templates = {} @digests = {} generator_directories ||= Dryml::DrymlGenerator.generator_directories load_templates(generator_directories) end |
Class Attribute Details
.output_directory ⇒ Object
Returns the value of attribute output_directory.
12 13 14 |
# File 'lib/dryml/dryml_generator.rb', line 12 def output_directory @output_directory end |
Instance Attribute Details
#controller ⇒ Object (readonly)
— Helper methods for the templates — #
109 110 111 |
# File 'lib/dryml/dryml_generator.rb', line 109 def controller @controller end |
#subsite ⇒ Object
Returns the value of attribute subsite.
36 37 38 |
# File 'lib/dryml/dryml_generator.rb', line 36 def subsite @subsite end |
Class Method Details
.enable(generator_directories = [], output_directory = nil) ⇒ Object
15 16 17 18 19 |
# File 'lib/dryml/dryml_generator.rb', line 15 def self.enable(generator_directories = [], output_directory = nil) @output_directory = output_directory @output_directory ||= "#{Rails.root}/app/views/taglibs/auto" if Object.const_defined?(:Rails) @generator_directories = generator_directories end |
.run(generator_directories = nil, output_directory = nil) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/dryml/dryml_generator.rb', line 21 def self.run(generator_directories=nil, output_directory=nil) @generator_directories ||= generator_directories @output_directory ||= output_directory @generator ||= new(generator_directories || @generator_directories) @generator.run end |
Instance Method Details
#a_or_an(word) ⇒ Object
229 230 231 |
# File 'lib/dryml/dryml_generator.rb', line 229 def a_or_an(word) (word =~ /^[aeiou]/i ? "an " : "a ") + word end |
#controllers ⇒ Object
112 113 114 |
# File 'lib/dryml/dryml_generator.rb', line 112 def controllers Hobo::Controller::Model.all_controllers(subsite).sort_by &:name end |
#creator_names ⇒ Object
220 221 222 |
# File 'lib/dryml/dryml_generator.rb', line 220 def creator_names creators.map { |c| c.name.to_s } end |
#creators ⇒ Object
212 213 214 |
# File 'lib/dryml/dryml_generator.rb', line 212 def creators defined?(model::Lifecycle) ? model::Lifecycle.publishable_creators : [] end |
#digest(s) ⇒ Object
102 103 104 |
# File 'lib/dryml/dryml_generator.rb', line 102 def digest(s) OpenSSL::Digest::SHA1.hexdigest(s) end |
#dryml_changed?(name, dryml) ⇒ Boolean
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/dryml/dryml_generator.rb', line 90 def dryml_changed?(name, dryml) key = "#{subsite}/#{name}" d = digest dryml if d != @digests[key] @digests[key] = d true else false end end |
#each_controller ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/dryml/dryml_generator.rb', line 121 def each_controller controllers.each do |controller| @controller = controller yield end @controller = nil end |
#each_model ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/dryml/dryml_generator.rb', line 130 def each_model models.each do |model| @model = model yield end @model = nil end |
#load_templates(generator_directories) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/dryml/dryml_generator.rb', line 39 def load_templates(generator_directories) generator_directories.each do |path| dir = path.to_s Dir.glob("#{dir}/**/*.dryml.erb").each do |f| name = f[dir.length + 1..-11] erb = File.read(f) @templates[name] = ERB.new(erb, nil, '-').src # Create output directories and parents as required [nil, *Hobo.subsites].each do |s| FileUtils.mkdir_p(File.dirname("#{output_dir s}/#{name}")) end end end end |
#model ⇒ Object
139 140 141 |
# File 'lib/dryml/dryml_generator.rb', line 139 def model @model || @controller.model end |
#model_class ⇒ Object
160 161 162 |
# File 'lib/dryml/dryml_generator.rb', line 160 def model_class model.name.underscore.gsub('_', '-').gsub('/', '--') end |
#model_name(*options) ⇒ Object
This method is used only to generate static default Model names in english only it does not provide any translation Localized apps will override the defaults by setting the specific keys in their locale files
146 147 148 149 150 151 |
# File 'lib/dryml/dryml_generator.rb', line 146 def model_name(*) name = :plural.in?() ? model.model_name.human(:count=>2, :locale=>:en) : model.model_name.human(:locale=>:en) name = name.titleize.downcase if :lowercase.in?() name = name.camelize if :camel.in?() name end |
#models ⇒ Object
117 118 119 |
# File 'lib/dryml/dryml_generator.rb', line 117 def models Hobo::Model.all_models.sort_by &:name end |
#output_dir(s = subsite) ⇒ Object
76 77 78 |
# File 'lib/dryml/dryml_generator.rb', line 76 def output_dir(s=subsite) s ? "#{Dryml::DrymlGenerator.output_directory}/#{s}" : Dryml::DrymlGenerator.output_directory end |
#run ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dryml/dryml_generator.rb', line 55 def run # FIXME # Ensure all view hints loaded before running subsites = [nil] if defined?(:Hobo) Hobo::Model.all_models.*.view_hints subsites += [*Hobo.subsites] end subsites.each { |s| run_for_subsite(s) } end |
#run_for_subsite(subsite) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/dryml/dryml_generator.rb', line 68 def run_for_subsite(subsite) self.subsite = subsite @templates.each_pair do |name, src| run_one(name, src) end end |
#run_one(name, src) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/dryml/dryml_generator.rb', line 81 def run_one(name, src) dryml = instance_eval(src, name) if dryml_changed?(name, dryml) out = HEADER + dryml File.open("#{output_dir}/#{name}.dryml", 'w') { |f| f.write(out) } end end |
#sortable_collection?(collection, model = self.model) ⇒ Boolean
177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/dryml/dryml_generator.rb', line 177 def sortable_collection?(collection, model=self.model) # There's no perfect way to detect for this, given that acts_as_list # does not provide any metadata to reflect on, but if the :order # option is the same as the target classes position_column, that's a # pretty safe bet if defined? ActiveRecord::Acts::List::InstanceMethods refl = model.reflections[collection] klass = refl.klass klass < ActiveRecord::Acts::List::InstanceMethods && klass.table_exists? && klass.new.position_column == refl.[:order].to_s end end |
#sq_escape(s) ⇒ Object
escape single quotes and backslashes for use in a single quoted string
155 156 157 |
# File 'lib/dryml/dryml_generator.rb', line 155 def sq_escape(s) s.gsub(/[\\]/, "\\\\\\\\").gsub(/'/, "\\\\'") end |
#standard_fields(*args) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/dryml/dryml_generator.rb', line 192 def standard_fields(*args) klass = args.first.is_a?(Class) ? args.shift : model extras = args fields = klass.attr_order.*.to_s & (klass.table_exists? ? klass.content_columns.*.name : []) fields -= %w{created_at updated_at created_on updated_on deleted_at} unless extras.include?(:include_timestamps) bt = extras.include?(:belongs_to) hm = extras.include?(:has_many) klass.reflections.values.sort_by { |refl| refl.name.to_s }.map do |refl| fields << refl.name.to_s if bt && refl.macro == :belongs_to fields << refl.name.to_s if hm && refl.macro == :has_many && refl.[:accessible] end fields.reject! { |f| model.never_show? f } fields end |
#through_collection_names(klass = model) ⇒ Object
170 171 172 173 174 |
# File 'lib/dryml/dryml_generator.rb', line 170 def through_collection_names(klass=model) klass.reflections.values.select do |refl| refl.macro == :has_many && refl.[:through] end.map {|x| x.[:through]} end |
#transition_names ⇒ Object
224 225 226 |
# File 'lib/dryml/dryml_generator.rb', line 224 def transition_names transitions.map { |t| t.name.to_s }.uniq end |
#transitions ⇒ Object
216 217 218 |
# File 'lib/dryml/dryml_generator.rb', line 216 def transitions defined?(model::Lifecycle) ? model::Lifecycle.publishable_transitions : [] end |
#view_hints ⇒ Object
165 166 167 |
# File 'lib/dryml/dryml_generator.rb', line 165 def view_hints model.view_hints end |