Sha256: 0175b9373c2d1c2796e2e34a6e05e3f52e93d20e9845e23a2a3b4bada1085917

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 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
      if settings[key].nil?
        nil
      else
        JSON.parse(%Q|{"value":#{settings[key]}}|)['value']
      end
    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

5 entries across 5 versions & 1 rubygems

Version Path
cockpit-0.2.4.4 lib/cockpit/core/scope.rb
cockpit-0.2.4.3 lib/cockpit/core/scope.rb
cockpit-0.2.4.2 lib/cockpit/core/scope.rb
cockpit-0.2.4.1 lib/cockpit/core/scope.rb
cockpit-0.2.4 lib/cockpit/core/scope.rb