Sha256: 73a81d9fdb9bfa0205d8a153afcaba34076473962215a99ee59fb1b6b53b90e8

Contents?: true

Size: 1.22 KB

Versions: 14

Compression:

Stored size: 1.22 KB

Contents

module ContentfulMiddleman
  class Context < BasicObject
    def initialize
      @variables = {}
    end

    def method_missing(symbol, *args, &block)
      if symbol =~ /\A.+=\z/
        variable_name  = symbol.to_s.gsub('=','')
        variable_value = args.first

        set variable_name, variable_value
      else
        get symbol
      end
    end

    def set(name, value)
      @variables[name.to_sym] = value
    end

    def get(name)
      @variables.fetch(name.to_sym)
    end

    def has?(name)
      @variables.key?(name)
    end

    def is_a?(klass)
      Context == klass
    end

    def to_yaml
      hashize.to_yaml
    end

    def hashize
      variables = @variables.dup
      variables.update(variables) do |variable_name, variable_value|
        ensure_primitive_data_types(variable_value)
      end
    end

    def ensure_primitive_data_types(value)
      case value
      when Context
        value.hashize
      when ::Array
        value.map {|element| ensure_primitive_data_types(element)}
      when ::Hash
        res = {}
        value.each do |k, v|
          res[ensure_primitive_data_types(k)] = ensure_primitive_data_types(v)
        end
        res
      else
        value
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
contentful_middleman-4.2.0 lib/contentful_middleman/commands/context.rb
contentful_middleman-4.1.0 lib/contentful_middleman/commands/context.rb
contentful_middleman-3.0.1 lib/contentful_middleman/commands/context.rb
contentful_middleman-4.0.1 lib/contentful_middleman/commands/context.rb
contentful_middleman-4.0.0 lib/contentful_middleman/commands/context.rb
contentful_middleman-3.0.0 lib/contentful_middleman/commands/context.rb
contentful_middleman-2.1.3 lib/contentful_middleman/commands/context.rb
contentful_middleman-2.1.2 lib/contentful_middleman/commands/context.rb
contentful_middleman-2.1.1 lib/contentful_middleman/commands/context.rb
contentful_middleman-2.1.0 lib/contentful_middleman/commands/context.rb
contentful_middleman-2.0.1 lib/contentful_middleman/commands/context.rb
contentful_middleman-2.0.0 lib/contentful_middleman/commands/context.rb
contentful_middleman-1.5.0 lib/contentful_middleman/commands/context.rb
contentful_middleman-1.4.2 lib/contentful_middleman/commands/context.rb