Module: Dao

Extended by:
Dao
Included in:
Dao
Defined in:
lib/dao/db.rb,
lib/dao/api.rb,
lib/dao/_lib.rb,
lib/dao/data.rb,
lib/dao/form.rb,
lib/dao/mode.rb,
lib/dao/name.rb,
lib/dao/path.rb,
lib/dao/slug.rb,
lib/dao/wrap.rb,
lib/dao/rails.rb,
lib/dao/route.rb,
lib/dao/coerce.rb,
lib/dao/engine.rb,
lib/dao/errors.rb,
lib/dao/params.rb,
lib/dao/result.rb,
lib/dao/status.rb,
lib/dao/upload.rb,
lib/dao/api/dsl.rb,
lib/dao/support.rb,
lib/dao/api/call.rb,
lib/dao/conducer.rb,
lib/dao/endpoint.rb,
lib/dao/messages.rb,
lib/dao/path_map.rb,
lib/dao/api/modes.rb,
lib/dao/extractor.rb,
lib/dao/api/routes.rb,
lib/dao/blankslate.rb,
lib/dao/exceptions.rb,
lib/dao/api/context.rb,
lib/dao/errors2html.rb,
lib/dao/validations.rb,
lib/dao/instance_exec.rb,
lib/dao/rack/middleware.rb,
lib/dao/api/initializers.rb,
lib/dao/conducer/autocrud.rb,
lib/dao/validations/common.rb,
lib/dao/conducer/attributes.rb,
lib/dao/conducer/collection.rb,
lib/dao/validations/callback.rb,
lib/dao/validations/instance.rb,
lib/dao/conducer/active_model.rb,
lib/dao/conducer/view_support.rb,
lib/dao/validations/validator.rb,
lib/dao/conducer/callback_support.rb,
lib/dao/conducer/controller_support.rb,
lib/dao/rack/middleware/params_parser.rb

Defined Under Namespace

Modules: Coerce, Errors2Html, InstanceExec, Middleware, Validations, Wrap Classes: Api, BlankSlate, Conducer, Context, Data, Db, Endpoint, Engine, Error, Errors, Extractor, Form, Messages, Mode, Name, Params, Path, PathMap, Result, Route, Slug, Status, Upload

Constant Summary collapse

Version =
'8.0.1'
Resource =
Conducer
Presenter =
Conducer
Conductor =
Conducer
Model =
Conducer
Errors2HTML =
Errors2Html
Validator =
Validations::Validator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.api(&block) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/dao/api/dsl.rb', line 43

def Dao.api(&block)
  if block
    Class.new(Api).tap{|api| api.evaluate(&block)}
  else
    Class.new(Api)
  end
end

.status(*args, &block) ⇒ Object



326
327
328
329
330
331
332
# File 'lib/dao/status.rb', line 326

def Dao.status(*args, &block)
  if args.empty? and block.nil?
    Status
  else
    Status.for(*args, &block)
  end
end

Instance Method Details

#args_for_arity(args, arity) ⇒ Object



206
207
208
209
# File 'lib/dao/support.rb', line 206

def args_for_arity(args, arity)
  arity = Integer(arity.respond_to?(:arity) ? arity.arity : arity)
  arity < 0 ? args.dup : args.slice(0, arity)
end

#call(object, method, *args, &block) ⇒ Object



201
202
203
204
# File 'lib/dao/support.rb', line 201

def call(object, method, *args, &block)
  args = Dao.args_for_arity(args, object.method(method).arity)
  object.send(method, *args, &block)
end

#currentObject



70
71
72
# File 'lib/dao/support.rb', line 70

def current
  Current
end

#current_controller(*args) ⇒ Object Also known as: controller



74
75
76
77
# File 'lib/dao/support.rb', line 74

def current_controller(*args)
  Current.controller = args.first unless args.empty?
  Current.controller
end

#current_controller=(controller) ⇒ Object Also known as: controller=



80
81
82
# File 'lib/dao/support.rb', line 80

def current_controller=(controller)
  Current.controller = controller
end

#db(*args, &block) ⇒ Object



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

def db(*args, &block)
  if args.empty? and block.nil?
    Db.instance
  else
    method = args.shift
    Db.instance.send(method, *args, &block)
  end
end

#dependenciesObject



8
9
10
11
12
13
14
15
16
# File 'lib/dao/_lib.rb', line 8

def dependencies
  {
    'rails'             => [ 'rails'             , ' ~> 6.0'  ] ,
    'map'               => [ 'map'               , ' ~> 6.6'  ] ,
    'fattr'             => [ 'fattr'             , ' ~> 2.4'  ] ,
    'tagz'              => [ 'tagz'              , ' ~> 9.10' ] ,
    'rails_current'     => [ 'rails_current'     , ' ~> 2.2'  ] ,
  }
end

#descriptionObject



18
19
20
# File 'lib/dao/_lib.rb', line 18

def description
  "presenter, conductor, api, and better form objects for you rails' pleasure"
end

#ensure_interface!(object, *interface) ⇒ Object



239
240
241
242
243
# File 'lib/dao/support.rb', line 239

def ensure_interface!(object, *interface)
  interface.flatten.compact.each do |method|
    raise(NotImplementedError, "#{ object.class.name }##{ method }") unless object.respond_to?(method)
  end
end

#inc_keys!(keys) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/dao/support.rb', line 149

