Module: Cistern::Attributes::ClassMethods

Included in:
Singular
Defined in:
lib/cistern/attributes.rb

Instance Method Summary collapse

Instance Method Details

#_load(marshalled) ⇒ Object



50
51
52
# File 'lib/cistern/attributes.rb', line 50

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

#aliasesObject



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

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

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



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

def attribute(_name, options = {})
  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:"
      options[:coverage_file] = attribute_call.absolute_path
      options[:coverage_line] = attribute_call.lineno
      options[:coverage_hits] = 0
    end
  end

  name = _name.to_s.to_sym

  self.send(:define_method, name) do
    read_attribute(name)
  end unless self.instance_methods.include?(name)

  if options[:type] == :boolean
    self.send(:alias_method, "#{name}?", name)
  end

  self.send(:define_method, "#{name}=") do |value|
    write_attribute(name, value)
  end unless self.instance_methods.include?("#{name}=".to_sym)

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

  options[:aliases] = Array(options[:aliases] || options[:alias]).map { |a| a.to_s.to_sym }

  options[:aliases].each do |new_alias|
    aliases[new_alias] << name.to_s.to_sym
  end
end

#attributesObject



58
59
60
# File 'lib/cistern/attributes.rb', line 58

def attributes
  @attributes ||= {}
end

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



104
105
106
107
# File 'lib/cistern/attributes.rb', line 104

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

#ignore_attributes(*args) ⇒ Object



109
110
111
# File 'lib/cistern/attributes.rb', line 109

def ignore_attributes(*args)
  @ignored_attributes = args
end

#ignored_attributesObject



113
114
115
# File 'lib/cistern/attributes.rb', line 113

def ignored_attributes
  @ignored_attributes ||= []
end