Class: TrueWeb::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/true-web/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Service

Returns a new instance of Service.



6
7
8
# File 'lib/true-web/service.rb', line 6

def initialize(dir)
  @root_dir = dir
end

Instance Attribute Details

#root_dirObject (readonly)

Returns the value of attribute root_dir.



3
4
5
# File 'lib/true-web/service.rb', line 3

def root_dir
  @root_dir
end

#url_prefixObject

Returns the value of attribute url_prefix.



4
5
6
# File 'lib/true-web/service.rb', line 4

def url_prefix
  @url_prefix
end

Instance Method Details

#app_dirObject



65
66
67
# File 'lib/true-web/service.rb', line 65

def app_dir
  File.join(root_dir, "app")
end

#append_load_pathsObject



19
20
21
22
# File 'lib/true-web/service.rb', line 19

def append_load_paths
  lib_dir = File.join(root_dir, "lib")
  $LOAD_PATH << lib_dir if File.directory?(lib_dir)
end

#autoload_modelsObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/true-web/service.rb', line 126

def autoload_models
  models_dir = File.join(root_dir, "app/models")
  if File.directory?(models_dir)
    directory_first_sort = TrueWeb::Plugins::DirectoryFirstSort.directory_first_sort
    Dir["#{models_dir}/**/*.rb"].sort_by(&directory_first_sort).each do |file|
      const_name_parts = file.
        gsub(models_dir, "").
        gsub("-", "_").
        gsub(/\.rb/, "").
        camelize.
        split("::").
        select {|part| part.present?}
      parent_const = const_name_parts[0..-2].join("::").constantize
      parent_const.autoload const_name_parts[-1].to_sym, file
    end
  end
end

#get_presenter_file_path(url) ⇒ Object



24
25
26
27
28
# File 'lib/true-web/service.rb', line 24

def get_presenter_file_path(url)
  stripped_url_prefix = strip_url_prefix(url)
  file_path = File.join(presenters_dir, "#{stripped_url_prefix}.rb")
  File.file?(file_path) ? file_path : nil
end

#get_spec_file_path(url) ⇒ Object



58
59
60
61
62
63
# File 'lib/true-web/service.rb', line 58

def get_spec_file_path(url)
  specs_url_prefix = File.join(url_prefix.to_s, "specs")
  stripped_url_prefix = strip_url_prefix(url, specs_url_prefix)
  file_path = File.join(spec_javascripts_dir, stripped_url_prefix)
  file_path if File.file?(file_path)
end

#get_spec_urls(url) ⇒ Object



45
46
47
48
49
50
# File 'lib/true-web/service.rb', line 45

def get_spec_urls(url)
  specs_url_prefix = File.join(url_prefix.to_s, "specs")
  stripped_url_prefix = strip_url_prefix(url, specs_url_prefix)
  path = File.join(spec_javascripts_dir, stripped_url_prefix.to_s)
  get_specs_urls_from_path(path)
end

#get_specs_urls_from_path(path = spec_javascripts_dir) ⇒ Object



52
53
54
55
56
# File 'lib/true-web/service.rb', line 52

def get_specs_urls_from_path(path=spec_javascripts_dir)
  (Dir["#{path}/**/*_spec.js"] + Dir["#{path}.js"]).flatten.compact.map do |file|
    file.gsub(spec_javascripts_dir, File.join(url_prefix.to_s, "specs")).gsub("//", "/")
  end
end

#get_static_file_path(url) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/true-web/service.rb', line 36

def get_static_file_path(url)
  stripped_url_prefix = strip_url_prefix(url)
  static_file_dirs.each do |dir|
    file_path = File.join(dir, stripped_url_prefix)
    return file_path if File.file?(file_path)
  end
  nil
end

#get_stylesheet_file_path(url) ⇒ Object



30
31
32
33
34
# File 'lib/true-web/service.rb', line 30

def get_stylesheet_file_path(url)
  stripped_url_prefix = strip_url_prefix(url)
  file_path = File.join(app_dir, stripped_url_prefix.gsub(/\.css$/, ".sass"))
  File.file?(file_path) ? file_path : nil
end

#init {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
14
15
16
17
# File 'lib/true-web/service.rb', line 10

def init
  init_controllers
  autoload_models
  append_load_paths
  eval_init_rb
  yield(self) if block_given?
  self
end

#init_controllersObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/true-web/service.rb', line 111

def init_controllers
  controllers_dir = File.join(root_dir, "app/controllers")
  if File.directory?(controllers_dir)
    module_files = Dir["#{controllers_dir}/**/*.module.rb"]
    module_files.each do |file|
      require file
    end

    directory_first_sort = TrueWeb::Plugins::DirectoryFirstSort.directory_first_sort
    (Dir["#{controllers_dir}/**/*.rb"] - module_files).sort_by(&directory_first_sort).each do |file|
      require file
    end
  end
end

#javascript_urls(params = {}) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/true-web/service.rb', line 88

def javascript_urls(params={})
  return [] unless File.directory?(public_javascripts_dir)
  glob = params[:glob] || "**/*.js"
  cache_buster = params.has_key?(:cache_bust) ? params[:cache_bust] : true
  Dir["#{public_javascripts_dir}/#{glob}"].sort_by(&TrueWeb::Plugins::DirectoryFirstSort.directory_first_sort).map do |file|
    asset_url(file, file.gsub(public_javascripts_dir, "#{url_prefix}/javascripts"), cache_buster)
  end
end

#presenters_dirObject



69
70
71
# File 'lib/true-web/service.rb', line 69

def presenters_dir
  File.join(app_dir, "presenters")
end

#public_dirObject



144
145
146
# File 'lib/true-web/service.rb', line 144

def public_dir
  File.join(root_dir, "public")
end

#spec_javascripts_dirObject



107
108
109
# File 'lib/true-web/service.rb', line 107

def spec_javascripts_dir
  File.join(root_dir, "spec/javascripts")
end

#stylesheet_urls(params = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/true-web/service.rb', line 97

def stylesheet_urls(params = {})
  return [] unless File.directory?(stylesheets_dir)
  glob = params[:glob] || "**/[^_]*.css"
  cache_buster = params.has_key?(:cache_bust) ? params[:cache_bust] : true
  sass_glob = glob.gsub(/\.css$/, ".sass")
  Dir["#{stylesheets_dir}/#{sass_glob}"].sort_by(&TrueWeb::Plugins::DirectoryFirstSort.directory_first_sort).map do |file|
    asset_url(file, file.gsub(stylesheets_dir, "#{url_prefix}/stylesheets").gsub(/\.sass$/, ".css"), cache_buster)
  end
end

#stylesheets_dirObject



77
78
79
# File 'lib/true-web/service.rb', line 77

def stylesheets_dir
  File.join(app_dir, "stylesheets")
end

#templates_dirObject



73
74
75
# File 'lib/true-web/service.rb', line 73

def templates_dir
  File.join(app_dir, "templates")
end

#templates_hashObject



81
82
83
84
85
86
# File 'lib/true-web/service.rb', line 81

def templates_hash
  Dir["#{templates_dir}/**/*.*"].inject({}) do |memo, path|
    memo[path.gsub(templates_dir, url_prefix.to_s)] = File.read(path)
    memo
  end
end