Class: Bike::File

Inherits:
Field
  • Object
show all
Defined in:
lib/scalar/file.rb

Overview

Author

Akira FUNAI

Copyright

Copyright © 2009 Akira FUNAI

Direct Known Subclasses

Img

Constant Summary

Constants inherited from Field

Bike::Field::DEFAULT_META

Instance Attribute Summary

Attributes inherited from Field

#action, #result

Instance Method Summary collapse

Methods inherited from Field

#[], #[]=, #create, #default_action, #delete, #empty?, #find_ancestor, #get, h, #inspect, instance, #item, #load, #load_default, #meta_admins, #meta_client, #meta_folder, #meta_full_name, #meta_group, #meta_name, #meta_owner, #meta_owners, #meta_roles, #meta_sd, #meta_short_name, #pending?, #permit?, #post, #update, #val, #valid?, #workflow

Methods included from I18n

_, bindtextdomain, domain, domain=, find_msg, lang, lang=, merge_msg!, msg, n_, parse_msg, po_dir, po_dir=

Constructor Details

#initialize(meta = {}) ⇒ File

Returns a new instance of File.



8
9
10
11
12
# File 'lib/scalar/file.rb', line 8

def initialize(meta = {})
  meta[:options].collect! {|i| i.downcase } if meta[:options]
  meta[:size] = $&.to_i if meta[:tokens] && meta[:tokens].first =~ /^\d+$/
  super
end

Instance Method Details

#bodyObject



37
38
39
40
41
42
43
44
# File 'lib/scalar/file.rb', line 37

def body
  raise Bike::Error::Forbidden unless permit? :read

  if ps = my[:persistent_sd]
    @body ||= ps.storage.val my[:persistent_name]
  end
  @body
end

#commit(type = :temp) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/scalar/file.rb', line 65

def commit(type = :temp)
  if type == :temp && @action == :delete
    @val = {}
    @body = nil
  elsif type == :persistent && ps = my[:persistent_sd]
    case @action
      when :create, :update, nil
        ps.storage.store(
          my[:persistent_name],
          @body,
          val['basename'][/\.([\w\.]+)$/, 1] || 'bin'
        ) if @body && valid?
      when :delete
        ps.storage.delete my[:persistent_name]
    end
  end
  super
end

#errorsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/scalar/file.rb', line 46

def errors
  if (
    val['basename'] &&
    my[:options].is_a?(::Array) &&
    !my[:options].empty? &&
    !my[:options].include?(val['basename'].to_s[/\.([\w\.]+)$/i, 1].downcase)
  )
    [_('wrong file type: should be %{types}') % {:types => my[:options].join('/')}]
  elsif (my[:max].to_i > 0) && (val['size'].to_i > my[:max])
    [_('too large: %{max} bytes maximum') % {:max => my[:max]}]
  elsif (my[:min].to_i == 1) && val['size'].to_i < 1
    [_('mandatory')]
  elsif (my[:min].to_i > 0) && (val['size'].to_i < my[:min])
    [_('too small: %{min} bytes minimum') % {:min => my[:min]}]
  else
    []
  end
end

#meta_pathObject



14
15
16
# File 'lib/scalar/file.rb', line 14

def meta_path
  my[:full_name].gsub('-', '/')
end

#meta_persistent_nameObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/scalar/file.rb', line 26

def meta_persistent_name
  f    = my[:parent]
  ps   = my[:persistent_sd]
  name = my[:id]
  until f == ps
    name = "#{f[:id]}-#{name}"
    f = f[:parent]
  end
  name
end

#meta_persistent_sdObject



22
23
24
# File 'lib/scalar/file.rb', line 22

def meta_persistent_sd
  find_ancestor {|f| f.is_a?(Bike::Set::Dynamic) && f.storage.persistent? }
end

#meta_tmp_pathObject



18
19
20
# File 'lib/scalar/file.rb', line 18

def meta_tmp_path
  "#{Bike.base[:path]}/#{Bike.base[:tid]}/#{my[:short_name].gsub('-', '/')}" if Bike.base
end