Module: PropertySets::PropertySetModel::ClassMethods

Defined in:
lib/property_sets/property_set_model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



104
105
106
107
108
109
# File 'lib/property_sets/property_set_model.rb', line 104

def self.extended(base)
  base.validate :validate_format_of_name
  base.validate :validate_length_of_serialized_data
  base.before_create :coerce_value
  base.attr_accessible :name, :value if defined?(ProtectedAttributes)
end

Instance Method Details

#default(key) ⇒ Object



123
124
125
# File 'lib/property_sets/property_set_model.rb', line 123

def default(key)
  PropertySets::Casting.read(type(key), raw_default(key))
end

#keysObject



119
120
121
# File 'lib/property_sets/property_set_model.rb', line 119

def keys
  properties.keys
end

#owner_assocObject



152
153
154
# File 'lib/property_sets/property_set_model.rb', line 152

def owner_assoc
  @owner_assoc
end

#owner_assoc=(association) ⇒ Object



148
149
150
# File 'lib/property_sets/property_set_model.rb', line 148

def owner_assoc=(association)
  @owner_assoc = association
end

#owner_class=(owner_class_name) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/property_sets/property_set_model.rb', line 139

def owner_class=(owner_class_name)
  @owner_class_sym = owner_class_name.to_s.demodulize.underscore.to_sym

  belongs_to owner_class_sym, class_name: owner_class_name
  validates_presence_of owner_class_sym, class_name: owner_class_name
  validates_uniqueness_of :name, scope: owner_class_key_sym, case_sensitive: false
  attr_accessible owner_class_key_sym, owner_class_sym if defined?(ProtectedAttributes)
end

#owner_class_key_symObject



160
161
162
# File 'lib/property_sets/property_set_model.rb', line 160

def owner_class_key_sym
  :"#{owner_class_sym}_id"
end

#owner_class_symObject



156
157
158
# File 'lib/property_sets/property_set_model.rb', line 156

def owner_class_sym
  @owner_class_sym
end

#propertiesObject



111
112
113
# File 'lib/property_sets/property_set_model.rb', line 111

def properties
  @properties ||= HashWithIndifferentAccess.new
end

#property(key, options = nil) ⇒ Object



115
116
117
# File 'lib/property_sets/property_set_model.rb', line 115

def property(key, options = nil)
  properties[key] = options
end

#protected?(key) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/property_sets/property_set_model.rb', line 135

def protected?(key)
  properties[key].try(:[], :protected) || false
end

#raw_default(key) ⇒ Object



127
128
129
# File 'lib/property_sets/property_set_model.rb', line 127

def raw_default(key)
  properties[key].try(:[], :default)
end

#type(key) ⇒ Object



131
132
133
# File 'lib/property_sets/property_set_model.rb', line 131

def type(key)
  properties[key].try(:[], :type) || :string
end