Module: Cistern::Attributes
- Defined in:
- lib/cistern/attributes.rb
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Class Method Summary
collapse
Class Method Details
.default_parser ⇒ Object
43
44
45
|
# File 'lib/cistern/attributes.rb', line 43
def self.default_parser
@default_parser ||= lambda { |v, opts| v }
end
|
.parsers ⇒ Object
2
3
4
5
6
7
8
9
10
11
|
# File 'lib/cistern/attributes.rb', line 2
def self.parsers
@parsers ||= {
:string => lambda { |v,opts| v.to_s },
:time => lambda { |v,opts| v.is_a?(Time) ? v : v && Time.parse(v.to_s) },
:integer => lambda { |v,opts| v && v.to_i },
:float => lambda { |v,opts| v && v.to_f },
:array => lambda { |v,opts| [*v] },
:boolean => lambda { |v,opts| ['true', '1'].include?(v.to_s.downcase) }
}
end
|
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
|
# File 'lib/cistern/attributes.rb', line 13
def self.transforms
@transforms ||= {
:squash => Proc.new do |k, _v, options|
v = Cistern::Hash.stringify_keys(_v)
squash = options[:squash]
if v.is_a?(::Hash) && squash.is_a?(Array)
travel = lambda do |tree, path|
if tree.is_a?(::Hash)
travel.call(tree[path.shift], path)
else tree
end
end
travel.call(v, squash.dup)
elsif v.is_a?(::Hash)
if v.key?(squash.to_s.to_sym)
v[squash.to_s.to_sym]
elsif v.has_key?(squash.to_s)
v[squash.to_s]
else
v
end
else v
end
end,
:none => lambda { |k, v, opts| v },
}
end
|