Class: LucidWorks::Schema::Attribute
- Inherits:
-
Object
- Object
- LucidWorks::Schema::Attribute
- Defined in:
- lib/lucid_works/schema/attribute.rb
Direct Known Subclasses
BooleanAttribute, CustomAttribute, IntegerAttribute, Iso8601Attribute, ListAttribute, StringAttribute
Constant Summary collapse
- ATTRIBUTES_TYPES =
[ :string, :integer, :boolean, :iso8601, :custom, :list ]
- RESERVED_ATTRIBUTE_NAMES =
%{ class nil? send caller object_id }
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#origin ⇒ Object
readonly
Returns the value of attribute origin.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
- .factory(schema, name, type, options = {}) ⇒ Object
-
.sanitize_name(identifier) ⇒ Object
Change any characters illegal for an identifier to _.
Instance Method Summary collapse
-
#create_accessors_for_attribute(klass) ⇒ Object
:nodoc:.
-
#encode_and_insert(value, hash, is_update) ⇒ Object
:nodoc:.
- #human_value(value) ⇒ Object
-
#initialize(schema, name, options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #nil_when_blank? ⇒ Boolean
- #omit_during_update? ⇒ Boolean
- #omit_when_blank? ⇒ Boolean
- #to_select ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(schema, name, options = {}) ⇒ Attribute
Returns a new instance of Attribute.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/lucid_works/schema/attribute.rb', line 26 def initialize(schema, name, ={}) @schema = schema @true_name = name.to_s # External representation. Will be used when sending data back to REST API @name = self.class.sanitize_name(name) # Internal representation. A symbol. @origin = [:origin] @omit_during_update = [:omit_during_update] @omit_when_blank = [:omit_when_blank] @nil_when_blank = [:nil_when_blank] @values = [:values] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/lucid_works/schema/attribute.rb', line 8 def name @name end |
#origin ⇒ Object (readonly)
Returns the value of attribute origin.
8 9 10 |
# File 'lib/lucid_works/schema/attribute.rb', line 8 def origin @origin end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
8 9 10 |
# File 'lib/lucid_works/schema/attribute.rb', line 8 def schema @schema end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
8 9 10 |
# File 'lib/lucid_works/schema/attribute.rb', line 8 def values @values end |
Class Method Details
.factory(schema, name, type, options = {}) ⇒ Object
12 13 14 15 16 |
# File 'lib/lucid_works/schema/attribute.rb', line 12 def factory(schema, name, type, ={}) raise "Unknown attribute type: #{type.inspect}" unless ATTRIBUTES_TYPES.include?(type) attribute_class = ( "LucidWorks::Schema::" + ("#{type}_attribute".classify) ).constantize attribute_class.new(schema, name, ) end |
.sanitize_name(identifier) ⇒ Object
Change any characters illegal for an identifier to _
19 20 21 22 23 |
# File 'lib/lucid_works/schema/attribute.rb', line 19 def sanitize_name(identifier) # :nodoc: sane_identifier = identifier.to_s.gsub(/[^\w]/, '_') sane_identifier = "_#{sane_identifier}" if RESERVED_ATTRIBUTE_NAMES.include?(sane_identifier) sane_identifier.to_sym end |
Instance Method Details
#create_accessors_for_attribute(klass) ⇒ Object
:nodoc:
45 46 47 48 49 50 51 |
# File 'lib/lucid_works/schema/attribute.rb', line 45 def create_accessors_for_attribute(klass) # :nodoc: klass.class_eval <<-EOF, __FILE__, __LINE__+1 def #{name} # def foo @attributes[:#{name}] # @attributes[:foo] end # end EOF end |
#encode_and_insert(value, hash, is_update) ⇒ Object
:nodoc:
53 54 55 56 57 |
# File 'lib/lucid_works/schema/attribute.rb', line 53 def encode_and_insert(value, hash, is_update) # :nodoc: return if omit_during_update? && is_update return if omit_when_blank? && (value.nil? || value == "") hash[@true_name.to_s] = encode(value) end |
#human_value(value) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/lucid_works/schema/attribute.rb', line 59 def human_value(value) if values # If this attribute was defined with a set of :values, # assume we can get translations for those valuesfrom the L10n database. l10n_scope = %w{activemodel models} + schema.model.name.underscore.split('/').drop(1) + [name] return I18n.t(value, :scope => l10n_scope, :default => value) end value.to_s rescue value.to_s end |
#nil_when_blank? ⇒ Boolean
43 |
# File 'lib/lucid_works/schema/attribute.rb', line 43 def nil_when_blank? ; @nil_when_blank end |
#omit_during_update? ⇒ Boolean
41 |
# File 'lib/lucid_works/schema/attribute.rb', line 41 def omit_during_update? ; @omit_during_update end |
#omit_when_blank? ⇒ Boolean
42 |
# File 'lib/lucid_works/schema/attribute.rb', line 42 def omit_when_blank? ; @omit_when_blank end |
#to_select ⇒ Object
72 73 74 75 76 77 |
# File 'lib/lucid_works/schema/attribute.rb', line 72 def to_select raise "Can't to_select for attribute #{name} as it has no values" unless values values.map do |value| [human_value(value), value] end end |
#type ⇒ Object
37 38 39 |
# File 'lib/lucid_works/schema/attribute.rb', line 37 def type raise "type() method must be implemented by Attribute subclass" end |