Module: Cistern::Attributes::ClassMethods
- Defined in:
- lib/cistern/attributes.rb
Instance Method Summary collapse
- #aliases ⇒ Object
- #attribute(name, options = {}) ⇒ Object
- #attributes ⇒ Object
- #default_parser ⇒ Object
- #identity(*args) ⇒ Object
- #ignore_attributes(*args) ⇒ Object
- #ignored_attributes ⇒ Object
- #parsers ⇒ Object
- #squasher(tree, path) ⇒ Object
- #transforms ⇒ Object
Instance Method Details
#aliases ⇒ Object
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, = {}) name_sym = name.to_sym if attributes.key?(name_sym) fail(ArgumentError, "#{self.name} attribute[#{name_sym}] specified more than once") end add_coverage() () attributes[name_sym] = define_attribute_reader(name_sym, ) define_attribute_writer(name_sym, ) name_sym end |
#attributes ⇒ Object
41 42 43 |
# File 'lib/cistern/attributes.rb', line 41 def attributes @attributes ||= parent_attributes || {} end |
#default_parser ⇒ Object
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_attributes ⇒ Object
72 73 74 |
# File 'lib/cistern/attributes.rb', line 72 def ignored_attributes @ignored_attributes ||= [] end |
#parsers ⇒ Object
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 |
#transforms ⇒ Object
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, | v = Cistern::Hash.stringify_keys(_v) squash = [:squash] v.is_a?(::Hash) ? squasher(v, squash.dup) : v end, none: ->(_, v, _) { v } } end |