Class: ContentfulMiddleman::Context

Inherits:
BasicObject
Defined in:
lib/contentful_middleman/commands/context.rb

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



3
4
5
# File 'lib/contentful_middleman/commands/context.rb', line 3

def initialize
  @variables = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/contentful_middleman/commands/context.rb', line 7

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

Instance Method Details

#ensure_primitive_data_types(value) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/contentful_middleman/commands/context.rb', line 45

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

#get(name) ⇒ Object



22
23
24
# File 'lib/contentful_middleman/commands/context.rb', line 22

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

#has?(name) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/contentful_middleman/commands/context.rb', line 26

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

#hashizeObject



38
39
40
41
42
43
# File 'lib/contentful_middleman/commands/context.rb', line 38

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

#is_a?(klass) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/contentful_middleman/commands/context.rb', line 30

def is_a?(klass)
  Context == klass
end

#set(name, value) ⇒ Object



18
19
20
# File 'lib/contentful_middleman/commands/context.rb', line 18

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

#to_yamlObject



34
35
36
# File 'lib/contentful_middleman/commands/context.rb', line 34

def to_yaml
  hashize.to_yaml
end