Class: DeadSimpleCMS::Attribute::Type::Base

Inherits:
Object
  • Object
show all
Includes:
Util::Identifier
Defined in:
lib/dead_simple_cms/attribute/type/base.rb

Direct Known Subclasses

Boolean, Datetime, File, Numeric, String, Symbol

Constant Summary collapse

VALID_INPUT_TYPES =
[:string, :text, :select, :file, :radio, :datetime, :check_box].freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, options = {}) ⇒ Base

Returns a new instance of Base.



28
29
30
31
32
33
34
# File 'lib/dead_simple_cms/attribute/type/base.rb', line 28

def initialize(identifier, options={})
  options.reverse_merge!(:group_hierarchy => [], :input_type => default_input_type, :required => false)
  @hint, @default, @input_type, @group_hierarchy, @section, @required, @length =
    options.values_at(:hint, :default, :input_type, :group_hierarchy, :section, :required, :length)
  raise("Invalid input type: #{input_type.inspect}. Should be one of #{VALID_INPUT_TYPES}.") unless VALID_INPUT_TYPES.include?(input_type)
  super
end

Class Attribute Details

.builder_method_nameObject

If not provided on the subclass infer it from the class name. Because of how class_attribute works, this method will be overwritten when someone explicitly calls .builder_method_name= on a subclass.



14
15
16
# File 'lib/dead_simple_cms/attribute/type/base.rb', line 14

def builder_method_name
  @builder_method_name ||=  name.demodulize.underscore
end

Instance Attribute Details

#group_hierarchyObject (readonly)

Returns the value of attribute group_hierarchy.



25
26
27
# File 'lib/dead_simple_cms/attribute/type/base.rb', line 25

def group_hierarchy
  @group_hierarchy
end

#input_typeObject (readonly)

Returns the value of attribute input_type.



25
26
27
# File 'lib/dead_simple_cms/attribute/type/base.rb', line 25

def input_type
  @input_type
end

#requiredObject (readonly)

Returns the value of attribute required.



25
26
27
# File 'lib/dead_simple_cms/attribute/type/base.rb', line 25

def required
  @required
end

#sectionObject

Returns the value of attribute section.



26
27
28
# File 'lib/dead_simple_cms/attribute/type/base.rb', line 26

def section
  @section
end

Instance Method Details

#defaultObject



53
54
55
# File 'lib/dead_simple_cms/attribute/type/base.rb', line 53

def default
  @default.is_a?(Proc) ? @default.call : @default
end

#hintObject



45
46
47
# File 'lib/dead_simple_cms/attribute/type/base.rb', line 45

def hint
  @hint.is_a?(Proc) ? @hint.call : @hint
end

#inspectObject



67
68
69
70
# File 'lib/dead_simple_cms/attribute/type/base.rb', line 67

def inspect
  ivars = [:identifier, :hint, :default, :required, :input_type].map { |m| ivar = "@#{m}" ; "#{ivar}=#{instance_variable_get(ivar).inspect}" }
  "#<#{self.class} #{ivars.join(", ")}"
end

#lengthObject



49
50
51
# File 'lib/dead_simple_cms/attribute/type/base.rb', line 49

def length
  @length.is_a?(Proc) ? @length.call : @length
end

#root_group?Boolean

Returns:



36
37
38
# File 'lib/dead_simple_cms/attribute/type/base.rb', line 36

def root_group?
  group_hierarchy.last.try(:root?)
end

#section_identifierObject

Public: The identifier on the section level. It must be unique amongst the groups.



41
42
43
# File 'lib/dead_simple_cms/attribute/type/base.rb', line 41

def section_identifier
  (group_hierarchy + [self]).map(&:identifier).join("_").to_sym
end

#valueObject

Public: Returns the non-blank value from the storage or the default.



62
63
64
65
# File 'lib/dead_simple_cms/attribute/type/base.rb', line 62

def value
  attributes = attributes_from_storage
  attributes.key?(section_identifier) ? attributes[section_identifier] : default
end

#value=(value) ⇒ Object



57
58
59
# File 'lib/dead_simple_cms/attribute/type/base.rb', line 57

def value=(value)
  attributes_from_storage[section_identifier] = convert_value(value)
end