Class: LucidWorks::Schema
- Inherits:
-
ActiveSupport::HashWithIndifferentAccess
- Object
- ActiveSupport::HashWithIndifferentAccess
- LucidWorks::Schema
- Defined in:
- lib/lucid_works/schema.rb,
lib/lucid_works/schema/attribute.rb,
lib/lucid_works/schema/custom_attribute.rb,
lib/lucid_works/schema/string_attribute.rb,
lib/lucid_works/schema/boolean_attribute.rb,
lib/lucid_works/schema/integer_attribute.rb,
lib/lucid_works/schema/iso8601_attribute.rb
Overview
Schema is used to delare attributes for a model.
Defined Under Namespace
Classes: Attribute, BooleanAttribute, CustomAttribute, IntegerAttribute, Iso8601Attribute, StringAttribute
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
-
#add_attribute(name, type = :string) ⇒ Object
Used for classes that have no predefined schema.
-
#create_accessors_for_attributes(klass) ⇒ Object
Create accessors for all attributes defined in the schema.
-
#dynamic_attributes(on = nil) ⇒ Object
(also: #dynamic_attributes?)
If called with true (the default), attributes will be added to the schema when the model is first retrieved from the server.
- #find_or_create_attribute(name) ⇒ Object
- #has_attribute?(name) ⇒ Boolean
-
#initialize(model) ⇒ Schema
constructor
Initializes the schema, taking a block of schema declarations.
-
#primary_key(key = nil) ⇒ Object
If this model’s primary key is something other than ‘id’, use the primary_key method it indicate that.
-
#reset! ⇒ Object
Reset the schema to the originally defined version (before we added a bunch of attributes during load or create).
Constructor Details
#initialize(model) ⇒ Schema
Initializes the schema, taking a block of schema declarations. Typically it is called from LucidWorks::Base, e.g.:
class MyModel < LucidWorks::Base
schema do
attribute :string1, :string
attribute :bool1, :boolean
attribute :integer1, :integer
attributes :string2, :string3, :string4
attributes :bool2, :bool3, :type => :boolean
attributes :int2, :int3, :type => :integer
attribute :string_with_values, :values => ['one', 'two']
attribute :dontsendme, :omit_during_update => true
attribute :sendnull, :string, :nil_when_blank => true
end
end
26 27 28 29 30 |
# File 'lib/lucid_works/schema.rb', line 26 def initialize(model) @model = model @primary_key = :id @dynamic_attributes = true end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
7 8 9 |
# File 'lib/lucid_works/schema.rb', line 7 def model @model end |
Instance Method Details
#add_attribute(name, type = :string) ⇒ Object
Used for classes that have no predefined schema. When the class is retrieved.
73 74 75 76 77 78 |
# File 'lib/lucid_works/schema.rb', line 73 def add_attribute(name, type=:string) # :nodoc: raise "Class #{model.name} already has attribute #{name}" if has_attribute?(Attribute.sanitize_name(name)) attribute = Attribute.factory(self, name, type, :origin => :added_later) attribute.create_accessors_for_attribute(model) self[attribute.name] = attribute end |
#create_accessors_for_attributes(klass) ⇒ Object
Create accessors for all attributes defined in the schema.
81 82 83 84 85 |
# File 'lib/lucid_works/schema.rb', line 81 def create_accessors_for_attributes(klass) # :nodoc: self.values.each do |attr| attr.create_accessors_for_attribute(klass) end end |
#dynamic_attributes(on = nil) ⇒ Object Also known as: dynamic_attributes?
If called with true (the default), attributes will be added to the schema when the model is first retrieved from the server. If set to false only the attributes defined in the schema will be allowed and anything else will raise an exception.
47 48 49 50 |
# File 'lib/lucid_works/schema.rb', line 47 def dynamic_attributes(on=nil) @dynamic_attributes = !!on unless on.nil? @dynamic_attributes end |
#find_or_create_attribute(name) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/lucid_works/schema.rb', line 58 def find_or_create_attribute(name) attrname = Attribute.sanitize_name(name) attribute = fetch(attrname, nil) unless attribute if dynamic_attributes? attribute = add_attribute(name, :string) else raise "unknown attribute: \"#{attr}\"" end end attribute end |
#has_attribute?(name) ⇒ Boolean
54 55 56 |
# File 'lib/lucid_works/schema.rb', line 54 def has_attribute?(name) has_key? Attribute.sanitize_name(name) end |
#primary_key(key = nil) ⇒ Object
If this model’s primary key is something other than ‘id’, use the primary_key method it indicate that. e.g.
primary_key "name"
37 38 39 40 |
# File 'lib/lucid_works/schema.rb', line 37 def primary_key(key=nil) @primary_key = key.to_sym if key @primary_key end |
#reset! ⇒ Object
Reset the schema to the originally defined version (before we added a bunch of attributes during load or create)
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/lucid_works/schema.rb', line 88 def reset! each do |name, attr| if attr.origin != :original model.class_eval <<-EOF, __FILE__, __LINE__+1 undef_method(:#{name}) undef_method(:"#{name}=") EOF delete(name) end end end |