Class: Dao::Params

Inherits:
Map
  • Object
show all
Includes:
Validations::Mixin
Defined in:
lib/dao/params.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validations::Mixin

included, list

Constructor Details

#initialize(*args, &block) ⇒ Params

Returns a new instance of Params.



77
78
79
80
81
82
83
84
# File 'lib/dao/params.rb', line 77

def initialize(*args, &block)
  @path = Path.default
  @status = Status.default
  @errors = Errors.new
  @validations = Validations.for(self)
  @form = Form.for(self)
  super
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



73
74
75
# File 'lib/dao/params.rb', line 73

def errors
  @errors
end

#formObject

Returns the value of attribute form.



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

def form
  @form
end

#pathObject

Returns the value of attribute path.



71
72
73
# File 'lib/dao/params.rb', line 71

def path
  @path
end

#resultObject

instance methods



69
70
71
# File 'lib/dao/params.rb', line 69

def result
  @result
end

#routeObject

Returns the value of attribute route.



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

def route
  @route
end

#statusObject

Returns the value of attribute status.



72
73
74
# File 'lib/dao/params.rb', line 72

def status
  @status
end

#validationsObject

Returns the value of attribute validations.



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

def validations
  @validations
end

Class Method Details

.keys_for(keys) ⇒ Object



53
54
55
# File 'lib/dao/params.rb', line 53

def keys_for(keys)
  keys.strip.split(%r/\s*,\s*/).map{|key| key =~ %r/^\d+$/ ? Integer(key) : key}
end

.parse(prefix, params = {}, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dao/params.rb', line 10

def parse(prefix, params = {}, options = {})
  prefix = prefix.to_s
  params = Map.new(params || {})
  base = Map.new(params || {})
  options = Map.options(options || {})
  parsed = Params.new
  parsed.update(params[prefix]) if params.has_key?(prefix)

  re = %r/^ #{ Regexp.escape(prefix) } (?: [(] ([^)]+) [)] )? $/x

  params.each do |key, value|
    next unless(key.is_a?(String) or key.is_a?(Symbol))
    key = key.to_s
    matched, keys = re.match(key).to_a
    next unless matched
    next unless keys
    keys = keys_for(keys)
    parsed.set(keys => value)
    base.delete(key)
  end

  whitelist = Set.new( [options.getopt([:include, :select, :only])].flatten.compact.map{|k| k.to_s} )
  blacklist = Set.new( [options.getopt([:exclude, :reject, :except])].flatten.compact.map{|k| k.to_s} )

  unless blacklist.empty?
    base.keys.dup.each do |key|
      base.delete(key) if blacklist.include?(key.to_s)
    end
  end

  unless whitelist.empty?
    base.keys.dup.each do |key|
      base.delete(key) unless whitelist.include?(key.to_s)
    end
  end

  if options.getopt(:fold, default=true)
    parsed_and_folded = base.merge(parsed)
  else
    parsed
  end
end

.process(path, params, options = {}) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/dao/params.rb', line 57

def process(path, params, options = {})
  return params if params.is_a?(Params)

  parsed = Params.parse(path, params, options)
  return parsed unless parsed.empty?

  return Params.new(params)
end

Instance Method Details

#inspectObject

look good for inspect



88
89
90
# File 'lib/dao/params.rb', line 88

def inspect
  ::JSON.pretty_generate(self, :max_nesting => 0)
end

#update(*args, &block) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/dao/params.rb', line 97

def update(*args, &block)
  if args.size==1 and args.first.respond_to?(:to_dao)
    to_dao = args.first.to_dao
    return super(to_dao)
  end
  super
end