Class: Praxis::Blueprint::FieldsetParser

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis/blueprint.rb

Overview

Simple helper class that can parse the ‘attribute :foobar` dsl into an equivalent structure hash. Example: do

 attribute :one
 attribute :complex do
   attribute :sub1
 end
end

is parsed as: { one: true, complex: { sub1: true} }

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ FieldsetParser

Returns a new instance of FieldsetParser.



28
29
30
31
# File 'lib/praxis/blueprint.rb', line 28

def initialize(&block)
  @hash = nil
  @block = block
end

Instance Method Details

#attribute(name, **args, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/praxis/blueprint.rb', line 33

def attribute(name, **args, &block)
  unless args.empty?
    raise "Default fieldset definitions do not accept parameters (got: #{args})" \
          "If you're upgrading from a previous version of Praxis and still using the view :default " \
          "block syntax, make sure you don't use any view: X parameters when you define the attributes " \
          '(expand them explicitly if you want deeper structure)' \
          "The offending view with parameters is defined in:\n#{Kernel.caller.first}"
  end
  @hash[name] = block_given? ? FieldsetParser.new(&block).fieldset : true
end

#fieldsetObject



44
45
46
47
48
49
50
51
# File 'lib/praxis/blueprint.rb', line 44

def fieldset
  return @hash if @hash

  # Lazy eval
  @hash = {}
  instance_eval(&@block)
  @hash
end