Class: Alula::Content::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/alula/contents/metadata.rb

Instance Method Summary collapse

Constructor Details

#initialize(default = {}) ⇒ Metadata

Returns a new instance of Metadata.



4
5
6
7
8
# File 'lib/alula/contents/metadata.rb', line 4

def initialize(default = {})
  default.each do |key, value|
    self.send("#{key}=", value)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/alula/contents/metadata.rb', line 27

def method_missing(meth, *args, &blk)
  if meth[/=$/]
    instance_variable_set("@#{meth[0..-2]}", *args)
  else
    # Localisation support
    value = instance_variable_get("@#{meth}")
    if value.kind_of?(Hash)
      # Try environment & locale first
      if value.has_key?(environment) and value[environment].kind_of?(Hash)
        return value[environment][args[0]] if value[environment].has_key?(args[0])
        return value[environment][base_locale] if value[environment].has_key?(base_locale)
        return value[environment]
      else
        # Try locales
        return value[args[0]] if value.has_key?(args[0])
        return value[base_locale] if value.has_key?(base_locale)
        return value[environment] if value.has_key?(environment)
      end
    end
    
    value
  end
end

Instance Method Details

#date=(date) ⇒ Object

Custom accessors



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/alula/contents/metadata.rb', line 52

def date=(date)
  begin
    if date.kind_of?(String)
      @date = Time.parse(date)
    else
      @date = date
    end
  rescue ArgumentError => e
    @date = nil
  end
end

#languages(locale = nil) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/alula/contents/metadata.rb', line 64

def languages(locale = nil)
  if @title.kind_of?(Hash)
    @title.keys
  else
    [base_locale]
  end
end

#load(payload) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/alula/contents/metadata.rb', line 10

def load(payload)
  meta = YAML.load(payload)
  meta.each do |key, value|
    # Check for localisations
    self.send("#{key}=", value)
  end
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
# File 'lib/alula/contents/metadata.rb', line 18

def respond_to?(name)
  # Accept all setters
  return true if name.to_s =~ /=$/
  
  return true if instance_variable_defined?("@#{name}")
  
  super
end