Class: DeadSimpleCMS::Attribute::Type::Base
- Inherits:
-
Object
- Object
- DeadSimpleCMS::Attribute::Type::Base
- Includes:
- Util::Identifier
- Defined in:
- lib/dead_simple_cms/attribute/type/base.rb
Constant Summary collapse
- VALID_INPUT_TYPES =
[:string, :text, :select, :file, :radio, :datetime, :check_box].freeze
Class Attribute Summary collapse
-
.builder_method_name ⇒ Object
If not provided on the subclass infer it from the class name.
Instance Attribute Summary collapse
-
#group_hierarchy ⇒ Object
readonly
Returns the value of attribute group_hierarchy.
-
#input_type ⇒ Object
readonly
Returns the value of attribute input_type.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#section ⇒ Object
Returns the value of attribute section.
Instance Method Summary collapse
- #default ⇒ Object
- #hint ⇒ Object
-
#initialize(identifier, options = {}) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
- #length ⇒ Object
- #root_group? ⇒ Boolean
-
#section_identifier ⇒ Object
Public: The identifier on the section level.
-
#value ⇒ Object
Public: Returns the non-blank value from the storage or the default.
- #value=(value) ⇒ Object
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, ={}) .reverse_merge!(:group_hierarchy => [], :input_type => default_input_type, :required => false) @hint, @default, @input_type, @group_hierarchy, @section, @required, @length = .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_name ⇒ Object
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_hierarchy ⇒ Object (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_type ⇒ Object (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 |
#required ⇒ Object (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 |
#section ⇒ Object
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
#default ⇒ Object
53 54 55 |
# File 'lib/dead_simple_cms/attribute/type/base.rb', line 53 def default @default.is_a?(Proc) ? @default.call : @default end |
#hint ⇒ Object
45 46 47 |
# File 'lib/dead_simple_cms/attribute/type/base.rb', line 45 def hint @hint.is_a?(Proc) ? @hint.call : @hint end |
#inspect ⇒ Object
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 |
#length ⇒ Object
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
36 37 38 |
# File 'lib/dead_simple_cms/attribute/type/base.rb', line 36 def root_group? group_hierarchy.last.try(:root?) end |
#section_identifier ⇒ Object
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 |
#value ⇒ Object
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 |