Module: Cistern::Attributes::ClassMethods

Included in:
Collection, Model
Defined in:
lib/cistern/attributes.rb

Instance Method Summary collapse

Instance Method Details

#_load(marshalled) ⇒ Object



47
48
49
# File 'lib/cistern/attributes.rb', line 47

def _load(marshalled)
  new(Marshal.load(marshalled))
end

#aliasesObject



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
# File 'lib/cistern/attributes.rb', line 59

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

  parser = Cistern::Attributes.parsers[options[:type]] ||
    options[:parser] ||
    Cistern::Attributes.default_parser
  transform = Cistern::Attributes.transforms[options[:squash] ? :squash : :none] ||
    Cistern::Attributes.default_transform

  self.send(:define_method, name) do
    attributes[name.to_s.to_sym]
  end

  self.send(:define_method, "#{name}=") do |value|
    transformed = transform.call(name, value, options)
    attributes[name.to_s.to_sym]= parser.call(transformed, options)
  end

  if self.attributes[name]
    raise(ArgumentError, "#{self.name} attribute[#{_name}] specified more than once")
  else
    self.attributes[name] = options
  end

  Array(options[:aliases]).each do |new_alias|
    aliases[new_alias] ||= []
    aliases[new_alias] << name
  end
end

#attributesObject



55
56
57
# File 'lib/cistern/attributes.rb', line 55

def attributes
  @attributes ||= {}
end

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



89
90
91
92
# File 'lib/cistern/attributes.rb', line 89

def identity(name, options = {})
  @identity = name
  self.attribute(name, options)
end

#ignore_attributes(*args) ⇒ Object



94
95
96
# File 'lib/cistern/attributes.rb', line 94

def ignore_attributes(*args)
  @ignored_attributes = args
end

#ignored_attributesObject



98
99
100
# File 'lib/cistern/attributes.rb', line 98

def ignored_attributes
  @ignored_attributes ||= []
end