Class: Jsonr::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonr/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Generator

Returns a new instance of Generator.



10
11
12
13
# File 'lib/jsonr/generator.rb', line 10

def initialize(&block)
  @commands = {}
  block.call(self) if block_given?
end

Instance Method Details

#append(selector, content) ⇒ Object

Appends given content to an element with given selector.

Example:

# Redirect to persons listing.
page.append "#persons", @person


39
40
41
42
# File 'lib/jsonr/generator.rb', line 39

def append(selector, content)
  @commands[:append] ||= {}
  @commands[:append][selector] = content
end

#data(key, value) ⇒ Object

Sets given key to a given value. Useful for passing custom objects.

Example:

page.data :person, @person


155
156
157
# File 'lib/jsonr/generator.rb', line 155

def data(key, value)
  @commands[key] = value
end

#flash(severity, message) ⇒ Object

Displays a flash notice.

Example:

# Show a flash message notice
page.flash :notice, "A person detail's were updated."


98
99
100
101
# File 'lib/jsonr/generator.rb', line 98

def flash(severity, message)
  @commands[:flash] ||= ActiveSupport::OrderedHash.new
  @commands[:flash][severity] = message
end

#hide(*selectors) ⇒ Object

Hides elements with given selectors.

Example:

# Hide a few people
page.hide '#person_1', '#person_2'


110
111
112
113
# File 'lib/jsonr/generator.rb', line 110

def hide(*selectors)
  @commands[:hide] ||= []
  @commands[:hide].push *selectors
end

#insert_before(selector, content) ⇒ Object

Inserts given content before an element with given selector.

Example:

# Redirect to persons listing.
page.insert_before "#persons", @person


75
76
77
78
# File 'lib/jsonr/generator.rb', line 75

def insert_before(selector, content)
  @commands[:insert_before] ||= {}
  @commands[:insert_before][selector] = content
end

#prepend(selector, content) ⇒ Object

Prepends given content to an element with given selector.

Example:

# Redirect to persons listing.
page.append "#persons", @person


51
52
53
54
# File 'lib/jsonr/generator.rb', line 51

def prepend(selector, content)
  @commands[:prepend] ||= {}
  @commands[:prepend][selector] = content
end

#redirect_to(url) ⇒ Object

Redirects a client to a given url.

Example:

# Redirect to persons listing.
page.redirect_to persons_url


87
88
89
# File 'lib/jsonr/generator.rb', line 87

def redirect_to(url)
  @commands[:redirect_to] = url
end

#remove(*selectors) ⇒ Object

Removes elements with the given selectors.

Example:

page.remove '#person_1', '#person_2'


145
146
147
148
# File 'lib/jsonr/generator.rb', line 145

def remove(*selectors)
  @commands[:remove] ||= []
  @commands[:remove].push *selectors
end

#replace(selector, content) ⇒ Object

Replaces a content of an element with given selector with given value.

Example:

# Redirect to persons listing.
page.replace "#person_1", @person


27
28
29
30
# File 'lib/jsonr/generator.rb', line 27

def replace(selector, content)
  @commands[:replace] ||= {}
  @commands[:replace][selector] = content
end

#replace_with(selector, content) ⇒ Object

Replaces an element with given selector with given value.

Example:

# Redirect to persons listing.
page.replace_with "#person_1", @person


63
64
65
66
# File 'lib/jsonr/generator.rb', line 63

def replace_with(selector, content)
  @commands[:replace_with] ||= {}
  @commands[:replace_with][selector] = content
end

#show(*selectors) ⇒ Object

Shows hidden elements with the given selectors.

Example:

# Show a few people
page.show '#person_1', '#person_2'


122
123
124
125
# File 'lib/jsonr/generator.rb', line 122

def show(*selectors)
  @commands[:show] ||= []
  @commands[:show].push *selectors
end

#to_sObject

Convert to a JSON string representation.



16
17
18
# File 'lib/jsonr/generator.rb', line 16

def to_s
  ActiveSupport::JSON.encode(@commands)
end

#toggle(*selectors) ⇒ Object

Toggles the visibility of the elements with the given selectors.

Example:

# Toggle a few people
page.toggle '#person_1', '#person_2'


134
135
136
137
# File 'lib/jsonr/generator.rb', line 134

def toggle(*selectors)
  @commands[:toggle] ||= []
  @commands[:toggle].push *selectors
end