Class: LucidWorks::Schema::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/lucid_works/schema/attribute.rb

Constant Summary collapse

ATTRIBUTES_TYPES =
[ :string, :integer, :boolean, :iso8601, :custom, :list ]
RESERVED_ATTRIBUTE_NAMES =
%{ class nil? send caller object_id }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options={})
  @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 = options[:origin]
  @omit_during_update = options[:omit_during_update]
  @omit_when_blank = options[:omit_when_blank]
  @nil_when_blank = options[:nil_when_blank]
  @values = options[:values]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/lucid_works/schema/attribute.rb', line 8

def name
  @name
end

#originObject (readonly)

Returns the value of attribute origin.



8
9
10
# File 'lib/lucid_works/schema/attribute.rb', line 8

def origin
  @origin
end

#schemaObject (readonly)

Returns the value of attribute schema.



8
9
10
# File 'lib/lucid_works/schema/attribute.rb', line 8

def schema
  @schema
end

#valuesObject (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, options={})
  raise "Unknown attribute type: #{type.inspect}" unless ATTRIBUTES_TYPES.include?(type)
  attribute_class = ( "LucidWorks::Schema::" + ("#{type}_attribute".classify) ).constantize
  attribute_class.new(schema, name, options)
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('/') + [name]
    return I18n.t(value, :scope => l10n_scope, :default => value)
  end
  value.to_s

rescue
  value.to_s
end

#nil_when_blank?Boolean

Returns:

  • (Boolean)


43
# File 'lib/lucid_works/schema/attribute.rb', line 43

def nil_when_blank?     ; @nil_when_blank     end

#omit_during_update?Boolean

Returns:

  • (Boolean)


41
# File 'lib/lucid_works/schema/attribute.rb', line 41

def omit_during_update? ; @omit_during_update end

#omit_when_blank?Boolean

Returns:

  • (Boolean)


42
# File 'lib/lucid_works/schema/attribute.rb', line 42

def omit_when_blank?    ; @omit_when_blank    end

#to_selectObject



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

#typeObject



37
38
39
# File 'lib/lucid_works/schema/attribute.rb', line 37

def type
  raise "type() method must be implemented by Attribute subclass"
end