Class: Stratagem::Model::Component::View

Inherits:
Base show all
Includes:
ParseUtil
Defined in:
lib/stratagem/model/components/view.rb

Constant Summary collapse

RAILS_FORM_FIELDS =
['check_box', 'file_field', 'hidden_field', 'password_field', 'radio_button', 'text_area', 'text_field']

Constants included from ParseUtil

ParseUtil::RUBY_OUTPUT_REGEX, ParseUtil::RUBY_REGEX

Instance Attribute Summary collapse

Attributes inherited from Base

#app_model, #klass, #parse_tree, #path, #source

Instance Method Summary collapse

Methods included from ParseUtil

find_classes, #gsub_ruby_blocks, qualified_class_name, #ruby_blocks, #ruby_output_blocks

Methods inherited from Base

#==, load_all, logger, #logger, #name

Constructor Details

#initialize(render_path) ⇒ View

Returns a new instance of View.



9
10
11
12
13
14
# File 'lib/stratagem/model/components/view.rb', line 9

def initialize(render_path)
  render_path =~ /\.(.*)/
  @extension = $1
  @path = render_path
  @render_path = render_path.gsub(/\..*/, '')
end

Instance Attribute Details

#extensionObject (readonly)

as seen by the controllers



7
8
9
# File 'lib/stratagem/model/components/view.rb', line 7

def extension
  @extension
end

#render_pathObject (readonly)

as seen by the controllers



7
8
9
# File 'lib/stratagem/model/components/view.rb', line 7

def render_path
  @render_path
end

Instance Method Details

#approximate_htmlObject



72
73
74
75
76
77
# File 'lib/stratagem/model/components/view.rb', line 72

def approximate_html
  html = read().gsub(RUBY_REGEX) do |match|
    handle_render(match)
  end
  html
end

#directoryObject



28
29
30
# File 'lib/stratagem/model/components/view.rb', line 28

def directory
  File.dirname(full_path)
end

#exportObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/stratagem/model/components/view.rb', line 32

def export
  begin
    {
      :external_id => self.object_id,
      :path => @path, 
      :render_path => @render_path, 
      # :forms => forms.map {|form| form.export } 
    }
  rescue
    logger.fatal($!)
    nil
  end
end

#formsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/stratagem/model/components/view.rb', line 50

def forms
  # extract ruby from the html
  ruby = ruby_blocks(approximate_html).join("\n")

  # dump ruby into a parse tree
  begin
    parse_tree = RedParse.new(ruby).parse
    # find the form nodes and send to a specialized function
    forms = []
    walk_tree(parse_tree) do |node|
      if (node.kind_of?(RedParse::CallNode) && (node.name =~ /form_/) && (node.block))
        forms << walk_form(node)
      end
    end
    forms
  rescue
    puts "ERROR: Unable to parse ruby. - #{$!.message}"
    puts ruby
    []
  end
end

#full_pathObject



24
25
26
# File 'lib/stratagem/model/components/view.rb', line 24

def full_path
  File.join(RAILS_ROOT, 'app', 'views', render_path+'.'+extension)
end

#handle_render(ruby) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/stratagem/model/components/view.rb', line 79

def handle_render(ruby)
  # remove the surrounding ruby indicators
  result = ruby
  ruby = ruby.stratagem_strip_erb
  if (ruby =~ /^render/)
    # load into parse tree

    parse_tree = RedParse.new(ruby).parse

    parse_tree.walk {|parent,i,subi,node|
      case node
      when RedParse::CallNode #... do something with method calls
        params = node.params.first # render always takes a hash
        render_path = nil
        passed_vars = {}
        if (params.kind_of?(RedParse::StringNode))
          render_path = params.first.to_s
        else
          render_path = params.get(:partial).first.split('/')
          object = params.get(:object).name if params.get(:object) 

          locals = params.get(:locals)
          render_path.last.gsub!(/^/, '_')
          render_path = render_path.join('/')
        end

        full_path = nil
        if (render_path =~ /\//)
          full_path = File.join(RAILS_ROOT, 'app', 'views', render_path+"."+extension)
        else
          full_path = File.join(directory, render_path+"."+extension)
        end

        begin
          result = File.read(full_path)
        rescue
          puts "ERROR: #{full_path} not found"
        end
      end
    }
  end
  result
end

#partial?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/stratagem/model/components/view.rb', line 20

def partial?
  File.basename(render_path) =~ /^_/
end

#readObject



16
17
18
# File 'lib/stratagem/model/components/view.rb', line 16

def read
  File.open(full_path) {|file| file.readlines().join }
end

#rendered_byObject



46
47
48
# File 'lib/stratagem/model/components/view.rb', line 46

def rendered_by
  
end