Module: Cistern::Attributes::ClassMethods

Defined in:
lib/cistern/attributes.rb

Instance Method Summary collapse

Instance Method Details

#aliasesObject



37
38
39
# File 'lib/cistern/attributes.rb', line 37

def aliases
  @aliases ||= Hash.new { |h, k| h[k] = [] }
end

#attribute(name, options = {}) ⇒ Object



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

def attribute(name, options = {})
  name_sym = name.to_sym

  if attributes.key?(name_sym)
    fail(ArgumentError, "#{self.name} attribute[#{name_sym}] specified more than once")
  end

  add_coverage(options)

  normalize_options(options)

  attributes[name_sym] = options

  define_attribute_reader(name_sym, options) unless options[:reader] == false
  define_attribute_writer(name_sym, options) unless options[:writer] == false

  name_sym
end

#attributesObject



41
42
43
# File 'lib/cistern/attributes.rb', line 41

def attributes
  @attributes ||= parent_attributes || {}
end

#default_parserObject



33
34
35
# File 'lib/cistern/attributes.rb', line 33

def default_parser
  @default_parser ||= ->(v, _opts) { v }
end

#identity(*args) ⇒ Object



64
65
66
# File 'lib/cistern/attributes.rb', line 64

def identity(*args)
  args.any? ? @identity = attribute(*args) : (@identity ||= parent_identity)
end

#ignore_attributes(*args) ⇒ Object



68
69
70
# File 'lib/cistern/attributes.rb', line 68

def ignore_attributes(*args)
  @ignored_attributes = args
end

#ignored_attributesObject



72
73
74
# File 'lib/cistern/attributes.rb', line 72

def ignored_attributes
  @ignored_attributes ||= []
end

#parsersObject



6
7
8
9
10
11
12
13
14
15
# File 'lib/cistern/attributes.rb', line 6

def parsers
  @parsers ||= {
    array:   ->(v, _) { [*v] },
    boolean: ->(v, _) { TRUTHY.include?(v.to_s.downcase) },
    float:   ->(v, _) { v && v.to_f },
    integer: ->(v, _) { v && v.to_i },
    string:  ->(v, _) { v && v.to_s },
    time:    ->(v, _) { v.is_a?(Time) ? v : v && Time.parse(v.to_s) },
  }
end

#squasher(tree, path) ⇒ Object



17
18
19
# File 'lib/cistern/attributes.rb', line 17

def squasher(tree, path)
  tree.is_a?(::Hash) ? squasher(tree[path.shift], path) : tree
end

#transformsObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cistern/attributes.rb', line 21

def transforms
  @transforms ||= {
    squash: proc do |_, _v, options|
      v      = Cistern::Hash.stringify_keys(_v)
      squash = options[:squash]

      v.is_a?(::Hash) ? squasher(v, squash.dup) : v
    end,
    none: ->(_, v, _) { v }
  }
end