Module: Rails3::Assist::Artifact::View

Extended by:
FileName
Includes:
CRUD, FileName, BaseHelper
Defined in:
lib/rails3_artifactor/artifact/view_artifact.rb,
lib/rails3_artifactor/artifact/file_name/view.rb

Defined Under Namespace

Modules: FileName, SingleArg, TwoArgs

Constant Summary

Constants included from FileName

FileName::DIR

Instance Method Summary collapse

Methods included from FileName

get_view_args, view_file_name

Methods included from BaseHelper

included

Instance Method Details

#create_view(*args, &block) ⇒ Object

CREATE



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

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

  content = get_view_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 view file: #{file_name}"
  # write file content of view
  File.open(file_name, 'w') do |f|  
    f.puts content 
  end
end

#get_view_content(args) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/rails3_artifactor/artifact/view_artifact.rb', line 84

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

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

Returns:

  • (Boolean)


6
7
8
9
# File 'lib/rails3_artifactor/artifact/view_artifact.rb', line 6

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

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

Returns:

  • (Boolean)


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

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

#insert_into_view(*args, &block) ⇒ Object

UPDATE



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

def insert_into_view *args, &block
  begin
    file_name = view_file_name(args)
    debug "file insertion (view): #{file_name}"
    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_view(*args, &block) ⇒ Object

READ



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

def read_view *args, &block
  file_name = view_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_view(*args) ⇒ Object

DELETE



72
73
74
75
# File 'lib/rails3_artifactor/artifact/view_artifact.rb', line 72

def remove_view *args
  file = view_file_name(args)
  FileUtils.rm_f(file) if File.exist?(file)
end

#remove_views(*args) ⇒ Object

remove_views :edit, :show, :folder => :person

Raises:

  • (ArgumentError)


78
79
80
81
82
# File 'lib/rails3_artifactor/artifact/view_artifact.rb', line 78

def remove_views *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_view name, options}      
end

#view_file(*args) ⇒ Object



19
20
21
# File 'lib/rails3_artifactor/artifact/view_artifact.rb', line 19

def view_file *args
  view_file_name(args)
end