Class: Formic::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/formic/base.rb

Direct Known Subclasses

Button, Form, Input, List, ListItem, ListItems, Tab, TabPanel

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, *args, &block) ⇒ Base

Returns a new instance of Base.



18
19
20
21
22
23
24
25
# File 'lib/formic/base.rb', line 18

def initialize page, *args, &block
  @page = page
  @options = {:class => []}

  _initialize *args, &block
  @options[:class].unshift( self.class.to_s.split('::').last.underscore )
  self.formics.unshift self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



33
34
35
36
37
# File 'lib/formic/base.rb', line 33

def method_missing method, *args, &block
  _initialize *args, &block
  self.add_class(method.to_s)
  return self
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



4
5
6
# File 'lib/formic/base.rb', line 4

def content
  @content
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/formic/base.rb', line 3

def options
  @options
end

#pageObject (readonly)

Returns the value of attribute page.



3
4
5
# File 'lib/formic/base.rb', line 3

def page
  @page
end

#templateObject

Returns the value of attribute template.



4
5
6
# File 'lib/formic/base.rb', line 4

def template
  @template
end

Class Method Details

.default_template(given_default_template) ⇒ Object



6
7
8
# File 'lib/formic/base.rb', line 6

def self.default_template given_default_template
  @template = given_default_template
end

.templateObject



10
11
12
# File 'lib/formic/base.rb', line 10

def self.template
  @template || self.superclass.template
end

Instance Method Details

#_initialize(options = {}, &block) ⇒ Object



39
40
41
42
43
# File 'lib/formic/base.rb', line 39

def _initialize options={}, &block
  self.merge_options options
  @content = block if block_given?
  return self
end

#add_class(classname) ⇒ Object



60
61
62
# File 'lib/formic/base.rb', line 60

def add_class classname
  self.options[:class].push classname
end

#formicsObject



49
50
51
52
# File 'lib/formic/base.rb', line 49

def formics
  @page.instance_variable_set(:@formics, []) unless @page.instance_variable_get(:@formics)
  @page.instance_variable_get(:@formics)
end

#has_class?(classname) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/formic/base.rb', line 45

def has_class? classname
  return self.options[:class].include? classname
end

#merge_options(new_options) ⇒ Object



54
55
56
57
58
# File 'lib/formic/base.rb', line 54

def merge_options new_options
  classes = [@options[:class], new_options[:class]].reject {|element| element.nil?}
  classes = classes.flatten.join(' ').split(' ').uniq
  @options.merge!(new_options).merge!(:class => classes)
end

#to_sObject



27
28
29
30
31
# File 'lib/formic/base.rb', line 27

def to_s
  rendered = self.page.render :partial => self.template, :locals => {:element => self}
  self.formics.shift
  return rendered
end