Class: Domino::Form

Inherits:
Domino show all
Defined in:
lib/domino/form.rb

Defined Under Namespace

Classes: BooleanField, Field, SelectField

Constant Summary collapse

FIELD_TYPES =
{
  select: SelectField,
  boolean: BooleanField
}.freeze

Instance Attribute Summary

Attributes inherited from Domino

#node

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Domino

all, #attribute, attribute, attribute_definitions, #attributes, attributes, each, find!, find_by, find_by!, #id, selector, where

Class Method Details

.create(attributes = {}) ⇒ Object



55
56
57
# File 'lib/domino/form.rb', line 55

def self.create(attributes = {})
  find!.create(attributes)
end

.field(*args, &callback) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/domino/form.rb', line 31

def self.field(*args, &callback)
  options = args.last.is_a?(::Hash) ? args.pop : {}
  attribute, locator = *args

  locator ||= !@key.to_s.empty? ? "#{@key}[#{attribute}]" : attribute

  field_type = options.delete(:as)
  field_class = field_type.is_a?(Class) && field_type.ancestors.include?(Field) ? field_type : FIELD_TYPES[field_type] || Field

  field_definitions[attribute] = field_class.new(attribute, locator, options, &callback)

  define_method :"#{attribute}" do |&block|
    if block.is_a?(Proc)
      block.call(self.class.field_definitions[attribute].field(node))
    else
      self.class.field_definitions[attribute].value(node)
    end
  end

  define_method :"#{attribute}=" do |value|
    self.class.field_definitions[attribute].write(node, value)
  end
end

.field_definitionsObject



19
20
21
# File 'lib/domino/form.rb', line 19

def self.field_definitions
  @field_definitions ||= {}
end

.fieldsObject



15
16
17
# File 'lib/domino/form.rb', line 15

def self.fields
  field_definitions.keys
end

.key(k) ⇒ Object



11
12
13
# File 'lib/domino/form.rb', line 11

def self.key(k)
  @key = k
end

.submit_with(submitter) ⇒ Object



23
24
25
# File 'lib/domino/form.rb', line 23

def self.submit_with(submitter)
  @submitter = submitter
end

.submitterObject



27
28
29
# File 'lib/domino/form.rb', line 27

def self.submitter
  @submitter ||= "input[type='submit']"
end

.update(attributes = {}) ⇒ Object



59
60
61
# File 'lib/domino/form.rb', line 59

def self.update(attributes = {})
  find!.update(attributes)
end

Instance Method Details

#create(attributes = {}) ⇒ Object



63
64
65
66
# File 'lib/domino/form.rb', line 63

def create(attributes = {})
  set(attributes)
  save
end

#fieldsObject



81
82
83
84
85
# File 'lib/domino/form.rb', line 81

def fields
  self.class.fields.each_with_object({}) do |field, memo|
    memo[field] = send(field)
  end
end

#saveObject



77
78
79
# File 'lib/domino/form.rb', line 77

def save
  find(self.class.submitter).click
end

#set(attributes = {}) ⇒ Object



73
74
75
# File 'lib/domino/form.rb', line 73

def set(attributes = {})
  attributes.each { |k, v| send("#{k}=", v) }
end

#update(attributes = {}) ⇒ Object



68
69
70
71
# File 'lib/domino/form.rb', line 68

def update(attributes = {})
  set(attributes)
  save
end