Class: Phaseout::SEOFields

Inherits:
Object
  • Object
show all
Defined in:
lib/phaseout/seo_fields.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, human_name, values = Hash.new) {|_self| ... } ⇒ SEOFields

Returns a new instance of SEOFields.

Yields:

  • (_self)

Yield Parameters:



7
8
9
10
# File 'lib/phaseout/seo_fields.rb', line 7

def initialize(key, human_name, values = Hash.new, &block)
  @key, @human_name, @values = I18n.transliterate(key).gsub(/\s+/, '_').underscore, human_name, values
  yield(self) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



83
84
85
86
87
88
89
# File 'lib/phaseout/seo_fields.rb', line 83

def method_missing(method, *args, &block)
  if block_given?
    @values[method.to_sym] = block
  else
    @values[method.to_sym] = args unless args.empty?
  end
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



4
5
6
# File 'lib/phaseout/seo_fields.rb', line 4

def default
  @default
end

#human_nameObject (readonly)

Returns the value of attribute human_name.



3
4
5
# File 'lib/phaseout/seo_fields.rb', line 3

def human_name
  @human_name
end

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/phaseout/seo_fields.rb', line 3

def key
  @key
end

#valuesObject Also known as: fields

Returns the value of attribute values.



4
5
6
# File 'lib/phaseout/seo_fields.rb', line 4

def values
  @values
end

Class Method Details

.all(action_key, &block) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/phaseout/seo_fields.rb', line 104

def self.all(action_key, &block)
  unless block_given?
    values = []
    self.all(action_key){ |field| values << field }
    return values
  end

  class_index_key = "#{Phaseout.redis.namespace}:action:#{action_key}"
  Phaseout.redis.sscan_each(class_index_key) do |value|
    yield self.find value.match('seo_key:').post_match
  end
end

.find(key) ⇒ Object



99
100
101
102
# File 'lib/phaseout/seo_fields.rb', line 99

def self.find(key)
  dump = Phaseout.redis.get "seo_key:#{key}"
  dump ? Marshal.load(dump) : nil
end

Instance Method Details

#[](index) ⇒ Object



70
71
72
# File 'lib/phaseout/seo_fields.rb', line 70

def [](index)
  values[index.to_sym]
end

#[]=(index, value) ⇒ Object



74
75
76
# File 'lib/phaseout/seo_fields.rb', line 74

def []=(index, value)
  values[index.to_sym] = value
end

#actionObject



22
23
24
# File 'lib/phaseout/seo_fields.rb', line 22

def action
  @action ||= ::Phaseout::SEOAction.new action_key
end

#action_keyObject



18
19
20
# File 'lib/phaseout/seo_fields.rb', line 18

def action_key
  @action_key ||= @key.match('seo_key:').post_match.match(':').pre_match
end

#deleteObject



49
50
51
52
# File 'lib/phaseout/seo_fields.rb', line 49

def delete
  Phaseout::SEOAction.find(action_key).remove key
  Phaseout.redis.del key
end

#dumpObject



41
42
43
# File 'lib/phaseout/seo_fields.rb', line 41

def dump
  Marshal.dump self
end

#evaluated_values(controller) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/phaseout/seo_fields.rb', line 54

def evaluated_values(controller)
  @_values ||= if @default
    @default.evaluated_values(controller).merge @values
  else
    Hash[
      @values.map do |helper, argument|
        if argument.is_a? Proc
          [ helper, controller.instance_eval(&argument) ]
        else
          [ helper, argument ]
        end
      end
    ]
  end
end

#idObject



26
27
28
# File 'lib/phaseout/seo_fields.rb', line 26

def id
  @key.match(/\#.+\:/).post_match.gsub(/\s+/, '_').underscore
end

#inspectObject Also known as: to_s



78
79
80
# File 'lib/phaseout/seo_fields.rb', line 78

def inspect
  "#<Phaseout::SEO #{action_key} #{@human_name}>"
end

#marshal_dumpObject



91
92
93
# File 'lib/phaseout/seo_fields.rb', line 91

def marshal_dump
  [ @key, @human_name, @values ]
end

#marshal_load(dump_array) ⇒ Object



95
96
97
# File 'lib/phaseout/seo_fields.rb', line 95

def marshal_load(dump_array)
  @key, @human_name, @values = dump_array
end

#saveObject



45
46
47
# File 'lib/phaseout/seo_fields.rb', line 45

def save
  Phaseout.redis.set key, self.dump
end

#to_html(controller) ⇒ Object



12
13
14
15
16
# File 'lib/phaseout/seo_fields.rb', line 12

def to_html(controller)
  evaluated_values(controller).map do |helper, argument|
    controller.view_context.send helper, *argument
  end.join.html_safe
end

#to_jsonObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/phaseout/seo_fields.rb', line 30

def to_json
  {
    id:         id,
    key:        @key.match('seo_key:').post_match,
    fields:     @values,
    name:       @human_name,
    action_id:  action_key.gsub('#', '_').underscore,
    action_key: action_key
  }.to_json
end