lib/hungryform/elements/base_element.rb in hungryform-0.0.1 vs lib/hungryform/elements/base_element.rb in hungryform-0.0.2

- old
+ new

@@ -1,22 +1,34 @@ class HungryForm - class BaseElement < ::Hashie::Mash - attr_accessor :name, :placeholders, :resolver + class BaseElement + attr_accessor :name, :placeholders, :resolver, :visible, :label, :dependency + alias_method :visible?, :visible def initialize(name, parent, resolver, options = {}) - self.placeholders ||= {} - self.resolver = resolver + @_options = options - super(options) + @placeholders ||= {} + @resolver = resolver - self.visible = true unless self.key?(:visible) - self.visible &&= resolver.resolve_dependency(::JSON.parse(self.dependency)) if self.key?(:dependency) + # The element is visible if no visible parameter passed or + # visible param equals true and the dependency is resolved positively + self.visible = options.has_key?(:visible)? options[:visible] : true + self.visible &&= resolver.resolve_dependency(::JSON.parse(options[:dependency])) if options[:dependency] + self.dependency = options[:dependency] || '' self.name = (parent.nil?? "" : "#{parent.name}_") + resolver.get_value(name, self) - if self.key?(:label) - self.label = resolver.get_value(self.label, self) - else + unless options[:label] self.label = resolver.get_value(name, self).humanize + else + self.label = resolver.get_value(options[:label], self) end + end + + def method_missing(name, *args, &block) + # Check if an option exists + return @_options.has_key?(name.to_s[0..-2].to_sym) if name.to_s[-1] == '?' + # Return an option + return @_options[name] if @_options.has_key?(name) + super end end end \ No newline at end of file