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



41
42
43
44
45
46
47
48
49
50
# File 'lib/contentful_middleman/commands/context.rb', line 41

def ensure_primitive_data_types(value)
  case value
  when Context
    value.hashize
  when ::Array
    value.map {|element| ensure_primitive_data_types(element)}
  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

#hashizeObject



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

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)


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

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



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

def to_yaml
  hashize.to_yaml
end