Module: RailsAssist::Artifact::Asset

Extended by:
FileName
Includes:
FileName, CRUD, BaseHelper
Defined in:
lib/rails_artifactor/artifact/asset_artifact.rb,
lib/rails_artifactor/artifact/file_name/asset.rb

Defined Under Namespace

Modules: FileName, SingleArg, TwoArgs

Constant Summary

Constants included from FileName

FileName::DIR

Instance Method Summary collapse

Methods included from FileName

asset_file_name, get_asset_args

Methods included from BaseHelper

included

Instance Method Details

#asset_file(*args) ⇒ Object



19
20
21
# File 'lib/rails_artifactor/artifact/asset_artifact.rb', line 19

def asset_file *args
  asset_file_name(args)
end

#create_asset(*args, &block) ⇒ Object

CREATE



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rails_artifactor/artifact/asset_artifact.rb', line 24

def create_asset *args, &block
  file_name = asset_file_name(args)
  dir = File.dirname(file_name)
  FileUtils.mkdir_p dir if !File.directory?(dir)

  content = get_asset_content(args) || yield if block

  # abort if no content given
  debug "Warning: Content must be passed in either as a :content hash or a block" if !content
  return nil if !content

  debug "Writing asset file: #{file_name}"
  # write file content of asset
  File.open(file_name, 'w') do |f|
    f.puts content
  end
end

#get_asset_content(args) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/rails_artifactor/artifact/asset_artifact.rb', line 109

def get_asset_content args
  args = args.flatten
  case args.first
  when Hash
    args.first[:content]
  end
end

#has_asset?(name, *args, &block) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
# File 'lib/rails_artifactor/artifact/asset_artifact.rb', line 6

def has_asset? name, *args, &block
  file_name = asset_file_name(name, args)
  file_name.path.file?
end

#has_assets?(*args, &block) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
# File 'lib/rails_artifactor/artifact/asset_artifact.rb', line 11

def has_assets? *args, &block
  options = last_option args
  args.to_strings.each do |name|
    return false if !has_asset? name, options
  end
  true
end

#insert_into_asset(*args, &block) ⇒ Object

UPDATE



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rails_artifactor/artifact/asset_artifact.rb', line 58

def insert_into_asset *args, &block
  begin
    file_name = asset_file_name(args)
    options = last_option args
    raise ArgumentError, ":before or :after option must be specified for insertion" if !(options[:before] || options[:after])
    File.insert_into file_name, options, &block
    true
  rescue
    nil
  end
end

#read_asset(*args, &block) ⇒ Object

READ



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rails_artifactor/artifact/asset_artifact.rb', line 43

def read_asset *args, &block
  file_name = asset_file_name(args)
  debug "reading from: #{file_name}"
  begin
    file = File.new(file_name)
    content = file.read
    debug "read content: #{content}"
    yield content if block
    content
  rescue
    nil
  end
end

#remove_asset(*args) ⇒ Object

DELETE



71
72
73
74
# File 'lib/rails_artifactor/artifact/asset_artifact.rb', line 71

def remove_asset *args
  file = asset_file_name(args)
  FileUtils.rm_f(file) if File.exist?(file)
end

#remove_assets(*args) ⇒ Object

remove_assets :edit, :show, :folder => :javascripts

Raises:

  • (ArgumentError)


77
78
79
80
81
# File 'lib/rails_artifactor/artifact/asset_artifact.rb', line 77

def remove_assets *args
  options = last_option args
  raise ArgumentError, "Missing :folder option in the last argument which must be a Hash" if !options && !options[:folder]
  args.to_symbols.each{|name| remove_asset name, options}
end