Module: Cistern::Attributes::ClassMethods
- Included in:
- Collection, Model
- Defined in:
- lib/cistern/attributes.rb
Instance Method Summary collapse
- #_load(marshalled) ⇒ Object
- #aliases ⇒ Object
- #attribute(_name, options = {}) ⇒ Object
- #attributes ⇒ Object
- #identity(name, options = {}) ⇒ Object
- #ignore_attributes(*args) ⇒ Object
- #ignored_attributes ⇒ Object
Instance Method Details
#_load(marshalled) ⇒ Object
47 48 49 |
# File 'lib/cistern/attributes.rb', line 47 def _load(marshalled) new(Marshal.load(marshalled)) end |
#aliases ⇒ Object
51 52 53 |
# File 'lib/cistern/attributes.rb', line 51 def aliases @aliases ||= {} end |
#attribute(_name, options = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/cistern/attributes.rb', line 59 def attribute(_name, = {}) if defined? Cistern::Coverage attribute_call = Cistern::Coverage.find_caller_before("cistern/attributes.rb") # Only use DSL attribute calls from within a model if attribute_call and attribute_call.label.start_with? "<class:" [:coverage_file] = attribute_call.absolute_path [:coverage_line] = attribute_call.lineno [:coverage_hits] = 0 end end name = _name.to_s.to_sym parser = Cistern::Attributes.parsers[[:type]] || [:parser] || Cistern::Attributes.default_parser transform = Cistern::Attributes.transforms[[:squash] ? :squash : :none] || Cistern::Attributes.default_transform self.send(:define_method, name) do # record the attribute was accessed self.class.attributes[name.to_s.to_sym][:coverage_hits] += 1 rescue nil attributes[name.to_s.to_sym] end self.send(:define_method, "#{name}=") do |value| transformed = transform.call(name, value, ) attributes[name.to_s.to_sym]= parser.call(transformed, ) end if self.attributes[name] raise(ArgumentError, "#{self.name} attribute[#{_name}] specified more than once") else self.attributes[name] = end Array([:aliases]).each do |new_alias| aliases[new_alias] ||= [] aliases[new_alias] << name end end |
#attributes ⇒ Object
55 56 57 |
# File 'lib/cistern/attributes.rb', line 55 def attributes @attributes ||= {} end |
#identity(name, options = {}) ⇒ Object
103 104 105 106 |
# File 'lib/cistern/attributes.rb', line 103 def identity(name, = {}) @identity = name self.attribute(name, ) end |
#ignore_attributes(*args) ⇒ Object
108 109 110 |
# File 'lib/cistern/attributes.rb', line 108 def ignore_attributes(*args) @ignored_attributes = args end |
#ignored_attributes ⇒ Object
112 113 114 |
# File 'lib/cistern/attributes.rb', line 112 def ignored_attributes @ignored_attributes ||= [] end |