Module: Foliokit::Modules::Element

Extended by:
ActiveSupport::Concern
Included in:
AssetRendition, ContentStack, Event, EventAction, EventActionGroup, Foliokit::Manifest, Overlay::OverlayBase, State
Defined in:
lib/foliokit/modules/element.rb

Instance Method Summary collapse

Instance Method Details

#initialize(element, package) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/foliokit/modules/element.rb', line 66

def initialize(element, package)
  @element = element
  @package = package
  self.class.element_reader_attributes.each do |name, options|
    key = name.to_s.underscore
    value = nil
    target = element
    if options[:selector]
      if options[:selector].respond_to?(:call)
        target = (target > instance_exec(options, &(options[:selector]))).first
      else
        target = (target > options[:selector]).first
      end
    end
    unless target.nil?
      if options[:type].class == Class
        value = options[:type].convert(target, name, options)
      else
        attribute_name = options[:attribute] || name
        value = options[:text] ? target.text.strip : target[attribute_name]
        unless value.nil?
          value = value.send(:"to_#{options[:type]}") unless options[:type].nil?
          value.gsub!(/\s/, "_") if options[:clean]
          value = hashify(value) if options[:hash]
        end
      end
    end
    value = options[:default] if value.nil? and options[:default]
    instance_variable_set("@#{key}", value)
  end
end