def inc_keys!(keys)
  last_number_index = nil

  keys.each_with_index do |k, i|
    if k.is_a?(Numeric)
      last_number_index = i
    end
  end

  if last_number_index
    keys[last_number_index] = keys[last_number_index] + 1
  end

  keys
end

#json_for(object, options = {}) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/dao/support.rb', line 183

def json_for(object, options = {})
  object = object.as_json if object.respond_to?(:as_json)

  pretty = (options.delete(:pretty) || json_pretty?)

  generate = pretty ? :pretty_generate : :generate

  begin
    JSON.send(generate, object, options)
  rescue Object => _
    YAML.load( object.to_yaml ).to_json
  end
end

#json_pretty?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/dao/support.rb', line 197

def json_pretty?
  @json_pretty ||= (defined?(Rails) ? !Rails.env.production? : true)
end

#keys_for(*keys) ⇒ Object Also known as: key_for



138
139
140
141
142
143
144
145
146
# File 'lib/dao/support.rb', line 138

def keys_for(*keys)
  keys = keys.join('.').scan(/[^\,\.\s]+/iomx)
  
  keys.map do |key|
    digity, stringy, digits = %r/^(~)?(\d+)$/iomx.match(key).to_a

    digity ? stringy ? String(digits) : Integer(digits) : key
  end
end

#libdir(*args, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dao/_lib.rb', line 22

def libdir(*args, &block)
  @libdir ||= File.dirname(File.expand_path(__FILE__).sub(/\.rb$/,''))
  args.empty? ? @libdir : File.join(@libdir, *args)
ensure
  if block
    begin
      $LOAD_PATH.unshift(@libdir)
      block.call()
    ensure
      $LOAD_PATH.shift()
    end
  end
end

#load(*libs) ⇒ Object



36
37
38
39
# File 'lib/dao/_lib.rb', line 36

def load(*libs)
  libs = libs.join(' ').scan(/[^\s+]+/)
  Dao.libdir{ libs.each{|lib| Kernel.load(lib) } }
end

#map_for(*args, &block) ⇒ Object Also known as: map, hash



3
4
5
# File 'lib/dao/support.rb', line 3

def map_for(*args, &block)
  Map.for(*args, &block)
end

#mock_controllerObject



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

def mock_controller
  Current.mock_controller
end

#name_for(path, *keys) ⇒ Object



66
67
68
# File 'lib/dao/support.rb', line 66

def name_for(path, *keys)
  Form.name_for(path, *keys)
end

#normalize_parameters(params) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/dao/support.rb', line 113

def normalize_parameters(params)
  dao = (params.delete('dao') || {}).merge(params.delete(:dao) || {})

  unless dao.empty?
    dao.each do |key, paths_and_values|
      next if paths_and_values.blank?
      map = Map.for(params[key])

      paths_and_values.each do |path, value|
        keys = keys_for(path)
        if map.has?(keys)
          inc_keys!(keys)
        end
        map.set(keys => value)
      end

      params[key] = map
    end

    params['dao'] = dao
  end

  params
end

#options_for(args) ⇒ Object



13
14
15
# File 'lib/dao/support.rb', line 13

def options_for(args)
  Map.options_for(args)
end

#options_for!(args) ⇒ Object



9
10
11
# File 'lib/dao/support.rb', line 9

def options_for!(args)
  Map.options_for!(args)
end

#render_json(object, options = {}) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/dao/support.rb', line 165

def render_json(object, options = {})
  options = options.to_options!
  controller = options[:controller] || Dao.current_controller

  controller.instance_eval do
    json = Dao.json_for(object)

    status = object.status rescue (options[:status] || 200)
    status = status.code if status.respond_to?(:code)

    respond_to do |wants|
      wants.json{ render :json => json, :status => status }
      wants.html{ render :text => json, :status => status, :content_type => 'text/plain' }
      wants.xml{ render :text => 'no soup for you!', :status => 403 }
    end
  end
end

#rootObject



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

def root
  if defined?(Rails.root) and Rails.root
    Rails.root
  else
    '.'
  end
end

#tree_walk(node, *path, &block) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/dao/support.rb', line 211

def tree_walk(node, *path, &block)
  iterator = Array === node ? :each_with_index : :each

  node.send(iterator) do |key, val|
    key, val = val, key if Array === node
    path.push(key)
    begin
      caught =
        catch(:tree_walk) do
          block.call(path, val)
          nil
        end
      next if caught==:next_sibling

      case val
        when Hash, Array
          tree_walk(val, *path, &block)
      end
    ensure
      path.pop
    end
  end
end

#unindent(s) ⇒ Object



60
61
62
63
64
# File 'lib/dao/support.rb', line 60

def unindent(s)
  s = "#{ s }"
  unindent!(s)
  s
end

#unindent!(s) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/dao/support.rb', line 50

def unindent!(s)
  margin = nil
  s.each_line do |line|
    next if line =~ %r/^\s*$/
    margin = line[%r/^\s*/] and break
  end
  s.gsub! %r/^#{ margin }/, "" if margin
  margin ? s : nil
end

#uuidObject



235
236
237
# File 'lib/dao/support.rb', line 235

def uuid
  SecureRandom.uuid
end

#versionObject



4
5
6
# File 'lib/dao/_lib.rb', line 4

def version
  Dao::Version
end