Sha256: 54fb04492fa8c1e87c42e4ec46fddda9bb38f9ffbf4bae055376c40f17d480c6

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

module Cockpit
  class Scope
    attr_reader :key, :settings
    
    def initialize(settings, method, *args, &block)
      @settings = settings
      _process(method, *args, &block)
    end
    
    def value
      settings[key]
    end
    
    def value=(x)
      settings[key] = x
    end
    
    def to_i
      value.to_i
    end
    
    def to_s
      value.to_s
    end
    
    def to_f
      value.to_f
    end
    
    def nil?
      value.nil?
    end
    
    def empty?
      value.empty?
    end
    
    def blank?
      value.blank?
    end
    
    def _process(method, *args, &block)
      method  = method.to_s
      node    = method.dup
      
      boolean = !!(node =~ /\?$/)
      node.gsub!(/\?/, "") if boolean
      
      set     = !!(node =~ /=$/)
      node.gsub!("=", "") if set
      
      new_key = "#{key}.#{node}"
      
      return value.send(method) if key && !settings.has_key?(new_key) && value.respond_to?(method)
      
      if key
        @key       = new_key
      else
        @key       = node
      end
      
      if boolean == true
        settings.has_key?(key)
      elsif set == true
        self.value = args.pop
      else
        self
      end
    end
    
    def method_missing(method, *args, &block)
      _process(method, *args, &block)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cockpit-0.2.3 lib/cockpit/core/scope.rb
cockpit-0.2.2 lib/cockpit/core/scope.rb
cockpit-0.2.1 lib/cockpit/core/scope.rb