Class: Lore::Attribute_Settings
- Defined in:
- lib/lore/model/attribute_settings.rb
Overview
There are several categories of attributes:
- required
-
A value for this attribute has to be set in any case (field is NOT NULL)
-
- implicit
-
Value for this attribute will be set by database. (field is set via sequence, trigger, etc.) Any given value will be ignored.
-
If none of the above, field will be treaded as NULL field.
Instance Attribute Summary collapse
-
#constraints ⇒ Object
Returns the value of attribute constraints.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#fields_flat ⇒ Object
Returns the value of attribute fields_flat.
-
#implicit ⇒ Object
Returns the value of attribute implicit.
-
#primary_keys ⇒ Object
Returns the value of attribute primary_keys.
-
#required ⇒ Object
Returns the value of attribute required.
-
#sequences ⇒ Object
Returns the value of attribute sequences.
-
#types ⇒ Object
Returns the value of attribute types.
Instance Method Summary collapse
- #[](table_name) ⇒ Object
- #add_aggregate_model(model) ⇒ Object
- #add_base_model(model) ⇒ Object
- #add_constraints(attrib, constraints = {}) ⇒ Object
- #add_hidden(attribute) ⇒ Object
- #add_primary_key(attribute, sequence_name = nil) ⇒ Object
-
#explicit ⇒ Object
All attributes that aren’t implicit and thus can be set manually.
- #fields_flat_rec(model = nil) ⇒ Object
- #implicit?(attribute) ⇒ Boolean
- #inherit(base_model) ⇒ Object
-
#initialize(accessor, fields, types) ⇒ Attribute_Settings
constructor
A new instance of Attribute_Settings.
- #num_fields ⇒ Object
- #num_own_fields ⇒ Object
- #required?(attribute) ⇒ Boolean
-
#set_implicit(*args) ⇒ Object
Implicit attributes are set by the DBMS, via sequences, triggers, or may not be set manually for some reason.
- #set_required(*args) ⇒ Object
- #set_sequence(attribute, sequence_name) ⇒ Object
Constructor Details
#initialize(accessor, fields, types) ⇒ Attribute_Settings
Returns a new instance of Attribute_Settings.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/lore/model/attribute_settings.rb', line 18 def initialize(accessor, fields, types) fields.map! { |a| a.to_sym } @accessor = accessor fields.map! { |f| f.to_sym } @fields = { accessor.table_name => fields } # @fields_flat = fields @required = {} @implicit = {} @types = {} @constraints = {} @hidden = {} @sequences = {} @primary_keys = {} sym_types = {} types.each_pair { |attrib, type| sym_types[attrib.to_sym] = type } @types[accessor.table_name] = sym_types end |
Instance Attribute Details
#constraints ⇒ Object
Returns the value of attribute constraints.
16 17 18 |
# File 'lib/lore/model/attribute_settings.rb', line 16 def constraints @constraints end |
#fields ⇒ Object
Returns the value of attribute fields.
16 17 18 |
# File 'lib/lore/model/attribute_settings.rb', line 16 def fields @fields end |
#fields_flat ⇒ Object
Returns the value of attribute fields_flat.
16 17 18 |
# File 'lib/lore/model/attribute_settings.rb', line 16 def fields_flat @fields_flat end |
#implicit ⇒ Object
Returns the value of attribute implicit.
16 17 18 |
# File 'lib/lore/model/attribute_settings.rb', line 16 def implicit @implicit end |
#primary_keys ⇒ Object
Returns the value of attribute primary_keys.
16 17 18 |
# File 'lib/lore/model/attribute_settings.rb', line 16 def primary_keys @primary_keys end |
#required ⇒ Object
Returns the value of attribute required.
16 17 18 |
# File 'lib/lore/model/attribute_settings.rb', line 16 def required @required end |
#sequences ⇒ Object
Returns the value of attribute sequences.
16 17 18 |
# File 'lib/lore/model/attribute_settings.rb', line 16 def sequences @sequences end |
#types ⇒ Object
Returns the value of attribute types.
16 17 18 |
# File 'lib/lore/model/attribute_settings.rb', line 16 def types @types end |
Instance Method Details
#[](table_name) ⇒ Object
38 39 40 |
# File 'lib/lore/model/attribute_settings.rb', line 38 def [](table_name) @fields[table_name] end |
#add_aggregate_model(model) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/lore/model/attribute_settings.rb', line 148 def add_aggregate_model(model) inherit(model) # Include indirect implicit fields, but do not # set aggregated primary keys as implicit, as it # will be set manually: aggregated_implicit = model.__attributes__.implicit.dup # model.get_primary_keys.each { |pkey_field| # aggregated_implicit[model.table_name].delete(pkey_field) # } # @implicit.update(aggregated_implicit) @fields_flat = false # Invalidate cached values end |
#add_base_model(model) ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/lore/model/attribute_settings.rb', line 140 def add_base_model(model) inherit(model) @sequences.update(model.__attributes__.sequences) @implicit.update(model.__attributes__.implicit) @required.update(model.__attributes__.required) @fields_flat = false # Invalidate cached values end |
#add_constraints(attrib, constraints = {}) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/lore/model/attribute_settings.rb', line 195 def add_constraints(attrib, constraints={}) if attrib.kind_of? Clause then attrib_split = attrib.to_s.split('.') table = attrib_split[0..-2] attrib = attrib_split[-1] else table = @accessor.table_name end attrib = attrib.to_sym unless attrib.is_a? Symbol @constraints[table] = Hash.new unless @constraints[table] @constraints[table][attrib] = Hash.new unless @constraints[table][attrib] if constraints[:mandatory] then set_required(table, attrib.to_s) end if constraints[:format] then @constraints[table][attrib][:format] = constraints[:format] end if constraints[:length] then if constraints[:length].kind_of? Range then @constraints[table][attrib][:minlength] = constraints[:length].first @constraints[table][attrib][:maxlength] = constraints[:length].last else @constraints[table][attrib][:minlength] = constraints[:length] @constraints[table][attrib][:maxlength] = constraints[:length] end end if constraints[:minlength] then @constraints[table][attrib][:minlength] = constraints[:minlength] end if constraints[:maxlength] then @constraints[table][attrib][:maxlength] = constraints[:maxlength] end end |
#add_hidden(attribute) ⇒ Object
136 137 138 |
# File 'lib/lore/model/attribute_settings.rb', line 136 def add_hidden(attribute) @hidden[@accessor.table_name] = attribute end |
#add_primary_key(attribute, sequence_name = nil) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/lore/model/attribute_settings.rb', line 97 def add_primary_key(attribute, sequence_name=nil) @primary_keys[@accessor.table_name] = [] unless @primary_keys[@accessor.table_name] @primary_keys[@accessor.table_name] << attribute if sequence_name then set_sequence(attribute, sequence_name) if sequence_name else set_required(attribute) end end |
#explicit ⇒ Object
All attributes that aren’t implicit and thus can be set manually.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/lore/model/attribute_settings.rb', line 118 def explicit return @explicit if @explicit @explicit = {} @fields.each_pair { |table, attrib_list| @explicit[table] = attrib_list.reject { |a| # @fields includes all inherited fields. # We do not want to aggregated models to # extend @expected and @implicit, as they # can only be referenced by their primary # keys, but never assigned values in e.g. # Model.create. !@implicit[table] || @implicit[table].include?(a.to_sym) } } @explicit.delete_if { |table, fields| fields.length == 0 } @explicit end |
#fields_flat_rec(model = nil) ⇒ Object
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/lore/model/attribute_settings.rb', line 175 def fields_flat_rec(model=nil) fields = [] model ||= @accessor joined_models = model.__associations__.joined_models model.__associations__.joins.each_pair { |table,base_tables| fields += @fields[table] fields += fields_flat_rec(joined_models[table].first) } fields end |
#implicit?(attribute) ⇒ Boolean
55 56 57 |
# File 'lib/lore/model/attribute_settings.rb', line 55 def implicit?(attribute) @implicit[attribute] end |
#inherit(base_model) ⇒ Object
186 187 188 189 190 191 192 193 |
# File 'lib/lore/model/attribute_settings.rb', line 186 def inherit(base_model) parent_attributes = base_model.__attributes__ @constraints.update(parent_attributes.constraints) @types.update(parent_attributes.types) @fields.update(parent_attributes.fields) # @fields_flat += parent_attributes.fields_flat @primary_keys.update(parent_attributes.primary_keys) end |
#num_fields ⇒ Object
42 43 44 |
# File 'lib/lore/model/attribute_settings.rb', line 42 def num_fields fields_flat.length end |
#num_own_fields ⇒ Object
45 46 47 |
# File 'lib/lore/model/attribute_settings.rb', line 45 def num_own_fields fields[@accessor.table_name].length end |
#required?(attribute) ⇒ Boolean
49 50 51 52 53 54 |
# File 'lib/lore/model/attribute_settings.rb', line 49 def required?(attribute) # Inherited primary keys are marked as required, # but they aren't in this model, where they are # also marked as implicit. @required[attribute] && !@implicit[attribute] || false end |
#set_implicit(*args) ⇒ Object
Implicit attributes are set by the DBMS, via sequences, triggers, or may not be set manually for some reason. Manually set values for implicit attributes are ignored on INSERT and UPDATE commands, but may be used in e.g. WHERE part of a query.
Usage:
set_implicit(table, :attrib_a)
Or
set_implicit(:attrib_a) # table defaults to own table
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/lore/model/attribute_settings.rb', line 83 def set_implicit(*args) table = nil attribute = nil if args.length > 1 then table = args.at(0) attribute = args.at(1).to_sym else table = @accessor.table_name attribute = args.at(0).to_sym end @implicit[table] = [] unless @implicit[table] @implicit[table] << attribute end |
#set_required(*args) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/lore/model/attribute_settings.rb', line 59 def set_required(*args) table = @accessor.table_name if args.length == 1 then attribute = args.at(0) else table = args.at(0) attribute = args.at(1) end @required[table] = {} unless @required[table] @required[table][attribute.to_sym] = true end |
#set_sequence(attribute, sequence_name) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/lore/model/attribute_settings.rb', line 107 def set_sequence(attribute, sequence_name) set_implicit(attribute) if @sequences[@accessor.table_name] then @sequences[@accessor.table_name][attribute] = sequence_name else @sequences[@accessor.table_name] = { attribute => sequence_name } end end |