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
38 39 40 |
# File 'lib/cistern/attributes.rb', line 38 def aliases @aliases ||= Hash.new { |h, k| h[k] = [] } end |
#attribute(name, options = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/cistern/attributes.rb', line 46 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, ) unless [:reader] == false define_attribute_writer(name_sym, ) unless [:writer] == false name_sym end |
#attributes ⇒ Object
42 43 44 |
# File 'lib/cistern/attributes.rb', line 42 def attributes @attributes ||= parent_attributes || {} end |
#default_parser ⇒ Object
34 35 36 |
# File 'lib/cistern/attributes.rb', line 34 def default_parser @default_parser ||= ->(v, _opts) { v } end |
#identity(*args) ⇒ Object
65 66 67 |
# File 'lib/cistern/attributes.rb', line 65 def identity(*args) args.any? ? @identity = attribute(*args) : (@identity ||= parent_identity) end |
#ignore_attributes(*args) ⇒ Object
69 70 71 |
# File 'lib/cistern/attributes.rb', line 69 def ignore_attributes(*args) @ignored_attributes = args end |
#ignored_attributes ⇒ Object
73 74 75 |
# File 'lib/cistern/attributes.rb', line 73 def ignored_attributes @ignored_attributes ||= [] end |
#parsers ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/cistern/attributes.rb', line 6 def parsers @parsers ||= { array: ->(v, _) { [*v] }, boolean: ->(v, _) { TRUTHY.include?(v.to_s.downcase) }, date: ->(v, _) { v.is_a?(Date) ? v : v && Date.parse(v.to_s) }, 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
18 19 20 |
# File 'lib/cistern/attributes.rb', line 18 def squasher(tree, path) tree.is_a?(::Hash) ? squasher(tree[path.shift], path) : tree end |
#transforms ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cistern/attributes.rb', line 22 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 |