Class: Runo::Field

Inherits:
Object
  • Object
show all
Includes:
I18n
Defined in:
lib/_field.rb

Constant Summary collapse

DEFAULT_META =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 = {}) ⇒ Field

Returns a new instance of Field.



31
32
33
34
# File 'lib/_field.rb', line 31

def initialize(meta = {})
  @meta = self.class.const_get(:DEFAULT_META).merge meta
  @val  = val_cast(nil)
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



29
30
31
# File 'lib/_field.rb', line 29

def action
  @action
end

#resultObject (readonly)

Returns the value of attribute result.



29
30
31
# File 'lib/_field.rb', line 29

def result
  @result
end

Class Method Details

.h(val) ⇒ Object



25
26
27
# File 'lib/_field.rb', line 25

def self.h(val)
  Rack::Utils.escape_html val.to_s
end

.instance(meta = {}) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/_field.rb', line 17

def self.instance(meta = {})
  k = meta[:klass].to_s.split(/-/).inject(Runo) {|c, name|
    name = name.capitalize
    c.const_get(name)
  }
  k.new(meta) if k < self
end

Instance Method Details

#[](id, *arg) ⇒ Object



40
41
42
# File 'lib/_field.rb', line 40

def [](id, *arg)
  respond_to?("meta_#{id}") ? __send__("meta_#{id}", *arg) : @meta[id]
end

#[]=(id, v) ⇒ Object



44
45
46
# File 'lib/_field.rb', line 44

def []=(id, v)
  @meta[id] = v
end

#commit(type = :temp) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/_field.rb', line 172

def commit(type = :temp)
  if valid?
    @result = @action
    @action = nil if type == :persistent
    self
  end
end

#create(v = nil) ⇒ Object



148
149
150
# File 'lib/_field.rb', line 148

def create(v = nil)
  post :create, v
end

#default_actionObject



121
122
123
124
125
# File 'lib/_field.rb', line 121

def default_action
  return :read unless my[:sd]
  actions = my[:sd].workflow.class.const_get(:PERM).keys - [:read, :create, :update]
  ([:read, :create, :update] + actions).find {|action| permit? action }
end

#deleteObject



156
157
158
# File 'lib/_field.rb', line 156

def delete
  post :delete
end

#empty?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/_field.rb', line 188

def empty?
  val.respond_to?(:empty?) ? val.empty? : (val.to_s == '')
end

#errorsObject



192
193
194
# File 'lib/_field.rb', line 192

def errors
  []
end

#find_ancestor(&block) ⇒ Object



57
58
59
60
# File 'lib/_field.rb', line 57

def find_ancestor(&block)
  f = self
  block.call(f) ? (return f) : (f = f[:parent]) until f.nil?
end

#get(arg = {}) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/_field.rb', line 127

def get(arg = {})
  if permit_get? arg
    _get(arg)
  else
    if arg[:action]
      raise Runo::Error::Forbidden
    else
      arg[:action] = default_action
    end
    arg[:action] ? _get(arg) : 'xxx'
  end
end

#inspectObject



36
37
38
# File 'lib/_field.rb', line 36

def inspect
  caller.grep(/`inspect'/).empty? ? super : "<#{self.class} name=\"#{my[:name]}\">"
end

#item(*item_steps) ⇒ Object



52
53
54
55
# File 'lib/_field.rb', line 52

def item(*item_steps)
  item_steps = item_steps.first if item_steps.first.is_a? ::Array
  item_steps.empty? ? self : nil # scalar has no item
end

#load(v = nil) ⇒ Object



144
145
146
# File 'lib/_field.rb', line 144

def load(v = nil)
  post :load, v
end

#load_defaultObject



140
141
142
# File 'lib/_field.rb', line 140

def load_default
  post :load_default
end

#meta_adminsObject



97
98
99
# File 'lib/_field.rb', line 97

def meta_admins
  (my[:parent] ? my[:parent][:admins] : ['root']) | Array(@meta[:admin])
end

#meta_clientObject



85
86
87
# File 'lib/_field.rb', line 85

def meta_client
  Runo.client
end

#meta_folderObject



77
78
79
# File 'lib/_field.rb', line 77

def meta_folder
  find_ancestor {|f| f.is_a? Runo::Set::Static::Folder }
end

#meta_full_nameObject



67
68
69
# File 'lib/_field.rb', line 67

def meta_full_name
  my[:parent] ? "#{my[:parent][:full_name]}-#{my[:id]}" : my[:id]
end

#meta_groupObject



101
102
103
# File 'lib/_field.rb', line 101

def meta_group
  @meta[:group] || (my[:parent] ? my[:parent][:group] : [])
end

#meta_nameObject



62
63
64
65
# File 'lib/_field.rb', line 62

def meta_name
  my[:parent] && !my[:parent].is_a?(Runo::Set::Static::Folder) ?
    "#{my[:parent][:name]}-#{my[:id]}" : my[:id]
end

#meta_ownerObject



89
90
91
# File 'lib/_field.rb', line 89

def meta_owner
  @meta[:owner] || (my[:parent] ? my[:parent][:owner] : 'root')
end

#meta_ownersObject



93
94
95
# File 'lib/_field.rb', line 93

def meta_owners
  (my[:parent] ? my[:parent][:owners] : ['root']) | Array(@meta[:owner])
end

#meta_rolesObject



105
106
107
108
109
110
111
112
# File 'lib/_field.rb', line 105

def meta_roles
  roles  = 0
  roles |= Runo::Workflow::ROLE_ADMIN if my[:admins].include? my[:client]
  roles |= Runo::Workflow::ROLE_GROUP if my[:group].include? my[:client]
  roles |= Runo::Workflow::ROLE_OWNER if my[:owner] == my[:client]
  roles |= (Runo.client == 'nobody') ? Runo::Workflow::ROLE_NONE : Runo::Workflow::ROLE_USER
  roles
end

#meta_sdObject



81
82
83
# File 'lib/_field.rb', line 81

def meta_sd
  find_ancestor {|f| f.is_a? Runo::Set::Dynamic }
end

#meta_short_nameObject



71
72
73
74
75
# File 'lib/_field.rb', line 71

def meta_short_name
  return '' if Runo.base && my[:full_name] == Runo.base[:full_name]
  my[:parent] && Runo.base && my[:parent][:full_name] != Runo.base[:full_name] ?
    "#{my[:parent][:short_name]}-#{my[:id]}" : my[:id]
end

#pending?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/_field.rb', line 180

def pending?
  @action ? true : false
end

#permit?(action) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
# File 'lib/_field.rb', line 114

def permit?(action)
  return false if action == :create && @result == :load
  return true unless my[:sd]
  return true if my[:sd].workflow.permit?(my[:roles], action)
  return true if find_ancestor {|f| f[:id] =~ Runo::REX::ID_NEW } # descendant of a new item
end

#post(action, v = nil) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/_field.rb', line 160

def post(action, v = nil)
  raise Runo::Error::Forbidden unless permit_post?(action, v)

  if _post action, val_cast(v)
    @result = (action == :load || action == :load_default) ? action : nil
    @action = (action == :load || action == :load_default) ? nil : action
  else
    @result = nil
  end
  self
end

#update(v = nil) ⇒ Object



152
153
154
# File 'lib/_field.rb', line 152

def update(v = nil)
  post :update, v
end

#valObject



48
49
50
# File 'lib/_field.rb', line 48

def val
  @val
end

#valid?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/_field.rb', line 184

def valid?
  errors.empty?
end