Class: BowerRails::Dsl

Inherits:
Object
  • Object
show all
Defined in:
lib/bower-rails/dsl.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDsl

Returns a new instance of Dsl.



13
14
15
16
17
# File 'lib/bower-rails/dsl.rb', line 13

def initialize
  @dependencies = {}
  @root_path ||= defined?(Rails) ? Rails.root : Dir.pwd
  @assets_path ||= "assets"
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



11
12
13
# File 'lib/bower-rails/dsl.rb', line 11

def dependencies
  @dependencies
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



11
12
13
# File 'lib/bower-rails/dsl.rb', line 11

def root_path
  @root_path
end

Class Method Details

.evalute(filename) ⇒ Object



7
8
9
# File 'lib/bower-rails/dsl.rb', line 7

def self.evalute(filename)
  new.tap { |dsl| dsl.eval_file(File.join(dsl.root_path, filename)) }
end

Instance Method Details

#asset(name, version = "latest") ⇒ Object



35
36
37
38
39
40
41
# File 'lib/bower-rails/dsl.rb', line 35

def asset(name, version = "latest")
  groups.each do |g|
    g_norm = normalize_location_path(g.first, group_assets_path(g))
    @dependencies[g_norm] ||= {}
    @dependencies[g_norm][name] = version
  end
end

#directoriesObject



23
24
25
# File 'lib/bower-rails/dsl.rb', line 23

def directories
  @dependencies.keys
end

#eval_file(file) ⇒ Object



19
20
21
# File 'lib/bower-rails/dsl.rb', line 19

def eval_file(file)
  instance_eval(File.open(file, "rb") { |f| f.read }, file.to_s)
end

#final_assets_pathObject



65
66
67
68
69
# File 'lib/bower-rails/dsl.rb', line 65

def final_assets_path
  groups.map do |g|
    [g.first.to_s, group_assets_path(g)]
  end
end

#group(name, options = {}, &block) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/bower-rails/dsl.rb', line 27

def group(name, options = {}, &block)
  if custom_assets_path = options[:assets_path]
    assert_asset_path custom_assets_path
  end
  add_group(name, options)
  yield if block_given?
end

#group_assets_path(group) ⇒ Object



71
72
73
74
# File 'lib/bower-rails/dsl.rb', line 71

def group_assets_path group
  group_options = Hash === group.last ? group.last : {:assets_path => @assets_path}
  group_options[:assets_path]
end

#to_json(location) ⇒ Object



43
44
45
# File 'lib/bower-rails/dsl.rb', line 43

def to_json(location)
  dependencies_to_json @dependencies[normalize_location_path(location)]
end

#write_bower_jsonObject



47
48
49
50
51
52
53
54
# File 'lib/bower-rails/dsl.rb', line 47

def write_bower_json
  @dependencies.each do |dir,data|
    FileUtils.mkdir_p dir unless File.directory? dir
    File.open(File.join(dir,"bower.json"), "w") do |f|
      f.write(dependencies_to_json(data))
    end
  end
end

#write_dotbowerrcObject



56
57
58
59
60
61
62
63
# File 'lib/bower-rails/dsl.rb', line 56

def write_dotbowerrc
  groups.map do |g|
    g_norm = normalize_location_path(g.first, group_assets_path(g))
    File.open(File.join(g_norm, ".bowerrc"), "w") do |f|
      f.write(JSON.pretty_generate({:directory => "bower_components/pt"}))
    end
  end
end